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 #include <linux/stddef.h>
8 #include <linux/spinlock.h>
9 #include <linux/slab.h>
10 #include <net/caif/caif_layer.h>
11 #include <net/caif/cfpkt.h>
12 #include <net/caif/cfserl.h>
14 #define container_obj(layr) ((struct cfserl *) layr)
16 #define CFSERL_STX 0x02
17 #define CAIF_MINIUM_PACKET_SIZE 4
20 struct cfpkt
*incomplete_frm
;
21 /* Protects parallel processing of incoming packets */
25 #define STXLEN(layr) (layr->usestx ? 1 : 0)
27 static int cfserl_receive(struct cflayer
*layr
, struct cfpkt
*pkt
);
28 static int cfserl_transmit(struct cflayer
*layr
, struct cfpkt
*pkt
);
29 static void cfserl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
32 struct cflayer
*cfserl_create(int type
, int instance
, bool use_stx
)
34 struct cfserl
*this = kmalloc(sizeof(struct cfserl
), GFP_ATOMIC
);
36 pr_warning("CAIF: %s(): Out of memory\n", __func__
);
39 caif_assert(offsetof(struct cfserl
, layer
) == 0);
40 memset(this, 0, sizeof(struct cfserl
));
41 this->layer
.receive
= cfserl_receive
;
42 this->layer
.transmit
= cfserl_transmit
;
43 this->layer
.ctrlcmd
= cfserl_ctrlcmd
;
44 this->layer
.type
= type
;
45 this->usestx
= use_stx
;
46 spin_lock_init(&this->sync
);
47 snprintf(this->layer
.name
, CAIF_LAYER_NAME_SZ
, "ser1");
51 static int cfserl_receive(struct cflayer
*l
, struct cfpkt
*newpkt
)
53 struct cfserl
*layr
= container_obj(l
);
55 struct cfpkt
*pkt
= NULL
;
56 struct cfpkt
*tail_pkt
= NULL
;
63 caif_assert(newpkt
!= NULL
);
64 spin_lock(&layr
->sync
);
66 if (layr
->incomplete_frm
!= NULL
) {
67 layr
->incomplete_frm
=
68 cfpkt_append(layr
->incomplete_frm
, newpkt
, expectlen
);
69 pkt
= layr
->incomplete_frm
;
71 spin_unlock(&layr
->sync
);
77 layr
->incomplete_frm
= NULL
;
80 /* Search for STX at start of pkt if STX is used */
82 cfpkt_extr_head(pkt
, &tmp8
, 1);
83 if (tmp8
!= CFSERL_STX
) {
84 while (cfpkt_more(pkt
)
85 && tmp8
!= CFSERL_STX
) {
86 cfpkt_extr_head(pkt
, &tmp8
, 1);
88 if (!cfpkt_more(pkt
)) {
90 layr
->incomplete_frm
= NULL
;
91 spin_unlock(&layr
->sync
);
97 pkt_len
= cfpkt_getlen(pkt
);
100 * pkt_len is the accumulated length of the packet data
101 * we have received so far.
102 * Exit if frame doesn't hold length.
107 cfpkt_add_head(pkt
, &stx
, 1);
108 layr
->incomplete_frm
= pkt
;
109 spin_unlock(&layr
->sync
);
114 * Find length of frame.
115 * expectlen is the length we need for a full frame.
117 cfpkt_peek_head(pkt
, &tmp
, 2);
118 expectlen
= le16_to_cpu(tmp
) + 2;
120 * Frame error handling
122 if (expectlen
< CAIF_MINIUM_PACKET_SIZE
123 || expectlen
> CAIF_MAX_FRAMESIZE
) {
127 layr
->incomplete_frm
= NULL
;
129 spin_unlock(&layr
->sync
);
135 if (pkt_len
< expectlen
) {
136 /* Too little received data */
138 cfpkt_add_head(pkt
, &stx
, 1);
139 layr
->incomplete_frm
= pkt
;
140 spin_unlock(&layr
->sync
);
145 * Enough data for at least one frame.
146 * Split the frame, if too long
148 if (pkt_len
> expectlen
)
149 tail_pkt
= cfpkt_split(pkt
, expectlen
);
153 /* Send the first part of packet upwards.*/
154 spin_unlock(&layr
->sync
);
155 ret
= layr
->layer
.up
->receive(layr
->layer
.up
, pkt
);
156 spin_lock(&layr
->sync
);
157 if (ret
== -EILSEQ
) {
159 if (tail_pkt
!= NULL
)
160 pkt
= cfpkt_append(pkt
, tail_pkt
, 0);
161 /* Start search for next STX if frame failed */
171 } while (pkt
!= NULL
);
173 spin_unlock(&layr
->sync
);
177 static int cfserl_transmit(struct cflayer
*layer
, struct cfpkt
*newpkt
)
179 struct cfserl
*layr
= container_obj(layer
);
181 u8 tmp8
= CFSERL_STX
;
183 cfpkt_add_head(newpkt
, &tmp8
, 1);
184 ret
= layer
->dn
->transmit(layer
->dn
, newpkt
);
186 cfpkt_extr_head(newpkt
, &tmp8
, 1);
191 static void cfserl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
194 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);