/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (C) ST-Ericsson AB 20103* Author: Sjur Brendeland4*/56#ifndef CFPKT_H_7#define CFPKT_H_8#include <net/caif/caif_layer.h>9#include <linux/types.h>10struct cfpkt;1112/* Create a CAIF packet.13* len: Length of packet to be created14* @return New packet.15*/16struct cfpkt *cfpkt_create(u16 len);1718/*19* Destroy a CAIF Packet.20* pkt Packet to be destroyed.21*/22void cfpkt_destroy(struct cfpkt *pkt);2324/*25* Extract header from packet.26*27* pkt Packet to extract header data from.28* data Pointer to copy the header data into.29* len Length of head data to copy.30* @return zero on success and error code upon failure31*/32int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len);3334static inline u8 cfpkt_extr_head_u8(struct cfpkt *pkt)35{36u8 tmp;3738cfpkt_extr_head(pkt, &tmp, 1);3940return tmp;41}4243static inline u16 cfpkt_extr_head_u16(struct cfpkt *pkt)44{45__le16 tmp;4647cfpkt_extr_head(pkt, &tmp, 2);4849return le16_to_cpu(tmp);50}5152static inline u32 cfpkt_extr_head_u32(struct cfpkt *pkt)53{54__le32 tmp;5556cfpkt_extr_head(pkt, &tmp, 4);5758return le32_to_cpu(tmp);59}6061/*62* Peek header from packet.63* Reads data from packet without changing packet.64*65* pkt Packet to extract header data from.66* data Pointer to copy the header data into.67* len Length of head data to copy.68* @return zero on success and error code upon failure69*/70int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len);7172/*73* Extract header from trailer (end of packet).74*75* pkt Packet to extract header data from.76* data Pointer to copy the trailer data into.77* len Length of header data to copy.78* @return zero on success and error code upon failure79*/80int cfpkt_extr_trail(struct cfpkt *pkt, void *data, u16 len);8182/*83* Add header to packet.84*85*86* pkt Packet to add header data to.87* data Pointer to data to copy into the header.88* len Length of header data to copy.89* @return zero on success and error code upon failure90*/91int cfpkt_add_head(struct cfpkt *pkt, const void *data, u16 len);9293/*94* Add trailer to packet.95*96*97* pkt Packet to add trailer data to.98* data Pointer to data to copy into the trailer.99* len Length of trailer data to copy.100* @return zero on success and error code upon failure101*/102int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len);103104/*105* Pad trailer on packet.106* Moves data pointer in packet, no content copied.107*108* pkt Packet in which to pad trailer.109* len Length of padding to add.110* @return zero on success and error code upon failure111*/112int cfpkt_pad_trail(struct cfpkt *pkt, u16 len);113114/*115* Add a single byte to packet body (tail).116*117* pkt Packet in which to add byte.118* data Byte to add.119* @return zero on success and error code upon failure120*/121int cfpkt_addbdy(struct cfpkt *pkt, const u8 data);122123/*124* Add a data to packet body (tail).125*126* pkt Packet in which to add data.127* data Pointer to data to copy into the packet body.128* len Length of data to add.129* @return zero on success and error code upon failure130*/131int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len);132133/*134* Checks whether there are more data to process in packet.135* pkt Packet to check.136* @return true if more data are available in packet false otherwise137*/138bool cfpkt_more(struct cfpkt *pkt);139140/*141* Checks whether the packet is erroneous,142* i.e. if it has been attempted to extract more data than available in packet143* or writing more data than has been allocated in cfpkt_create().144* pkt Packet to check.145* @return true on error false otherwise146*/147bool cfpkt_erroneous(struct cfpkt *pkt);148149/*150* Get the packet length.151* pkt Packet to get length from.152* @return Number of bytes in packet.153*/154u16 cfpkt_getlen(struct cfpkt *pkt);155156/*157* Set the packet length, by adjusting the trailer pointer according to length.158* pkt Packet to set length.159* len Packet length.160* @return Number of bytes in packet.161*/162int cfpkt_setlen(struct cfpkt *pkt, u16 len);163164/*165* cfpkt_append - Appends a packet's data to another packet.166* dstpkt: Packet to append data into, WILL BE FREED BY THIS FUNCTION167* addpkt: Packet to be appended and automatically released,168* WILL BE FREED BY THIS FUNCTION.169* expectlen: Packet's expected total length. This should be considered170* as a hint.171* NB: Input packets will be destroyed after appending and cannot be used172* after calling this function.173* @return The new appended packet.174*/175struct cfpkt *cfpkt_append(struct cfpkt *dstpkt, struct cfpkt *addpkt,176u16 expectlen);177178/*179* cfpkt_split - Split a packet into two packets at the specified split point.180* pkt: Packet to be split (will contain the first part of the data on exit)181* pos: Position to split packet in two parts.182* @return The new packet, containing the second part of the data.183*/184struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos);185186/*187* Iteration function, iterates the packet buffers from start to end.188*189* Checksum iteration function used to iterate buffers190* (we may have packets consisting of a chain of buffers)191* pkt: Packet to calculate checksum for192* iter_func: Function pointer to iteration function193* chks: Checksum calculated so far.194* buf: Pointer to the buffer to checksum195* len: Length of buf.196* data: Initial checksum value.197* @return Checksum of buffer.198*/199200int cfpkt_iterate(struct cfpkt *pkt,201u16 (*iter_func)(u16 chks, void *buf, u16 len),202u16 data);203204/* Map from a "native" packet (e.g. Linux Socket Buffer) to a CAIF packet.205* dir - Direction indicating whether this packet is to be sent or received.206* nativepkt - The native packet to be transformed to a CAIF packet207* @return The mapped CAIF Packet CFPKT.208*/209struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt);210211/* Map from a CAIF packet to a "native" packet (e.g. Linux Socket Buffer).212* pkt - The CAIF packet to be transformed into a "native" packet.213* @return The native packet transformed from a CAIF packet.214*/215void *cfpkt_tonative(struct cfpkt *pkt);216217/*218* Returns packet information for a packet.219* pkt Packet to get info from;220* @return Packet information221*/222struct caif_payload_info *cfpkt_info(struct cfpkt *pkt);223224/** cfpkt_set_prio - set priority for a CAIF packet.225*226* @pkt: The CAIF packet to be adjusted.227* @prio: one of TC_PRIO_ constants.228*/229void cfpkt_set_prio(struct cfpkt *pkt, int prio);230231#endif /* CFPKT_H_ */232233234