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
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #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>
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
23 #define container_obj(layr) container_of(layr, struct cfsrvl, layer)
25 static void cfservl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
28 struct cfsrvl
*service
= container_obj(layr
);
30 caif_assert(layr
->up
!= NULL
);
31 caif_assert(layr
->up
->ctrlcmd
!= NULL
);
34 case CAIF_CTRLCMD_INIT_RSP
:
36 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
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
);
43 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND
:
44 if (phyid
!= service
->dev_info
.id
)
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;
51 case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND
:
52 if (phyid
!= service
->dev_info
.id
)
54 if (service
->modem_flow_on
) {
55 layr
->up
->ctrlcmd(layr
->up
,
56 CAIF_CTRLCMD_FLOW_ON_IND
,
59 service
->phy_flow_on
= true;
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
);
66 service
->modem_flow_on
= false;
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
);
73 service
->modem_flow_on
= true;
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
);
80 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
81 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
84 pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl
);
85 /* 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;
92 static int cfservl_modemcmd(struct cflayer
*layr
, enum caif_modemcmd ctrl
)
94 struct cfsrvl
*service
= container_obj(layr
);
96 caif_assert(layr
!= NULL
);
97 caif_assert(layr
->dn
!= NULL
);
98 caif_assert(layr
->dn
->transmit
!= NULL
);
100 if (!service
->supports_flowctrl
)
104 case CAIF_MODEMCMD_FLOW_ON_REQ
:
107 struct caif_payload_info
*info
;
108 u8 flow_on
= SRVL_FLOW_ON
;
109 pkt
= cfpkt_create(SRVL_CTRL_PKT_SIZE
);
111 pr_warn("Out of memory\n");
115 if (cfpkt_add_head(pkt
, &flow_on
, 1) < 0) {
116 pr_err("Packet is erroneous!\n");
120 info
= cfpkt_info(pkt
);
121 info
->channel_id
= service
->layer
.id
;
123 info
->dev_info
= &service
->dev_info
;
124 return layr
->dn
->transmit(layr
->dn
, pkt
);
126 case CAIF_MODEMCMD_FLOW_OFF_REQ
:
129 struct caif_payload_info
*info
;
130 u8 flow_off
= SRVL_FLOW_OFF
;
131 pkt
= cfpkt_create(SRVL_CTRL_PKT_SIZE
);
133 pr_warn("Out of memory\n");
137 if (cfpkt_add_head(pkt
, &flow_off
, 1) < 0) {
138 pr_err("Packet is erroneous!\n");
142 info
= cfpkt_info(pkt
);
143 info
->channel_id
= service
->layer
.id
;
145 info
->dev_info
= &service
->dev_info
;
146 return layr
->dn
->transmit(layr
->dn
, pkt
);
154 void cfservl_destroy(struct cflayer
*layer
)
159 void cfsrvl_release(struct kref
*kref
)
161 struct cfsrvl
*service
= container_of(kref
, struct cfsrvl
, ref
);
165 void cfsrvl_init(struct cfsrvl
*service
,
167 struct dev_info
*dev_info
,
168 bool supports_flowctrl
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
;
179 service
->supports_flowctrl
= supports_flowctrl
;
180 service
->release
= cfsrvl_release
;
181 kref_init(&service
->ref
);
185 bool cfsrvl_ready(struct cfsrvl
*service
, int *err
)
187 if (service
->open
&& service
->modem_flow_on
&& service
->phy_flow_on
)
189 if (!service
->open
) {
193 caif_assert(!(service
->modem_flow_on
&& service
->phy_flow_on
));
197 u8
cfsrvl_getphyid(struct cflayer
*layer
)
199 struct cfsrvl
*servl
= container_obj(layer
);
200 return servl
->dev_info
.id
;
203 bool cfsrvl_phyid_match(struct cflayer
*layer
, int phyid
)
205 struct cfsrvl
*servl
= container_obj(layer
);
206 return servl
->dev_info
.id
== phyid
;