2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland
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 <linux/module.h>
14 #include <linux/pkt_sched.h>
15 #include <net/caif/caif_layer.h>
16 #include <net/caif/cfsrvl.h>
17 #include <net/caif/cfpkt.h>
18 #include <net/caif/caif_dev.h>
20 #define SRVL_CTRL_PKT_SIZE 1
21 #define SRVL_FLOW_OFF 0x81
22 #define SRVL_FLOW_ON 0x80
23 #define SRVL_SET_PIN 0x82
24 #define SRVL_CTRL_PKT_SIZE 1
26 #define container_obj(layr) container_of(layr, struct cfsrvl, layer)
28 static void cfservl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
31 struct cfsrvl
*service
= container_obj(layr
);
33 if (layr
->up
== NULL
|| layr
->up
->ctrlcmd
== NULL
)
37 case CAIF_CTRLCMD_INIT_RSP
:
39 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
41 case CAIF_CTRLCMD_DEINIT_RSP
:
42 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
43 service
->open
= false;
44 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
46 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND
:
47 if (phyid
!= service
->dev_info
.id
)
49 if (service
->modem_flow_on
)
50 layr
->up
->ctrlcmd(layr
->up
,
51 CAIF_CTRLCMD_FLOW_OFF_IND
, phyid
);
52 service
->phy_flow_on
= false;
54 case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND
:
55 if (phyid
!= service
->dev_info
.id
)
57 if (service
->modem_flow_on
) {
58 layr
->up
->ctrlcmd(layr
->up
,
59 CAIF_CTRLCMD_FLOW_ON_IND
,
62 service
->phy_flow_on
= true;
64 case CAIF_CTRLCMD_FLOW_OFF_IND
:
65 if (service
->phy_flow_on
) {
66 layr
->up
->ctrlcmd(layr
->up
,
67 CAIF_CTRLCMD_FLOW_OFF_IND
, phyid
);
69 service
->modem_flow_on
= false;
71 case CAIF_CTRLCMD_FLOW_ON_IND
:
72 if (service
->phy_flow_on
) {
73 layr
->up
->ctrlcmd(layr
->up
,
74 CAIF_CTRLCMD_FLOW_ON_IND
, phyid
);
76 service
->modem_flow_on
= true;
78 case _CAIF_CTRLCMD_PHYIF_DOWN_IND
:
79 /* In case interface is down, let's fake a remove shutdown */
80 layr
->up
->ctrlcmd(layr
->up
,
81 CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
, phyid
);
83 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
84 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
87 pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl
);
88 /* We have both modem and phy flow on, send flow on */
89 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
90 service
->phy_flow_on
= true;
95 static int cfservl_modemcmd(struct cflayer
*layr
, enum caif_modemcmd ctrl
)
97 struct cfsrvl
*service
= container_obj(layr
);
99 caif_assert(layr
!= NULL
);
100 caif_assert(layr
->dn
!= NULL
);
101 caif_assert(layr
->dn
->transmit
!= NULL
);
103 if (!service
->supports_flowctrl
)
107 case CAIF_MODEMCMD_FLOW_ON_REQ
:
110 struct caif_payload_info
*info
;
111 u8 flow_on
= SRVL_FLOW_ON
;
112 pkt
= cfpkt_create(SRVL_CTRL_PKT_SIZE
);
116 if (cfpkt_add_head(pkt
, &flow_on
, 1) < 0) {
117 pr_err("Packet is erroneous!\n");
121 info
= cfpkt_info(pkt
);
122 info
->channel_id
= service
->layer
.id
;
124 info
->dev_info
= &service
->dev_info
;
125 cfpkt_set_prio(pkt
, TC_PRIO_CONTROL
);
126 return layr
->dn
->transmit(layr
->dn
, pkt
);
128 case CAIF_MODEMCMD_FLOW_OFF_REQ
:
131 struct caif_payload_info
*info
;
132 u8 flow_off
= SRVL_FLOW_OFF
;
133 pkt
= cfpkt_create(SRVL_CTRL_PKT_SIZE
);
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 cfpkt_set_prio(pkt
, TC_PRIO_CONTROL
);
147 return layr
->dn
->transmit(layr
->dn
, pkt
);
155 static void cfsrvl_release(struct cflayer
*layer
)
157 struct cfsrvl
*service
= container_of(layer
, struct cfsrvl
, layer
);
161 void cfsrvl_init(struct cfsrvl
*service
,
163 struct dev_info
*dev_info
,
164 bool supports_flowctrl
)
166 caif_assert(offsetof(struct cfsrvl
, layer
) == 0);
167 service
->open
= false;
168 service
->modem_flow_on
= true;
169 service
->phy_flow_on
= true;
170 service
->layer
.id
= channel_id
;
171 service
->layer
.ctrlcmd
= cfservl_ctrlcmd
;
172 service
->layer
.modemcmd
= cfservl_modemcmd
;
173 service
->dev_info
= *dev_info
;
174 service
->supports_flowctrl
= supports_flowctrl
;
175 service
->release
= cfsrvl_release
;
178 bool cfsrvl_ready(struct cfsrvl
*service
, int *err
)
180 if (!service
->open
) {
187 u8
cfsrvl_getphyid(struct cflayer
*layer
)
189 struct cfsrvl
*servl
= container_obj(layer
);
190 return servl
->dev_info
.id
;
193 bool cfsrvl_phyid_match(struct cflayer
*layer
, int phyid
)
195 struct cfsrvl
*servl
= container_obj(layer
);
196 return servl
->dev_info
.id
== phyid
;
199 void caif_free_client(struct cflayer
*adap_layer
)
201 struct cfsrvl
*servl
;
202 if (adap_layer
== NULL
|| adap_layer
->dn
== NULL
)
204 servl
= container_obj(adap_layer
->dn
);
205 servl
->release(&servl
->layer
);
207 EXPORT_SYMBOL(caif_free_client
);
209 void caif_client_register_refcnt(struct cflayer
*adapt_layer
,
210 void (*hold
)(struct cflayer
*lyr
),
211 void (*put
)(struct cflayer
*lyr
))
213 struct cfsrvl
*service
;
215 if (WARN_ON(adapt_layer
== NULL
|| adapt_layer
->dn
== NULL
))
217 service
= container_of(adapt_layer
->dn
, struct cfsrvl
, layer
);
218 service
->hold
= hold
;
221 EXPORT_SYMBOL(caif_client_register_refcnt
);