^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) ST-Ericsson AB 2010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Sjur Brendeland
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/caif/caif_layer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/caif/cfpkt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/caif/cfserl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define container_obj(layr) ((struct cfserl *) layr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define CFSERL_STX 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define SERIAL_MINIUM_PACKET_SIZE 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define SERIAL_MAX_FRAMESIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct cfserl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct cflayer layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct cfpkt *incomplete_frm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* Protects parallel processing of incoming packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) spinlock_t sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) bool usestx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int cfserl_receive(struct cflayer *layr, struct cfpkt *pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int cfserl_transmit(struct cflayer *layr, struct cfpkt *pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int phyid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void cfserl_release(struct cflayer *layer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) kfree(layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct cflayer *cfserl_create(int instance, bool use_stx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct cfserl *this = kzalloc(sizeof(struct cfserl), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) caif_assert(offsetof(struct cfserl, layer) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) this->layer.receive = cfserl_receive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) this->layer.transmit = cfserl_transmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) this->layer.ctrlcmd = cfserl_ctrlcmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) this->usestx = use_stx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) spin_lock_init(&this->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "ser1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return &this->layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int cfserl_receive(struct cflayer *l, struct cfpkt *newpkt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct cfserl *layr = container_obj(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u16 pkt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct cfpkt *pkt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct cfpkt *tail_pkt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) u8 tmp8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u16 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u8 stx = CFSERL_STX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) u16 expectlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) caif_assert(newpkt != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) spin_lock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (layr->incomplete_frm != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) layr->incomplete_frm =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) cfpkt_append(layr->incomplete_frm, newpkt, expectlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) pkt = layr->incomplete_frm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (pkt == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) spin_unlock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pkt = newpkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) layr->incomplete_frm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Search for STX at start of pkt if STX is used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (layr->usestx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) cfpkt_extr_head(pkt, &tmp8, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (tmp8 != CFSERL_STX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) while (cfpkt_more(pkt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) && tmp8 != CFSERL_STX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) cfpkt_extr_head(pkt, &tmp8, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!cfpkt_more(pkt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cfpkt_destroy(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) layr->incomplete_frm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) spin_unlock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pkt_len = cfpkt_getlen(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * pkt_len is the accumulated length of the packet data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * we have received so far.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Exit if frame doesn't hold length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (pkt_len < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (layr->usestx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) cfpkt_add_head(pkt, &stx, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) layr->incomplete_frm = pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) spin_unlock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Find length of frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * expectlen is the length we need for a full frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) cfpkt_peek_head(pkt, &tmp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) expectlen = le16_to_cpu(tmp) + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Frame error handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (expectlen < SERIAL_MINIUM_PACKET_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) || expectlen > SERIAL_MAX_FRAMESIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!layr->usestx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (pkt != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) cfpkt_destroy(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) layr->incomplete_frm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) expectlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) spin_unlock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (pkt_len < expectlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Too little received data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (layr->usestx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) cfpkt_add_head(pkt, &stx, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) layr->incomplete_frm = pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) spin_unlock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Enough data for at least one frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Split the frame, if too long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (pkt_len > expectlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) tail_pkt = cfpkt_split(pkt, expectlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) tail_pkt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Send the first part of packet upwards.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) spin_unlock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = layr->layer.up->receive(layr->layer.up, pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) spin_lock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (ret == -EILSEQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (layr->usestx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (tail_pkt != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) pkt = cfpkt_append(pkt, tail_pkt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Start search for next STX if frame failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) cfpkt_destroy(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) pkt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pkt = tail_pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) } while (pkt != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) spin_unlock(&layr->sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int cfserl_transmit(struct cflayer *layer, struct cfpkt *newpkt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct cfserl *layr = container_obj(layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u8 tmp8 = CFSERL_STX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (layr->usestx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) cfpkt_add_head(newpkt, &tmp8, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return layer->dn->transmit(layer->dn, newpkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int phyid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) layr->up->ctrlcmd(layr->up, ctrl, phyid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }