usbip_common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <linux/sched/task.h>
  33. #include <uapi/linux/usbip.h>
  34. #define USBIP_VERSION "1.0.0"
  35. #undef pr_fmt
  36. #ifdef DEBUG
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
  38. #else
  39. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  40. #endif
  41. enum {
  42. usbip_debug_xmit = (1 << 0),
  43. usbip_debug_sysfs = (1 << 1),
  44. usbip_debug_urb = (1 << 2),
  45. usbip_debug_eh = (1 << 3),
  46. usbip_debug_stub_cmp = (1 << 8),
  47. usbip_debug_stub_dev = (1 << 9),
  48. usbip_debug_stub_rx = (1 << 10),
  49. usbip_debug_stub_tx = (1 << 11),
  50. usbip_debug_vhci_rh = (1 << 8),
  51. usbip_debug_vhci_hc = (1 << 9),
  52. usbip_debug_vhci_rx = (1 << 10),
  53. usbip_debug_vhci_tx = (1 << 11),
  54. usbip_debug_vhci_sysfs = (1 << 12)
  55. };
  56. #define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
  57. #define usbip_dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
  58. #define usbip_dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
  59. #define usbip_dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
  60. #define usbip_dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
  61. #define usbip_dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
  62. #define usbip_dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
  63. #define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
  64. extern unsigned long usbip_debug_flag;
  65. extern struct device_attribute dev_attr_usbip_debug;
  66. #define usbip_dbg_with_flag(flag, fmt, args...) \
  67. do { \
  68. if (flag & usbip_debug_flag) \
  69. pr_debug(fmt, ##args); \
  70. } while (0)
  71. #define usbip_dbg_sysfs(fmt, args...) \
  72. usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
  73. #define usbip_dbg_xmit(fmt, args...) \
  74. usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
  75. #define usbip_dbg_urb(fmt, args...) \
  76. usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
  77. #define usbip_dbg_eh(fmt, args...) \
  78. usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
  79. #define usbip_dbg_vhci_rh(fmt, args...) \
  80. usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
  81. #define usbip_dbg_vhci_hc(fmt, args...) \
  82. usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
  83. #define usbip_dbg_vhci_rx(fmt, args...) \
  84. usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
  85. #define usbip_dbg_vhci_tx(fmt, args...) \
  86. usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
  87. #define usbip_dbg_vhci_sysfs(fmt, args...) \
  88. usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
  89. #define usbip_dbg_stub_cmp(fmt, args...) \
  90. usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
  91. #define usbip_dbg_stub_rx(fmt, args...) \
  92. usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
  93. #define usbip_dbg_stub_tx(fmt, args...) \
  94. usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
  95. /*
  96. * USB/IP request headers
  97. *
  98. * Each request is transferred across the network to its counterpart, which
  99. * facilitates the normal USB communication. The values contained in the headers
  100. * are basically the same as in a URB. Currently, four request types are
  101. * defined:
  102. *
  103. * - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb()
  104. * (client to server)
  105. *
  106. * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
  107. * (server to client)
  108. *
  109. * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
  110. * corresponds to usb_unlink_urb()
  111. * (client to server)
  112. *
  113. * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
  114. * (server to client)
  115. *
  116. */
  117. #define USBIP_CMD_SUBMIT 0x0001
  118. #define USBIP_CMD_UNLINK 0x0002
  119. #define USBIP_RET_SUBMIT 0x0003
  120. #define USBIP_RET_UNLINK 0x0004
  121. #define USBIP_DIR_OUT 0x00
  122. #define USBIP_DIR_IN 0x01
  123. /**
  124. * struct usbip_header_basic - data pertinent to every request
  125. * @command: the usbip request type
  126. * @seqnum: sequential number that identifies requests; incremented per
  127. * connection
  128. * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
  129. * in the stub driver, this value is ((busnum << 16) | devnum)
  130. * @direction: direction of the transfer
  131. * @ep: endpoint number
  132. */
  133. struct usbip_header_basic {
  134. __u32 command;
  135. __u32 seqnum;
  136. __u32 devid;
  137. __u32 direction;
  138. __u32 ep;
  139. } __packed;
  140. /**
  141. * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
  142. * @transfer_flags: URB flags
  143. * @transfer_buffer_length: the data size for (in) or (out) transfer
  144. * @start_frame: initial frame for isochronous or interrupt transfers
  145. * @number_of_packets: number of isochronous packets
  146. * @interval: maximum time for the request on the server-side host controller
  147. * @setup: setup data for a control request
  148. */
  149. struct usbip_header_cmd_submit {
  150. __u32 transfer_flags;
  151. __s32 transfer_buffer_length;
  152. /* it is difficult for usbip to sync frames (reserved only?) */
  153. __s32 start_frame;
  154. __s32 number_of_packets;
  155. __s32 interval;
  156. unsigned char setup[8];
  157. } __packed;
  158. /**
  159. * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
  160. * @status: return status of a non-iso request
  161. * @actual_length: number of bytes transferred
  162. * @start_frame: initial frame for isochronous or interrupt transfers
  163. * @number_of_packets: number of isochronous packets
  164. * @error_count: number of errors for isochronous transfers
  165. */
  166. struct usbip_header_ret_submit {
  167. __s32 status;
  168. __s32 actual_length;
  169. __s32 start_frame;
  170. __s32 number_of_packets;
  171. __s32 error_count;
  172. } __packed;
  173. /**
  174. * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
  175. * @seqnum: the URB seqnum to unlink
  176. */
  177. struct usbip_header_cmd_unlink {
  178. __u32 seqnum;
  179. } __packed;
  180. /**
  181. * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
  182. * @status: return status of the request
  183. */
  184. struct usbip_header_ret_unlink {
  185. __s32 status;
  186. } __packed;
  187. /**
  188. * struct usbip_header - common header for all usbip packets
  189. * @base: the basic header
  190. * @u: packet type dependent header
  191. */
  192. struct usbip_header {
  193. struct usbip_header_basic base;
  194. union {
  195. struct usbip_header_cmd_submit cmd_submit;
  196. struct usbip_header_ret_submit ret_submit;
  197. struct usbip_header_cmd_unlink cmd_unlink;
  198. struct usbip_header_ret_unlink ret_unlink;
  199. } u;
  200. } __packed;
  201. /*
  202. * This is the same as usb_iso_packet_descriptor but packed for pdu.
  203. */
  204. struct usbip_iso_packet_descriptor {
  205. __u32 offset;
  206. __u32 length; /* expected length */
  207. __u32 actual_length;
  208. __u32 status;
  209. } __packed;
  210. enum usbip_side {
  211. USBIP_VHCI,
  212. USBIP_STUB,
  213. USBIP_VUDC,
  214. };
  215. /* event handler */
  216. #define USBIP_EH_SHUTDOWN (1 << 0)
  217. #define USBIP_EH_BYE (1 << 1)
  218. #define USBIP_EH_RESET (1 << 2)
  219. #define USBIP_EH_UNUSABLE (1 << 3)
  220. #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
  221. #define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  222. #define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  223. #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  224. #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  225. #define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
  226. #define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  227. #define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  228. /* catastrophic emulated usb error */
  229. #define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  230. #define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  231. #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
  232. #define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  233. #define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  234. #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  235. /* a common structure for stub_device and vhci_device */
  236. struct usbip_device {
  237. enum usbip_side side;
  238. enum usbip_device_status status;
  239. /* lock for status */
  240. spinlock_t lock;
  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 */