can_emu.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * CAN common CAN bus emulation support
  3. *
  4. * Copyright (c) 2013-2014 Jin Yang
  5. * Copyright (c) 2014-2018 Pavel Pisa
  6. *
  7. * Initial development supported by Google GSoC 2013 from RTEMS project slot
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #ifndef NET_CAN_EMU_H
  28. #define NET_CAN_EMU_H
  29. #include "qemu/queue.h"
  30. #include "qom/object.h"
  31. /* NOTE: the following two structures is copied from <linux/can.h>. */
  32. /*
  33. * Controller Area Network Identifier structure
  34. *
  35. * bit 0-28 : CAN identifier (11/29 bit)
  36. * bit 29 : error frame flag (0 = data frame, 1 = error frame)
  37. * bit 30 : remote transmission request flag (1 = rtr frame)
  38. * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
  39. */
  40. typedef uint32_t qemu_canid_t;
  41. typedef struct qemu_can_frame {
  42. qemu_canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
  43. uint8_t can_dlc; /* data length code: 0 .. 8 */
  44. uint8_t flags;
  45. uint8_t data[64] QEMU_ALIGNED(8);
  46. } qemu_can_frame;
  47. /* Keep defines for QEMU separate from Linux ones for now */
  48. #define QEMU_CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
  49. #define QEMU_CAN_RTR_FLAG 0x40000000U /* remote transmission request */
  50. #define QEMU_CAN_ERR_FLAG 0x20000000U /* error message frame */
  51. #define QEMU_CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
  52. #define QEMU_CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
  53. #define QEMU_CAN_FRMF_BRS 0x01 /* bit rate switch (2nd bitrate for data) */
  54. #define QEMU_CAN_FRMF_ESI 0x02 /* error state ind. of transmitting node */
  55. #define QEMU_CAN_FRMF_TYPE_FD 0x10 /* internal bit ind. of CAN FD frame */
  56. /**
  57. * struct qemu_can_filter - CAN ID based filter in can_register().
  58. * @can_id: relevant bits of CAN ID which are not masked out.
  59. * @can_mask: CAN mask (see description)
  60. *
  61. * Description:
  62. * A filter matches, when
  63. *
  64. * <received_can_id> & mask == can_id & mask
  65. *
  66. * The filter can be inverted (QEMU_CAN_INV_FILTER bit set in can_id) or it can
  67. * filter for error message frames (QEMU_CAN_ERR_FLAG bit set in mask).
  68. */
  69. typedef struct qemu_can_filter {
  70. qemu_canid_t can_id;
  71. qemu_canid_t can_mask;
  72. } qemu_can_filter;
  73. /* QEMU_CAN_INV_FILTER can be set in qemu_can_filter.can_id */
  74. #define QEMU_CAN_INV_FILTER 0x20000000U
  75. typedef struct CanBusClientState CanBusClientState;
  76. typedef struct CanBusState CanBusState;
  77. typedef struct CanBusClientInfo {
  78. bool (*can_receive)(CanBusClientState *);
  79. ssize_t (*receive)(CanBusClientState *,
  80. const struct qemu_can_frame *frames, size_t frames_cnt);
  81. } CanBusClientInfo;
  82. struct CanBusClientState {
  83. CanBusClientInfo *info;
  84. CanBusState *bus;
  85. int link_down;
  86. QTAILQ_ENTRY(CanBusClientState) next;
  87. CanBusClientState *peer;
  88. char *model;
  89. char *name;
  90. void (*destructor)(CanBusClientState *);
  91. bool fd_mode;
  92. };
  93. #define TYPE_CAN_BUS "can-bus"
  94. OBJECT_DECLARE_SIMPLE_TYPE(CanBusState, CAN_BUS)
  95. int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_id);
  96. int can_bus_insert_client(CanBusState *bus, CanBusClientState *client);
  97. int can_bus_remove_client(CanBusClientState *client);
  98. ssize_t can_bus_client_send(CanBusClientState *,
  99. const struct qemu_can_frame *frames,
  100. size_t frames_cnt);
  101. int can_bus_client_set_filters(CanBusClientState *,
  102. const struct qemu_can_filter *filters,
  103. size_t filters_cnt);
  104. uint8_t can_dlc2len(uint8_t can_dlc);
  105. uint8_t can_len2dlc(uint8_t len);
  106. #endif