02795aff57a426fbbd1080214f46c5a1fb569d34
[linux-2.6.git] / net / caif / cfutill.c
1 /*
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
5  */
6
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>
14
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);
24
25 struct cflayer *cfutill_create(u8 channel_id, struct dev_info *dev_info)
26 {
27         struct cfsrvl *util = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
28         if (!util) {
29                 pr_warning("CAIF: %s(): Out of memory\n", __func__);
30                 return NULL;
31         }
32         caif_assert(offsetof(struct cfsrvl, layer) == 0);
33         memset(util, 0, sizeof(struct cfsrvl));
34         cfsrvl_init(util, channel_id, dev_info, true);
35         util->layer.receive = cfutill_receive;
36         util->layer.transmit = cfutill_transmit;
37         snprintf(util->layer.name, CAIF_LAYER_NAME_SZ - 1, "util1");
38         return &util->layer;
39 }
40
41 static int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt)
42 {
43         u8 cmd = -1;
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__);
51                 cfpkt_destroy(pkt);
52                 return -EPROTO;
53         }
54
55         switch (cmd) {
56         case UTIL_PAYLOAD:
57                 return layr->up->receive(layr->up, pkt);
58         case UTIL_FLOW_OFF:
59                 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
60                 cfpkt_destroy(pkt);
61                 return 0;
62         case UTIL_FLOW_ON:
63                 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
64                 cfpkt_destroy(pkt);
65                 return 0;
66         case UTIL_REMOTE_SHUTDOWN:      /* Remote Shutdown Request */
67                 pr_err("CAIF: %s(): REMOTE SHUTDOWN REQUEST RECEIVED\n",
68                         __func__);
69                 layr->ctrlcmd(layr, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, 0);
70                 service->open = false;
71                 cfpkt_destroy(pkt);
72                 return 0;
73         default:
74                 cfpkt_destroy(pkt);
75                 pr_warning("CAIF: %s(): Unknown service control %d (0x%x)\n",
76                            __func__, cmd, cmd);
77                 return -EPROTO;
78         }
79 }
80
81 static int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt)
82 {
83         u8 zero = 0;
84         struct caif_payload_info *info;
85         int ret;
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))
91                 return ret;
92
93         cfpkt_add_head(pkt, &zero, 1);
94         /* Add info for MUX-layer to route the packet out. */
95         info = cfpkt_info(pkt);
96         info->channel_id = service->layer.id;
97         /*
98          * To optimize alignment, we add up the size of CAIF header before
99          * payload.
100          */
101         info->hdr_len = 1;
102         info->dev_info = &service->dev_info;
103         ret = layr->dn->transmit(layr->dn, pkt);
104         if (ret < 0) {
105                 u32 tmp32;
106                 cfpkt_extr_head(pkt, &tmp32, 4);
107         }
108         return ret;
109 }