1 /* SPDX-License-Identifier: GPL-2.0+ */
5 * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
8 #ifndef _SURFACE_AGGREGATOR_SSH_PARSER_H
9 #define _SURFACE_AGGREGATOR_SSH_PARSER_H
11 #include <linux/device.h>
12 #include <linux/kfifo.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
16 #include <linux/surface_aggregator/serial_hub.h>
19 * struct sshp_buf - Parser buffer for SSH messages.
20 * @ptr: Pointer to the beginning of the buffer.
21 * @len: Number of bytes used in the buffer.
22 * @cap: Maximum capacity of the buffer.
31 * sshp_buf_init() - Initialize a SSH parser buffer.
32 * @buf: The buffer to initialize.
33 * @ptr: The memory backing the buffer.
34 * @cap: The length of the memory backing the buffer, i.e. its capacity.
36 * Initializes the buffer with the given memory as backing and set its used
39 static inline void sshp_buf_init(struct sshp_buf
*buf
, u8
*ptr
, size_t cap
)
47 * sshp_buf_alloc() - Allocate and initialize a SSH parser buffer.
48 * @buf: The buffer to initialize/allocate to.
49 * @cap: The desired capacity of the buffer.
50 * @flags: The flags used for allocating the memory.
52 * Allocates @cap bytes and initializes the provided buffer struct with the
55 * Return: Returns zero on success and %-ENOMEM if allocation failed.
57 static inline int sshp_buf_alloc(struct sshp_buf
*buf
, size_t cap
, gfp_t flags
)
61 ptr
= kzalloc(cap
, flags
);
65 sshp_buf_init(buf
, ptr
, cap
);
70 * sshp_buf_free() - Free a SSH parser buffer.
71 * @buf: The buffer to free.
73 * Frees a SSH parser buffer by freeing the memory backing it and then
74 * resetting its pointer to %NULL and length and capacity to zero. Intended to
75 * free a buffer previously allocated with sshp_buf_alloc().
77 static inline void sshp_buf_free(struct sshp_buf
*buf
)
86 * sshp_buf_drop() - Drop data from the beginning of the buffer.
87 * @buf: The buffer to drop data from.
88 * @n: The number of bytes to drop.
90 * Drops the first @n bytes from the buffer. Re-aligns any remaining data to
91 * the beginning of the buffer.
93 static inline void sshp_buf_drop(struct sshp_buf
*buf
, size_t n
)
95 memmove(buf
->ptr
, buf
->ptr
+ n
, buf
->len
- n
);
100 * sshp_buf_read_from_fifo() - Transfer data from a fifo to the buffer.
101 * @buf: The buffer to write the data into.
102 * @fifo: The fifo to read the data from.
104 * Transfers the data contained in the fifo to the buffer, removing it from
105 * the fifo. This function will try to transfer as much data as possible,
106 * limited either by the remaining space in the buffer or by the number of
107 * bytes available in the fifo.
109 * Return: Returns the number of bytes transferred.
111 static inline size_t sshp_buf_read_from_fifo(struct sshp_buf
*buf
,
116 n
= kfifo_out(fifo
, buf
->ptr
+ buf
->len
, buf
->cap
- buf
->len
);
123 * sshp_buf_span_from() - Initialize a span from the given buffer and offset.
124 * @buf: The buffer to create the span from.
125 * @offset: The offset in the buffer at which the span should start.
126 * @span: The span to initialize (output).
128 * Initializes the provided span to point to the memory at the given offset in
129 * the buffer, with the length of the span being capped by the number of bytes
130 * used in the buffer after the offset (i.e. bytes remaining after the
133 * Warning: This function does not validate that @offset is less than or equal
134 * to the number of bytes used in the buffer or the buffer capacity. This must
135 * be guaranteed by the caller.
137 static inline void sshp_buf_span_from(struct sshp_buf
*buf
, size_t offset
,
138 struct ssam_span
*span
)
140 span
->ptr
= buf
->ptr
+ offset
;
141 span
->len
= buf
->len
- offset
;
144 bool sshp_find_syn(const struct ssam_span
*src
, struct ssam_span
*rem
);
146 int sshp_parse_frame(const struct device
*dev
, const struct ssam_span
*source
,
147 struct ssh_frame
**frame
, struct ssam_span
*payload
,
150 int sshp_parse_command(const struct device
*dev
, const struct ssam_span
*source
,
151 struct ssh_command
**command
,
152 struct ssam_span
*command_data
);
154 #endif /* _SURFACE_AGGREGATOR_SSH_PARSER_h */