usbip_common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * Arbitrary limit for the maximum number of isochronous packets in an URB,
  110. * compare for example the uhci_submit_isochronous function in
  111. * drivers/usb/host/uhci-q.c
  112. */
  113. #define USBIP_MAX_ISO_PACKETS 1024
  114. /**
  115. * struct usbip_header_basic - data pertinent to every request
  116. * @command: the usbip request type
  117. * @seqnum: sequential number that identifies requests; incremented per
  118. * connection
  119. * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
  120. * in the stub driver, this value is ((busnum << 16) | devnum)
  121. * @direction: direction of the transfer
  122. * @ep: endpoint number
  123. */
  124. struct usbip_header_basic {
  125. __u32 command;
  126. __u32 seqnum;
  127. __u32 devid;
  128. __u32 direction;
  129. __u32 ep;
  130. } __packed;
  131. /**
  132. * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
  133. * @transfer_flags: URB flags
  134. * @transfer_buffer_length: the data size for (in) or (out) transfer
  135. * @start_frame: initial frame for isochronous or interrupt transfers
  136. * @number_of_packets: number of isochronous packets
  137. * @interval: maximum time for the request on the server-side host controller
  138. * @setup: setup data for a control request
  139. */
  140. struct usbip_header_cmd_submit {
  141. __u32 transfer_flags;
  142. __s32 transfer_buffer_length;
  143. /* it is difficult for usbip to sync frames (reserved only?) */
  144. __s32 start_frame;
  145. __s32 number_of_packets;
  146. __s32 interval;
  147. unsigned char setup[8];
  148. } __packed;
  149. /**
  150. * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
  151. * @status: return status of a non-iso request
  152. * @actual_length: number of bytes transferred
  153. * @start_frame: initial frame for isochronous or interrupt transfers
  154. * @number_of_packets: number of isochronous packets
  155. * @error_count: number of errors for isochronous transfers
  156. */
  157. struct usbip_header_ret_submit {
  158. __s32 status;
  159. __s32 actual_length;
  160. __s32 start_frame;
  161. __s32 number_of_packets;
  162. __s32 error_count;
  163. } __packed;
  164. /**
  165. * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
  166. * @seqnum: the URB seqnum to unlink
  167. */
  168. struct usbip_header_cmd_unlink {
  169. __u32 seqnum;
  170. } __packed;
  171. /**
  172. * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
  173. * @status: return status of the request
  174. */
  175. struct usbip_header_ret_unlink {
  176. __s32 status;
  177. } __packed;
  178. /**
  179. * struct usbip_header - common header for all usbip packets
  180. * @base: the basic header
  181. * @u: packet type dependent header
  182. */
  183. struct usbip_header {
  184. struct usbip_header_basic base;
  185. union {
  186. struct usbip_header_cmd_submit cmd_submit;
  187. struct usbip_header_ret_submit ret_submit;
  188. struct usbip_header_cmd_unlink cmd_unlink;
  189. struct usbip_header_ret_unlink ret_unlink;
  190. } u;
  191. } __packed;
  192. /*
  193. * This is the same as usb_iso_packet_descriptor but packed for pdu.
  194. */
  195. struct usbip_iso_packet_descriptor {
  196. __u32 offset;
  197. __u32 length; /* expected length */
  198. __u32 actual_length;
  199. __u32 status;
  200. } __packed;
  201. enum usbip_side {
  202. USBIP_VHCI,
  203. USBIP_STUB,
  204. USBIP_VUDC,
  205. };
  206. /* event handler */
  207. #define USBIP_EH_SHUTDOWN (1 << 0)
  208. #define USBIP_EH_BYE (1 << 1)
  209. #define USBIP_EH_RESET (1 << 2)
  210. #define USBIP_EH_UNUSABLE (1 << 3)
  211. #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
  212. #define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  213. #define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  214. #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  215. #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  216. #define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
  217. #define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  218. #define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  219. /* catastrophic emulated usb error */
  220. #define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  221. #define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  222. #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
  223. #define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  224. #define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  225. #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  226. /* a common structure for stub_device and vhci_device */
  227. struct usbip_device {
  228. enum usbip_side side;
  229. enum usbip_device_status status;
  230. /* lock for status */
  231. spinlock_t lock;
  232. int sockfd;
  233. struct socket *tcp_socket;
  234. struct task_struct *tcp_rx;
  235. struct task_struct *tcp_tx;
  236. unsigned long event;
  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_init_eh(void);
  274. void usbip_finish_eh(void);
  275. int usbip_start_eh(struct usbip_device *ud);
  276. void usbip_stop_eh(struct usbip_device *ud);
  277. void usbip_event_add(struct usbip_device *ud, unsigned long event);
  278. int usbip_event_happened(struct usbip_device *ud);
  279. int usbip_in_eh(struct task_struct *task);
  280. static inline int interface_to_busnum(struct usb_interface *interface)
  281. {
  282. struct usb_device *udev = interface_to_usbdev(interface);
  283. return udev->bus->busnum;
  284. }
  285. static inline int interface_to_devnum(struct usb_interface *interface)
  286. {
  287. struct usb_device *udev = interface_to_usbdev(interface);
  288. return udev->devnum;
  289. }
  290. #endif /* __USBIP_COMMON_H */