2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #include <linux/stddef.h>
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <net/caif/caif_layer.h>
13 #include <net/caif/cfpkt.h>
14 #include <net/caif/cfserl.h>
16 #define container_obj(layr) ((struct cfserl *) layr)
18 #define CFSERL_STX 0x02
19 #define SERIAL_MINIUM_PACKET_SIZE 4
20 #define SERIAL_MAX_FRAMESIZE 4096
23 struct cfpkt
*incomplete_frm
;
24 /* Protects parallel processing of incoming packets */
28 #define STXLEN(layr) (layr->usestx ? 1 : 0)
30 static int cfserl_receive(struct cflayer
*layr
, struct cfpkt
*pkt
);
31 static int cfserl_transmit(struct cflayer
*layr
, struct cfpkt
*pkt
);
32 static void cfserl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
35 struct cflayer
*cfserl_create(int type
, int instance
, bool use_stx
)
37 struct cfserl
*this = kmalloc(sizeof(struct cfserl
), GFP_ATOMIC
);
39 pr_warn("Out of memory\n");
42 caif_assert(offsetof(struct cfserl
, layer
) == 0);
43 memset(this, 0, sizeof(struct cfserl
));
44 this->layer
.receive
= cfserl_receive
;
45 this->layer
.transmit
= cfserl_transmit
;
46 this->layer
.ctrlcmd
= cfserl_ctrlcmd
;
47 this->layer
.type
= type
;
48 this->usestx
= use_stx
;
49 spin_lock_init(&this->sync
);
50 snprintf(this->layer
.name
, CAIF_LAYER_NAME_SZ
, "ser1");
54 static int cfserl_receive(struct cflayer
*l
, struct cfpkt
*newpkt
)
56 struct cfserl
*layr
= container_obj(l
);
58 struct cfpkt
*pkt
= NULL
;
59 struct cfpkt
*tail_pkt
= NULL
;
66 caif_assert(newpkt
!= NULL
);
67 spin_lock(&layr
->sync
);
69 if (layr
->incomplete_frm
!= NULL
) {
70 layr
->incomplete_frm
=
71 cfpkt_append(layr
->incomplete_frm
, newpkt
, expectlen
);
72 pkt
= layr
->incomplete_frm
;
74 spin_unlock(&layr
->sync
);
80 layr
->incomplete_frm
= NULL
;
83 /* Search for STX at start of pkt if STX is used */
85 cfpkt_extr_head(pkt
, &tmp8
, 1);
86 if (tmp8
!= CFSERL_STX
) {
87 while (cfpkt_more(pkt
)
88 && tmp8
!= CFSERL_STX
) {
89 cfpkt_extr_head(pkt
, &tmp8
, 1);
91 if (!cfpkt_more(pkt
)) {
93 layr
->incomplete_frm
= NULL
;
94 spin_unlock(&layr
->sync
);
100 pkt_len
= cfpkt_getlen(pkt
);
103 * pkt_len is the accumulated length of the packet data
104 * we have received so far.
105 * Exit if frame doesn't hold length.
110 cfpkt_add_head(pkt
, &stx
, 1);
111 layr
->incomplete_frm
= pkt
;
112 spin_unlock(&layr
->sync
);
117 * Find length of frame.
118 * expectlen is the length we need for a full frame.
120 cfpkt_peek_head(pkt
, &tmp
, 2);
121 expectlen
= le16_to_cpu(tmp
) + 2;
123 * Frame error handling
125 if (expectlen
< SERIAL_MINIUM_PACKET_SIZE
126 || expectlen
> SERIAL_MAX_FRAMESIZE
) {
130 layr
->incomplete_frm
= NULL
;
132 spin_unlock(&layr
->sync
);
138 if (pkt_len
< expectlen
) {
139 /* Too little received data */
141 cfpkt_add_head(pkt
, &stx
, 1);
142 layr
->incomplete_frm
= pkt
;
143 spin_unlock(&layr
->sync
);
148 * Enough data for at least one frame.
149 * Split the frame, if too long
151 if (pkt_len
> expectlen
)
152 tail_pkt
= cfpkt_split(pkt
, expectlen
);
156 /* Send the first part of packet upwards.*/
157 spin_unlock(&layr
->sync
);
158 ret
= layr
->layer
.up
->receive(layr
->layer
.up
, pkt
);
159 spin_lock(&layr
->sync
);
160 if (ret
== -EILSEQ
) {
162 if (tail_pkt
!= NULL
)
163 pkt
= cfpkt_append(pkt
, tail_pkt
, 0);
164 /* Start search for next STX if frame failed */
174 } while (pkt
!= NULL
);
176 spin_unlock(&layr
->sync
);
180 static int cfserl_transmit(struct cflayer
*layer
, struct cfpkt
*newpkt
)
182 struct cfserl
*layr
= container_obj(layer
);
184 u8 tmp8
= CFSERL_STX
;
186 cfpkt_add_head(newpkt
, &tmp8
, 1);
187 ret
= layer
->dn
->transmit(layer
->dn
, newpkt
);
189 cfpkt_extr_head(newpkt
, &tmp8
, 1);
194 static void cfserl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
197 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);