Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * SSH message parser.
0004  *
0005  * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
0006  */
0007 
0008 #ifndef _SURFACE_AGGREGATOR_SSH_PARSER_H
0009 #define _SURFACE_AGGREGATOR_SSH_PARSER_H
0010 
0011 #include <linux/device.h>
0012 #include <linux/kfifo.h>
0013 #include <linux/slab.h>
0014 #include <linux/types.h>
0015 
0016 #include <linux/surface_aggregator/serial_hub.h>
0017 
0018 /**
0019  * struct sshp_buf - Parser buffer for SSH messages.
0020  * @ptr: Pointer to the beginning of the buffer.
0021  * @len: Number of bytes used in the buffer.
0022  * @cap: Maximum capacity of the buffer.
0023  */
0024 struct sshp_buf {
0025     u8    *ptr;
0026     size_t len;
0027     size_t cap;
0028 };
0029 
0030 /**
0031  * sshp_buf_init() - Initialize a SSH parser buffer.
0032  * @buf: The buffer to initialize.
0033  * @ptr: The memory backing the buffer.
0034  * @cap: The length of the memory backing the buffer, i.e. its capacity.
0035  *
0036  * Initializes the buffer with the given memory as backing and set its used
0037  * length to zero.
0038  */
0039 static inline void sshp_buf_init(struct sshp_buf *buf, u8 *ptr, size_t cap)
0040 {
0041     buf->ptr = ptr;
0042     buf->len = 0;
0043     buf->cap = cap;
0044 }
0045 
0046 /**
0047  * sshp_buf_alloc() - Allocate and initialize a SSH parser buffer.
0048  * @buf:   The buffer to initialize/allocate to.
0049  * @cap:   The desired capacity of the buffer.
0050  * @flags: The flags used for allocating the memory.
0051  *
0052  * Allocates @cap bytes and initializes the provided buffer struct with the
0053  * allocated memory.
0054  *
0055  * Return: Returns zero on success and %-ENOMEM if allocation failed.
0056  */
0057 static inline int sshp_buf_alloc(struct sshp_buf *buf, size_t cap, gfp_t flags)
0058 {
0059     u8 *ptr;
0060 
0061     ptr = kzalloc(cap, flags);
0062     if (!ptr)
0063         return -ENOMEM;
0064 
0065     sshp_buf_init(buf, ptr, cap);
0066     return 0;
0067 }
0068 
0069 /**
0070  * sshp_buf_free() - Free a SSH parser buffer.
0071  * @buf: The buffer to free.
0072  *
0073  * Frees a SSH parser buffer by freeing the memory backing it and then
0074  * resetting its pointer to %NULL and length and capacity to zero. Intended to
0075  * free a buffer previously allocated with sshp_buf_alloc().
0076  */
0077 static inline void sshp_buf_free(struct sshp_buf *buf)
0078 {
0079     kfree(buf->ptr);
0080     buf->ptr = NULL;
0081     buf->len = 0;
0082     buf->cap = 0;
0083 }
0084 
0085 /**
0086  * sshp_buf_drop() - Drop data from the beginning of the buffer.
0087  * @buf: The buffer to drop data from.
0088  * @n:   The number of bytes to drop.
0089  *
0090  * Drops the first @n bytes from the buffer. Re-aligns any remaining data to
0091  * the beginning of the buffer.
0092  */
0093 static inline void sshp_buf_drop(struct sshp_buf *buf, size_t n)
0094 {
0095     memmove(buf->ptr, buf->ptr + n, buf->len - n);
0096     buf->len -= n;
0097 }
0098 
0099 /**
0100  * sshp_buf_read_from_fifo() - Transfer data from a fifo to the buffer.
0101  * @buf:  The buffer to write the data into.
0102  * @fifo: The fifo to read the data from.
0103  *
0104  * Transfers the data contained in the fifo to the buffer, removing it from
0105  * the fifo. This function will try to transfer as much data as possible,
0106  * limited either by the remaining space in the buffer or by the number of
0107  * bytes available in the fifo.
0108  *
0109  * Return: Returns the number of bytes transferred.
0110  */
0111 static inline size_t sshp_buf_read_from_fifo(struct sshp_buf *buf,
0112                          struct kfifo *fifo)
0113 {
0114     size_t n;
0115 
0116     n =  kfifo_out(fifo, buf->ptr + buf->len, buf->cap - buf->len);
0117     buf->len += n;
0118 
0119     return n;
0120 }
0121 
0122 /**
0123  * sshp_buf_span_from() - Initialize a span from the given buffer and offset.
0124  * @buf:    The buffer to create the span from.
0125  * @offset: The offset in the buffer at which the span should start.
0126  * @span:   The span to initialize (output).
0127  *
0128  * Initializes the provided span to point to the memory at the given offset in
0129  * the buffer, with the length of the span being capped by the number of bytes
0130  * used in the buffer after the offset (i.e. bytes remaining after the
0131  * offset).
0132  *
0133  * Warning: This function does not validate that @offset is less than or equal
0134  * to the number of bytes used in the buffer or the buffer capacity. This must
0135  * be guaranteed by the caller.
0136  */
0137 static inline void sshp_buf_span_from(struct sshp_buf *buf, size_t offset,
0138                       struct ssam_span *span)
0139 {
0140     span->ptr = buf->ptr + offset;
0141     span->len = buf->len - offset;
0142 }
0143 
0144 bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem);
0145 
0146 int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
0147              struct ssh_frame **frame, struct ssam_span *payload,
0148              size_t maxlen);
0149 
0150 int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
0151                struct ssh_command **command,
0152                struct ssam_span *command_data);
0153 
0154 #endif /* _SURFACE_AGGREGATOR_SSH_PARSER_h */