blob: 86d2dadb4b737d9c8ec2fae9f5d677e084ccd748 [file] [log] [blame]
Sjur Braendelandb482cd22010-03-30 13:56:23 +00001/*
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
Joe Perchesb31fa5b2010-09-05 21:31:11 +00007#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
Sjur Braendelandb482cd22010-03-30 13:56:23 +00009#include <linux/kernel.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/errno.h>
13#include <net/caif/caif_layer.h>
14#include <net/caif/cfsrvl.h>
15#include <net/caif/cfpkt.h>
16
17#define container_obj(layr) ((struct cfsrvl *) layr)
18#define UTIL_PAYLOAD 0x00
19#define UTIL_CMD_BIT 0x80
20#define UTIL_REMOTE_SHUTDOWN 0x82
21#define UTIL_FLOW_OFF 0x81
22#define UTIL_FLOW_ON 0x80
Shan Wei441c7932011-01-13 22:19:52 +000023
Sjur Braendelandb482cd22010-03-30 13:56:23 +000024static int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt);
25static int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt);
26
27struct cflayer *cfutill_create(u8 channel_id, struct dev_info *dev_info)
28{
Joe Perches7ac2ed02011-08-25 13:22:24 +000029 struct cfsrvl *util = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
30 if (!util)
Sjur Braendelandb482cd22010-03-30 13:56:23 +000031 return NULL;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000032 caif_assert(offsetof(struct cfsrvl, layer) == 0);
Sjur Braendelandb1c74242010-06-17 06:55:38 +000033 cfsrvl_init(util, channel_id, dev_info, true);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000034 util->layer.receive = cfutill_receive;
35 util->layer.transmit = cfutill_transmit;
36 snprintf(util->layer.name, CAIF_LAYER_NAME_SZ - 1, "util1");
37 return &util->layer;
38}
39
40static int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt)
41{
42 u8 cmd = -1;
43 struct cfsrvl *service = container_obj(layr);
44 caif_assert(layr != NULL);
45 caif_assert(layr->up != NULL);
46 caif_assert(layr->up->receive != NULL);
47 caif_assert(layr->up->ctrlcmd != NULL);
48 if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +000049 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000050 cfpkt_destroy(pkt);
51 return -EPROTO;
52 }
53
54 switch (cmd) {
55 case UTIL_PAYLOAD:
56 return layr->up->receive(layr->up, pkt);
57 case UTIL_FLOW_OFF:
58 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
59 cfpkt_destroy(pkt);
60 return 0;
61 case UTIL_FLOW_ON:
62 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
63 cfpkt_destroy(pkt);
64 return 0;
65 case UTIL_REMOTE_SHUTDOWN: /* Remote Shutdown Request */
Joe Perchesb31fa5b2010-09-05 21:31:11 +000066 pr_err("REMOTE SHUTDOWN REQUEST RECEIVED\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000067 layr->ctrlcmd(layr, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, 0);
68 service->open = false;
69 cfpkt_destroy(pkt);
70 return 0;
71 default:
72 cfpkt_destroy(pkt);
Joe Perchesb31fa5b2010-09-05 21:31:11 +000073 pr_warn("Unknown service control %d (0x%x)\n", cmd, cmd);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000074 return -EPROTO;
75 }
76}
77
78static int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt)
79{
80 u8 zero = 0;
81 struct caif_payload_info *info;
82 int ret;
83 struct cfsrvl *service = container_obj(layr);
84 caif_assert(layr != NULL);
85 caif_assert(layr->dn != NULL);
86 caif_assert(layr->dn->transmit != NULL);
Dmitry Tarnyagin374458b2012-03-11 10:28:31 +000087
88 if (!cfsrvl_ready(service, &ret)) {
89 cfpkt_destroy(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000090 return ret;
Dmitry Tarnyagin374458b2012-03-11 10:28:31 +000091 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +000092
Sjur Braendelandb482cd22010-03-30 13:56:23 +000093 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;
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000103 return layr->dn->transmit(layr->dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000104}