blob: ab5e542526bf4e9e2f65562e56a97ccb96ba85bf [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/errno.h>
12#include <linux/slab.h>
13#include <net/caif/caif_layer.h>
14#include <net/caif/cfsrvl.h>
15#include <net/caif/cfpkt.h>
16
17#define SRVL_CTRL_PKT_SIZE 1
18#define SRVL_FLOW_OFF 0x81
19#define SRVL_FLOW_ON 0x80
20#define SRVL_SET_PIN 0x82
21#define SRVL_CTRL_PKT_SIZE 1
22
23#define container_obj(layr) container_of(layr, struct cfsrvl, layer)
24
25static void cfservl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
26 int phyid)
27{
28 struct cfsrvl *service = container_obj(layr);
Sjur Braendelandb1c74242010-06-17 06:55:38 +000029
Sjur Braendelandb482cd22010-03-30 13:56:23 +000030 caif_assert(layr->up != NULL);
31 caif_assert(layr->up->ctrlcmd != NULL);
Sjur Braendelandb1c74242010-06-17 06:55:38 +000032
Sjur Braendelandb482cd22010-03-30 13:56:23 +000033 switch (ctrl) {
34 case CAIF_CTRLCMD_INIT_RSP:
35 service->open = true;
36 layr->up->ctrlcmd(layr->up, ctrl, phyid);
37 break;
38 case CAIF_CTRLCMD_DEINIT_RSP:
39 case CAIF_CTRLCMD_INIT_FAIL_RSP:
40 service->open = false;
41 layr->up->ctrlcmd(layr->up, ctrl, phyid);
42 break;
43 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
44 if (phyid != service->dev_info.id)
45 break;
46 if (service->modem_flow_on)
47 layr->up->ctrlcmd(layr->up,
48 CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
49 service->phy_flow_on = false;
50 break;
51 case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND:
52 if (phyid != service->dev_info.id)
53 return;
54 if (service->modem_flow_on) {
55 layr->up->ctrlcmd(layr->up,
56 CAIF_CTRLCMD_FLOW_ON_IND,
57 phyid);
58 }
59 service->phy_flow_on = true;
60 break;
61 case CAIF_CTRLCMD_FLOW_OFF_IND:
62 if (service->phy_flow_on) {
63 layr->up->ctrlcmd(layr->up,
64 CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
65 }
66 service->modem_flow_on = false;
67 break;
68 case CAIF_CTRLCMD_FLOW_ON_IND:
69 if (service->phy_flow_on) {
70 layr->up->ctrlcmd(layr->up,
71 CAIF_CTRLCMD_FLOW_ON_IND, phyid);
72 }
73 service->modem_flow_on = true;
74 break;
75 case _CAIF_CTRLCMD_PHYIF_DOWN_IND:
76 /* In case interface is down, let's fake a remove shutdown */
77 layr->up->ctrlcmd(layr->up,
78 CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, phyid);
79 break;
80 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
81 layr->up->ctrlcmd(layr->up, ctrl, phyid);
82 break;
83 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +000084 pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000085 /* We have both modem and phy flow on, send flow on */
86 layr->up->ctrlcmd(layr->up, ctrl, phyid);
87 service->phy_flow_on = true;
88 break;
89 }
90}
91
92static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
93{
94 struct cfsrvl *service = container_obj(layr);
Sjur Braendelandb1c74242010-06-17 06:55:38 +000095
Sjur Braendelandb482cd22010-03-30 13:56:23 +000096 caif_assert(layr != NULL);
97 caif_assert(layr->dn != NULL);
98 caif_assert(layr->dn->transmit != NULL);
Sjur Braendelandb1c74242010-06-17 06:55:38 +000099
100 if (!service->supports_flowctrl)
101 return 0;
102
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000103 switch (ctrl) {
104 case CAIF_MODEMCMD_FLOW_ON_REQ:
105 {
106 struct cfpkt *pkt;
107 struct caif_payload_info *info;
108 u8 flow_on = SRVL_FLOW_ON;
109 pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
110 if (!pkt) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000111 pr_warn("Out of memory\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000112 return -ENOMEM;
113 }
114
115 if (cfpkt_add_head(pkt, &flow_on, 1) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000116 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000117 cfpkt_destroy(pkt);
118 return -EPROTO;
119 }
120 info = cfpkt_info(pkt);
121 info->channel_id = service->layer.id;
122 info->hdr_len = 1;
123 info->dev_info = &service->dev_info;
124 return layr->dn->transmit(layr->dn, pkt);
125 }
126 case CAIF_MODEMCMD_FLOW_OFF_REQ:
127 {
128 struct cfpkt *pkt;
129 struct caif_payload_info *info;
130 u8 flow_off = SRVL_FLOW_OFF;
131 pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
Sjur Braendeland638e6282010-05-21 02:16:09 +0000132 if (!pkt) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000133 pr_warn("Out of memory\n");
Sjur Braendeland638e6282010-05-21 02:16:09 +0000134 return -ENOMEM;
135 }
136
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000137 if (cfpkt_add_head(pkt, &flow_off, 1) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000138 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000139 cfpkt_destroy(pkt);
140 return -EPROTO;
141 }
142 info = cfpkt_info(pkt);
143 info->channel_id = service->layer.id;
144 info->hdr_len = 1;
145 info->dev_info = &service->dev_info;
146 return layr->dn->transmit(layr->dn, pkt);
147 }
148 default:
149 break;
150 }
151 return -EINVAL;
152}
153
154void cfservl_destroy(struct cflayer *layer)
155{
156 kfree(layer);
157}
158
Sjur Braendelanda7da1f52010-06-17 06:55:39 +0000159void cfsrvl_release(struct kref *kref)
160{
161 struct cfsrvl *service = container_of(kref, struct cfsrvl, ref);
Sjur Braendelanda7da1f52010-06-17 06:55:39 +0000162 kfree(service);
163}
164
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000165void cfsrvl_init(struct cfsrvl *service,
Sjur Braendelandb1c74242010-06-17 06:55:38 +0000166 u8 channel_id,
167 struct dev_info *dev_info,
168 bool supports_flowctrl
169 )
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000170{
171 caif_assert(offsetof(struct cfsrvl, layer) == 0);
172 service->open = false;
173 service->modem_flow_on = true;
174 service->phy_flow_on = true;
175 service->layer.id = channel_id;
176 service->layer.ctrlcmd = cfservl_ctrlcmd;
177 service->layer.modemcmd = cfservl_modemcmd;
178 service->dev_info = *dev_info;
Sjur Braendelandb1c74242010-06-17 06:55:38 +0000179 service->supports_flowctrl = supports_flowctrl;
Sjur Braendelanda7da1f52010-06-17 06:55:39 +0000180 service->release = cfsrvl_release;
Sjur Braendeland5b208652010-04-28 08:54:36 +0000181 kref_init(&service->ref);
182}
183
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000184
185bool cfsrvl_ready(struct cfsrvl *service, int *err)
186{
187 if (service->open && service->modem_flow_on && service->phy_flow_on)
188 return true;
189 if (!service->open) {
190 *err = -ENOTCONN;
191 return false;
192 }
193 caif_assert(!(service->modem_flow_on && service->phy_flow_on));
194 *err = -EAGAIN;
195 return false;
196}
197u8 cfsrvl_getphyid(struct cflayer *layer)
198{
199 struct cfsrvl *servl = container_obj(layer);
200 return servl->dev_info.id;
201}
202
203bool cfsrvl_phyid_match(struct cflayer *layer, int phyid)
204{
205 struct cfsrvl *servl = container_obj(layer);
206 return servl->dev_info.id == phyid;
207}