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/kernel.h>
8 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <net/caif/caif_layer.h>
12 #include <net/caif/cfsrvl.h>
13 #include <net/caif/cfpkt.h>
15 #define container_obj(layr) ((struct cfsrvl *) layr)
16 #define UTIL_PAYLOAD 0x00
17 #define UTIL_CMD_BIT 0x80
18 #define UTIL_REMOTE_SHUTDOWN 0x82
19 #define UTIL_FLOW_OFF 0x81
20 #define UTIL_FLOW_ON 0x80
21 #define UTIL_CTRL_PKT_SIZE 1
22 static int cfutill_receive(struct cflayer
*layr
, struct cfpkt
*pkt
);
23 static int cfutill_transmit(struct cflayer
*layr
, struct cfpkt
*pkt
);
25 struct cflayer
*cfutill_create(u8 channel_id
, struct dev_info
*dev_info
)
27 struct cfsrvl
*util
= kmalloc(sizeof(struct cfsrvl
), GFP_ATOMIC
);
29 pr_warning("CAIF: %s(): Out of memory\n", __func__
);
32 caif_assert(offsetof(struct cfsrvl
, layer
) == 0);
33 memset(util
, 0, sizeof(struct cfsrvl
));
34 cfsrvl_init(util
, channel_id
, dev_info
);
35 util
->layer
.receive
= cfutill_receive
;
36 util
->layer
.transmit
= cfutill_transmit
;
37 snprintf(util
->layer
.name
, CAIF_LAYER_NAME_SZ
- 1, "util1");
41 static int cfutill_receive(struct cflayer
*layr
, struct cfpkt
*pkt
)
44 struct cfsrvl
*service
= container_obj(layr
);
45 caif_assert(layr
!= NULL
);
46 caif_assert(layr
->up
!= NULL
);
47 caif_assert(layr
->up
->receive
!= NULL
);
48 caif_assert(layr
->up
->ctrlcmd
!= NULL
);
49 if (cfpkt_extr_head(pkt
, &cmd
, 1) < 0) {
50 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__
);
57 return layr
->up
->receive(layr
->up
, pkt
);
59 layr
->ctrlcmd(layr
, CAIF_CTRLCMD_FLOW_OFF_IND
, 0);
63 layr
->ctrlcmd(layr
, CAIF_CTRLCMD_FLOW_ON_IND
, 0);
66 case UTIL_REMOTE_SHUTDOWN
: /* Remote Shutdown Request */
67 pr_err("CAIF: %s(): REMOTE SHUTDOWN REQUEST RECEIVED\n",
69 layr
->ctrlcmd(layr
, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
, 0);
70 service
->open
= false;
75 pr_warning("CAIF: %s(): Unknown service control %d (0x%x)\n",
81 static int cfutill_transmit(struct cflayer
*layr
, struct cfpkt
*pkt
)
84 struct caif_payload_info
*info
;
86 struct cfsrvl
*service
= container_obj(layr
);
87 caif_assert(layr
!= NULL
);
88 caif_assert(layr
->dn
!= NULL
);
89 caif_assert(layr
->dn
->transmit
!= NULL
);
90 if (!cfsrvl_ready(service
, &ret
))
93 if (cfpkt_getlen(pkt
) > CAIF_MAX_PAYLOAD_SIZE
) {
94 pr_err("CAIF: %s(): packet too large size=%d\n",
95 __func__
, cfpkt_getlen(pkt
));
99 cfpkt_add_head(pkt
, &zero
, 1);
100 /* Add info for MUX-layer to route the packet out. */
101 info
= cfpkt_info(pkt
);
102 info
->channel_id
= service
->layer
.id
;
104 * To optimize alignment, we add up the size of CAIF header before
108 info
->dev_info
= &service
->dev_info
;
109 ret
= layr
->dn
->transmit(layr
->dn
, pkt
);
112 cfpkt_extr_head(pkt
, &tmp32
, 4);