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 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/slab.h>
11 #include <net/caif/caif_layer.h>
12 #include <net/caif/cfsrvl.h>
13 #include <net/caif/cfpkt.h>
15 #define SRVL_CTRL_PKT_SIZE 1
16 #define SRVL_FLOW_OFF 0x81
17 #define SRVL_FLOW_ON 0x80
18 #define SRVL_SET_PIN 0x82
19 #define SRVL_CTRL_PKT_SIZE 1
21 #define container_obj(layr) container_of(layr, struct cfsrvl, layer)
23 static void cfservl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
26 struct cfsrvl
*service
= container_obj(layr
);
27 caif_assert(layr
->up
!= NULL
);
28 caif_assert(layr
->up
->ctrlcmd
!= NULL
);
30 case CAIF_CTRLCMD_INIT_RSP
:
32 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
34 case CAIF_CTRLCMD_DEINIT_RSP
:
35 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
36 service
->open
= false;
37 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
39 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND
:
40 if (phyid
!= service
->dev_info
.id
)
42 if (service
->modem_flow_on
)
43 layr
->up
->ctrlcmd(layr
->up
,
44 CAIF_CTRLCMD_FLOW_OFF_IND
, phyid
);
45 service
->phy_flow_on
= false;
47 case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND
:
48 if (phyid
!= service
->dev_info
.id
)
50 if (service
->modem_flow_on
) {
51 layr
->up
->ctrlcmd(layr
->up
,
52 CAIF_CTRLCMD_FLOW_ON_IND
,
55 service
->phy_flow_on
= true;
57 case CAIF_CTRLCMD_FLOW_OFF_IND
:
58 if (service
->phy_flow_on
) {
59 layr
->up
->ctrlcmd(layr
->up
,
60 CAIF_CTRLCMD_FLOW_OFF_IND
, phyid
);
62 service
->modem_flow_on
= false;
64 case CAIF_CTRLCMD_FLOW_ON_IND
:
65 if (service
->phy_flow_on
) {
66 layr
->up
->ctrlcmd(layr
->up
,
67 CAIF_CTRLCMD_FLOW_ON_IND
, phyid
);
69 service
->modem_flow_on
= true;
71 case _CAIF_CTRLCMD_PHYIF_DOWN_IND
:
72 /* In case interface is down, let's fake a remove shutdown */
73 layr
->up
->ctrlcmd(layr
->up
,
74 CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
, phyid
);
76 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
77 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
80 pr_warning("CAIF: %s(): "
81 "Unexpected ctrl in cfsrvl (%d)\n", __func__
, ctrl
);
82 /* We have both modem and phy flow on, send flow on */
83 layr
->up
->ctrlcmd(layr
->up
, ctrl
, phyid
);
84 service
->phy_flow_on
= true;
89 static int cfservl_modemcmd(struct cflayer
*layr
, enum caif_modemcmd ctrl
)
91 struct cfsrvl
*service
= container_obj(layr
);
92 caif_assert(layr
!= NULL
);
93 caif_assert(layr
->dn
!= NULL
);
94 caif_assert(layr
->dn
->transmit
!= NULL
);
96 case CAIF_MODEMCMD_FLOW_ON_REQ
:
99 struct caif_payload_info
*info
;
100 u8 flow_on
= SRVL_FLOW_ON
;
101 pkt
= cfpkt_create(SRVL_CTRL_PKT_SIZE
);
103 pr_warning("CAIF: %s(): Out of memory\n",
108 if (cfpkt_add_head(pkt
, &flow_on
, 1) < 0) {
109 pr_err("CAIF: %s(): Packet is erroneous!\n",
114 info
= cfpkt_info(pkt
);
115 info
->channel_id
= service
->layer
.id
;
117 info
->dev_info
= &service
->dev_info
;
118 return layr
->dn
->transmit(layr
->dn
, pkt
);
120 case CAIF_MODEMCMD_FLOW_OFF_REQ
:
123 struct caif_payload_info
*info
;
124 u8 flow_off
= SRVL_FLOW_OFF
;
125 pkt
= cfpkt_create(SRVL_CTRL_PKT_SIZE
);
127 pr_warning("CAIF: %s(): Out of memory\n",
132 if (cfpkt_add_head(pkt
, &flow_off
, 1) < 0) {
133 pr_err("CAIF: %s(): Packet is erroneous!\n",
138 info
= cfpkt_info(pkt
);
139 info
->channel_id
= service
->layer
.id
;
141 info
->dev_info
= &service
->dev_info
;
142 return layr
->dn
->transmit(layr
->dn
, pkt
);
150 void cfservl_destroy(struct cflayer
*layer
)
155 void cfsrvl_init(struct cfsrvl
*service
,
157 struct dev_info
*dev_info
)
159 caif_assert(offsetof(struct cfsrvl
, layer
) == 0);
160 service
->open
= false;
161 service
->modem_flow_on
= true;
162 service
->phy_flow_on
= true;
163 service
->layer
.id
= channel_id
;
164 service
->layer
.ctrlcmd
= cfservl_ctrlcmd
;
165 service
->layer
.modemcmd
= cfservl_modemcmd
;
166 service
->dev_info
= *dev_info
;
167 kref_init(&service
->ref
);
170 void cfsrvl_release(struct kref
*kref
)
172 struct cfsrvl
*service
= container_of(kref
, struct cfsrvl
, ref
);
176 bool cfsrvl_ready(struct cfsrvl
*service
, int *err
)
178 if (service
->open
&& service
->modem_flow_on
&& service
->phy_flow_on
)
180 if (!service
->open
) {
184 caif_assert(!(service
->modem_flow_on
&& service
->phy_flow_on
));
188 u8
cfsrvl_getphyid(struct cflayer
*layer
)
190 struct cfsrvl
*servl
= container_obj(layer
);
191 return servl
->dev_info
.id
;
194 bool cfsrvl_phyid_match(struct cflayer
*layer
, int phyid
)
196 struct cfsrvl
*servl
= container_obj(layer
);
197 return servl
->dev_info
.id
== phyid
;