ssh_parser.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * SSH message parser.
  4. *
  5. * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
  6. */
  7. #ifndef _SURFACE_AGGREGATOR_SSH_PARSER_H
  8. #define _SURFACE_AGGREGATOR_SSH_PARSER_H
  9. #include <linux/device.h>
  10. #include <linux/kfifo.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/surface_aggregator/serial_hub.h>
  14. /**
  15. * struct sshp_buf - Parser buffer for SSH messages.
  16. * @ptr: Pointer to the beginning of the buffer.
  17. * @len: Number of bytes used in the buffer.
  18. * @cap: Maximum capacity of the buffer.
  19. */
  20. struct sshp_buf {
  21. u8 *ptr;
  22. size_t len;
  23. size_t cap;
  24. };
  25. /**
  26. * sshp_buf_init() - Initialize a SSH parser buffer.
  27. * @buf: The buffer to initialize.
  28. * @ptr: The memory backing the buffer.
  29. * @cap: The length of the memory backing the buffer, i.e. its capacity.
  30. *
  31. * Initializes the buffer with the given memory as backing and set its used
  32. * length to zero.
  33. */
  34. static inline void sshp_buf_init(struct sshp_buf *buf, u8 *ptr, size_t cap)
  35. {
  36. buf->ptr = ptr;
  37. buf->len = 0;
  38. buf->cap = cap;
  39. }
  40. /**
  41. * sshp_buf_alloc() - Allocate and initialize a SSH parser buffer.
  42. * @buf: The buffer to initialize/allocate to.
  43. * @cap: The desired capacity of the buffer.
  44. * @flags: The flags used for allocating the memory.
  45. *
  46. * Allocates @cap bytes and initializes the provided buffer struct with the
  47. * allocated memory.
  48. *
  49. * Return: Returns zero on success and %-ENOMEM if allocation failed.
  50. */
  51. static inline int sshp_buf_alloc(struct sshp_buf *buf, size_t cap, gfp_t flags)
  52. {
  53. u8 *ptr;
  54. ptr = kzalloc(cap, flags);
  55. if (!ptr)
  56. return -ENOMEM;
  57. sshp_buf_init(buf, ptr, cap);
  58. return 0;
  59. }
  60. /**
  61. * sshp_buf_free() - Free a SSH parser buffer.
  62. * @buf: The buffer to free.
  63. *
  64. * Frees a SSH parser buffer by freeing the memory backing it and then
  65. * resetting its pointer to %NULL and length and capacity to zero. Intended to
  66. * free a buffer previously allocated with sshp_buf_alloc().
  67. */
  68. static inline void sshp_buf_free(struct sshp_buf *buf)
  69. {
  70. kfree(buf->ptr);
  71. buf->ptr = NULL;
  72. buf->len = 0;
  73. buf->cap = 0;
  74. }
  75. /**
  76. * sshp_buf_drop() - Drop data from the beginning of the buffer.
  77. * @buf: The buffer to drop data from.
  78. * @n: The number of bytes to drop.
  79. *
  80. * Drops the first @n bytes from the buffer. Re-aligns any remaining data to
  81. * the beginning of the buffer.
  82. */
  83. static inline void sshp_buf_drop(struct sshp_buf *buf, size_t n)
  84. {
  85. memmove(buf->ptr, buf->ptr + n, buf->len - n);
  86. buf->len -= n;
  87. }
  88. /**
  89. * sshp_buf_read_from_fifo() - Transfer data from a fifo to the buffer.
  90. * @buf: The buffer to write the data into.
  91. * @fifo: The fifo to read the data from.
  92. *
  93. * Transfers the data contained in the fifo to the buffer, removing it from
  94. * the fifo. This function will try to transfer as much data as possible,
  95. * limited either by the remaining space in the buffer or by the number of
  96. * bytes available in the fifo.
  97. *
  98. * Return: Returns the number of bytes transferred.
  99. */
  100. static inline size_t sshp_buf_read_from_fifo(struct sshp_buf *buf,
  101. struct kfifo *fifo)
  102. {
  103. size_t n;
  104. n = kfifo_out(fifo, buf->ptr + buf->len, buf->cap - buf->len);
  105. buf->len += n;
  106. return n;
  107. }
  108. /**
  109. * sshp_buf_span_from() - Initialize a span from the given buffer and offset.
  110. * @buf: The buffer to create the span from.
  111. * @offset: The offset in the buffer at which the span should start.
  112. * @span: The span to initialize (output).
  113. *
  114. * Initializes the provided span to point to the memory at the given offset in
  115. * the buffer, with the length of the span being capped by the number of bytes
  116. * used in the buffer after the offset (i.e. bytes remaining after the
  117. * offset).
  118. *
  119. * Warning: This function does not validate that @offset is less than or equal
  120. * to the number of bytes used in the buffer or the buffer capacity. This must
  121. * be guaranteed by the caller.
  122. */
  123. static inline void sshp_buf_span_from(struct sshp_buf *buf, size_t offset,
  124. struct ssam_span *span)
  125. {
  126. span->ptr = buf->ptr + offset;
  127. span->len = buf->len - offset;
  128. }
  129. bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem);
  130. int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
  131. struct ssh_frame **frame, struct ssam_span *payload,
  132. size_t maxlen);
  133. int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
  134. struct ssh_command **command,
  135. struct ssam_span *command_data);
  136. #endif /* _SURFACE_AGGREGATOR_SSH_PARSER_h */