blob: 3bdddb32d55a7a1ce6c37e7293dbc5461dc98aa4 [file] [log] [blame]
Sjur Braendelandb482cd22010-03-30 13:56:23 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
sjur.brandeland@stericsson.com26ee65e2013-04-22 23:57:01 +00003 * Author: Sjur Brendeland
Sjur Braendelandb482cd22010-03-30 13:56:23 +00004 * 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/stddef.h>
10#include <linux/spinlock.h>
11#include <linux/slab.h>
12#include <net/caif/caif_layer.h>
13#include <net/caif/cfsrvl.h>
14#include <net/caif/cfpkt.h>
15
Sjur Brændeland4a9f65f2011-04-11 10:11:30 +000016
Sjur Braendelandb482cd22010-03-30 13:56:23 +000017#define container_obj(layr) ((struct cfsrvl *) layr)
18
19#define DGM_CMD_BIT 0x80
20#define DGM_FLOW_OFF 0x81
21#define DGM_FLOW_ON 0x80
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000022#define DGM_MTU 1500
Sjur Braendelandb482cd22010-03-30 13:56:23 +000023
24static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt);
25static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt);
26
27struct cflayer *cfdgml_create(u8 channel_id, struct dev_info *dev_info)
28{
Joe Perches7ac2ed02011-08-25 13:22:24 +000029 struct cfsrvl *dgm = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
30 if (!dgm)
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(dgm, channel_id, dev_info, true);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000034 dgm->layer.receive = cfdgml_receive;
35 dgm->layer.transmit = cfdgml_transmit;
36 snprintf(dgm->layer.name, CAIF_LAYER_NAME_SZ - 1, "dgm%d", channel_id);
37 dgm->layer.name[CAIF_LAYER_NAME_SZ - 1] = '\0';
38 return &dgm->layer;
39}
40
41static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt)
42{
43 u8 cmd = -1;
44 u8 dgmhdr[3];
45 int ret;
46 caif_assert(layr->up != NULL);
47 caif_assert(layr->receive != NULL);
48 caif_assert(layr->ctrlcmd != NULL);
49
50 if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +000051 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000052 cfpkt_destroy(pkt);
53 return -EPROTO;
54 }
55
56 if ((cmd & DGM_CMD_BIT) == 0) {
57 if (cfpkt_extr_head(pkt, &dgmhdr, 3) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +000058 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000059 cfpkt_destroy(pkt);
60 return -EPROTO;
61 }
62 ret = layr->up->receive(layr->up, pkt);
63 return ret;
64 }
65
66 switch (cmd) {
67 case DGM_FLOW_OFF: /* FLOW OFF */
68 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
69 cfpkt_destroy(pkt);
70 return 0;
71 case DGM_FLOW_ON: /* FLOW ON */
72 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
73 cfpkt_destroy(pkt);
74 return 0;
75 default:
76 cfpkt_destroy(pkt);
Joe Perchesb31fa5b2010-09-05 21:31:11 +000077 pr_info("Unknown datagram control %d (0x%x)\n", cmd, cmd);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000078 return -EPROTO;
79 }
80}
81
82static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt)
83{
Sjur Brændeland4a9f65f2011-04-11 10:11:30 +000084 u8 packet_type;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000085 u32 zero = 0;
86 struct caif_payload_info *info;
87 struct cfsrvl *service = container_obj(layr);
88 int ret;
Dmitry Tarnyagin374458b2012-03-11 10:28:31 +000089
90 if (!cfsrvl_ready(service, &ret)) {
91 cfpkt_destroy(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000092 return ret;
Dmitry Tarnyagin374458b2012-03-11 10:28:31 +000093 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +000094
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000095 /* STE Modem cannot handle more than 1500 bytes datagrams */
Dmitry Tarnyagin374458b2012-03-11 10:28:31 +000096 if (cfpkt_getlen(pkt) > DGM_MTU) {
97 cfpkt_destroy(pkt);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000098 return -EMSGSIZE;
Dmitry Tarnyagin374458b2012-03-11 10:28:31 +000099 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000100
Sjur Brændeland4a9f65f2011-04-11 10:11:30 +0000101 cfpkt_add_head(pkt, &zero, 3);
102 packet_type = 0x08; /* B9 set - UNCLASSIFIED */
103 cfpkt_add_head(pkt, &packet_type, 1);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000104
105 /* Add info for MUX-layer to route the packet out. */
106 info = cfpkt_info(pkt);
107 info->channel_id = service->layer.id;
108 /* To optimize alignment, we add up the size of CAIF header
109 * before payload.
110 */
111 info->hdr_len = 4;
112 info->dev_info = &service->dev_info;
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000113 return layr->dn->transmit(layr->dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000114}