blob: d3ed264ad6c42b932d84a721b270f88d0d6b574b [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/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
16#define container_obj(layr) ((struct cfsrvl *) layr)
17
18#define DGM_CMD_BIT 0x80
19#define DGM_FLOW_OFF 0x81
20#define DGM_FLOW_ON 0x80
21#define DGM_CTRL_PKT_SIZE 1
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{
29 struct cfsrvl *dgm = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
30 if (!dgm) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +000031 pr_warn("Out of memory\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000032 return NULL;
33 }
34 caif_assert(offsetof(struct cfsrvl, layer) == 0);
35 memset(dgm, 0, sizeof(struct cfsrvl));
Sjur Braendelandb1c74242010-06-17 06:55:38 +000036 cfsrvl_init(dgm, channel_id, dev_info, true);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000037 dgm->layer.receive = cfdgml_receive;
38 dgm->layer.transmit = cfdgml_transmit;
39 snprintf(dgm->layer.name, CAIF_LAYER_NAME_SZ - 1, "dgm%d", channel_id);
40 dgm->layer.name[CAIF_LAYER_NAME_SZ - 1] = '\0';
41 return &dgm->layer;
42}
43
44static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt)
45{
46 u8 cmd = -1;
47 u8 dgmhdr[3];
48 int ret;
49 caif_assert(layr->up != NULL);
50 caif_assert(layr->receive != NULL);
51 caif_assert(layr->ctrlcmd != NULL);
52
53 if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +000054 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000055 cfpkt_destroy(pkt);
56 return -EPROTO;
57 }
58
59 if ((cmd & DGM_CMD_BIT) == 0) {
60 if (cfpkt_extr_head(pkt, &dgmhdr, 3) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +000061 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000062 cfpkt_destroy(pkt);
63 return -EPROTO;
64 }
65 ret = layr->up->receive(layr->up, pkt);
66 return ret;
67 }
68
69 switch (cmd) {
70 case DGM_FLOW_OFF: /* FLOW OFF */
71 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
72 cfpkt_destroy(pkt);
73 return 0;
74 case DGM_FLOW_ON: /* FLOW ON */
75 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
76 cfpkt_destroy(pkt);
77 return 0;
78 default:
79 cfpkt_destroy(pkt);
Joe Perchesb31fa5b2010-09-05 21:31:11 +000080 pr_info("Unknown datagram control %d (0x%x)\n", cmd, cmd);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000081 return -EPROTO;
82 }
83}
84
85static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt)
86{
87 u32 zero = 0;
88 struct caif_payload_info *info;
89 struct cfsrvl *service = container_obj(layr);
90 int ret;
91 if (!cfsrvl_ready(service, &ret))
92 return ret;
93
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000094 /* STE Modem cannot handle more than 1500 bytes datagrams */
95 if (cfpkt_getlen(pkt) > DGM_MTU)
96 return -EMSGSIZE;
97
Sjur Braendelandb482cd22010-03-30 13:56:23 +000098 cfpkt_add_head(pkt, &zero, 4);
99
100 /* Add info for MUX-layer to route the packet out. */
101 info = cfpkt_info(pkt);
102 info->channel_id = service->layer.id;
103 /* To optimize alignment, we add up the size of CAIF header
104 * before payload.
105 */
106 info->hdr_len = 4;
107 info->dev_info = &service->dev_info;
108 ret = layr->dn->transmit(layr->dn, pkt);
109 if (ret < 0) {
110 u32 tmp32;
111 cfpkt_extr_head(pkt, &tmp32, 4);
112 }
113 return ret;
114}