xref: /openbmc/linux/net/caif/cfsrvl.c (revision 81d67439)
1 /*
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 
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8 
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 <net/caif/caif_layer.h>
15 #include <net/caif/cfsrvl.h>
16 #include <net/caif/cfpkt.h>
17 
18 #define SRVL_CTRL_PKT_SIZE 1
19 #define SRVL_FLOW_OFF 0x81
20 #define SRVL_FLOW_ON  0x80
21 #define SRVL_SET_PIN  0x82
22 #define SRVL_CTRL_PKT_SIZE 1
23 
24 #define container_obj(layr) container_of(layr, struct cfsrvl, layer)
25 
26 static void cfservl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
27 				int phyid)
28 {
29 	struct cfsrvl *service = container_obj(layr);
30 
31 	if (layr->up == NULL || layr->up->ctrlcmd == NULL)
32 		return;
33 
34 	switch (ctrl) {
35 	case CAIF_CTRLCMD_INIT_RSP:
36 		service->open = true;
37 		layr->up->ctrlcmd(layr->up, ctrl, phyid);
38 		break;
39 	case CAIF_CTRLCMD_DEINIT_RSP:
40 	case CAIF_CTRLCMD_INIT_FAIL_RSP:
41 		service->open = false;
42 		layr->up->ctrlcmd(layr->up, ctrl, phyid);
43 		break;
44 	case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
45 		if (phyid != service->dev_info.id)
46 			break;
47 		if (service->modem_flow_on)
48 			layr->up->ctrlcmd(layr->up,
49 					  CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
50 		service->phy_flow_on = false;
51 		break;
52 	case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND:
53 		if (phyid != service->dev_info.id)
54 			return;
55 		if (service->modem_flow_on) {
56 			layr->up->ctrlcmd(layr->up,
57 					   CAIF_CTRLCMD_FLOW_ON_IND,
58 					   phyid);
59 		}
60 		service->phy_flow_on = true;
61 		break;
62 	case CAIF_CTRLCMD_FLOW_OFF_IND:
63 		if (service->phy_flow_on) {
64 			layr->up->ctrlcmd(layr->up,
65 					  CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
66 		}
67 		service->modem_flow_on = false;
68 		break;
69 	case CAIF_CTRLCMD_FLOW_ON_IND:
70 		if (service->phy_flow_on) {
71 			layr->up->ctrlcmd(layr->up,
72 					  CAIF_CTRLCMD_FLOW_ON_IND, phyid);
73 		}
74 		service->modem_flow_on = true;
75 		break;
76 	case _CAIF_CTRLCMD_PHYIF_DOWN_IND:
77 		/* In case interface is down, let's fake a remove shutdown */
78 		layr->up->ctrlcmd(layr->up,
79 				CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, phyid);
80 		break;
81 	case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
82 		layr->up->ctrlcmd(layr->up, ctrl, phyid);
83 		break;
84 	default:
85 		pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl);
86 		/* We have both modem and phy flow on, send flow on */
87 		layr->up->ctrlcmd(layr->up, ctrl, phyid);
88 		service->phy_flow_on = true;
89 		break;
90 	}
91 }
92 
93 static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
94 {
95 	struct cfsrvl *service = container_obj(layr);
96 
97 	caif_assert(layr != NULL);
98 	caif_assert(layr->dn != NULL);
99 	caif_assert(layr->dn->transmit != NULL);
100 
101 	if (!service->supports_flowctrl)
102 		return 0;
103 
104 	switch (ctrl) {
105 	case CAIF_MODEMCMD_FLOW_ON_REQ:
106 		{
107 			struct cfpkt *pkt;
108 			struct caif_payload_info *info;
109 			u8 flow_on = SRVL_FLOW_ON;
110 			pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
111 			if (!pkt) {
112 				pr_warn("Out of memory\n");
113 				return -ENOMEM;
114 			}
115 
116 			if (cfpkt_add_head(pkt, &flow_on, 1) < 0) {
117 				pr_err("Packet is erroneous!\n");
118 				cfpkt_destroy(pkt);
119 				return -EPROTO;
120 			}
121 			info = cfpkt_info(pkt);
122 			info->channel_id = service->layer.id;
123 			info->hdr_len = 1;
124 			info->dev_info = &service->dev_info;
125 			return layr->dn->transmit(layr->dn, pkt);
126 		}
127 	case CAIF_MODEMCMD_FLOW_OFF_REQ:
128 		{
129 			struct cfpkt *pkt;
130 			struct caif_payload_info *info;
131 			u8 flow_off = SRVL_FLOW_OFF;
132 			pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
133 			if (!pkt) {
134 				pr_warn("Out of memory\n");
135 				return -ENOMEM;
136 			}
137 
138 			if (cfpkt_add_head(pkt, &flow_off, 1) < 0) {
139 				pr_err("Packet is erroneous!\n");
140 				cfpkt_destroy(pkt);
141 				return -EPROTO;
142 			}
143 			info = cfpkt_info(pkt);
144 			info->channel_id = service->layer.id;
145 			info->hdr_len = 1;
146 			info->dev_info = &service->dev_info;
147 			return layr->dn->transmit(layr->dn, pkt);
148 		}
149 	default:
150 	  break;
151 	}
152 	return -EINVAL;
153 }
154 
155 static void cfsrvl_release(struct cflayer *layer)
156 {
157 	struct cfsrvl *service = container_of(layer, struct cfsrvl, layer);
158 	kfree(service);
159 }
160 
161 void cfsrvl_init(struct cfsrvl *service,
162 			u8 channel_id,
163 			struct dev_info *dev_info,
164 			bool supports_flowctrl
165 			)
166 {
167 	caif_assert(offsetof(struct cfsrvl, layer) == 0);
168 	service->open = false;
169 	service->modem_flow_on = true;
170 	service->phy_flow_on = true;
171 	service->layer.id = channel_id;
172 	service->layer.ctrlcmd = cfservl_ctrlcmd;
173 	service->layer.modemcmd = cfservl_modemcmd;
174 	service->dev_info = *dev_info;
175 	service->supports_flowctrl = supports_flowctrl;
176 	service->release = cfsrvl_release;
177 }
178 
179 bool cfsrvl_ready(struct cfsrvl *service, int *err)
180 {
181 	if (service->open && service->modem_flow_on && service->phy_flow_on)
182 		return true;
183 	if (!service->open) {
184 		*err = -ENOTCONN;
185 		return false;
186 	}
187 	caif_assert(!(service->modem_flow_on && service->phy_flow_on));
188 	*err = -EAGAIN;
189 	return false;
190 }
191 
192 u8 cfsrvl_getphyid(struct cflayer *layer)
193 {
194 	struct cfsrvl *servl = container_obj(layer);
195 	return servl->dev_info.id;
196 }
197 
198 bool cfsrvl_phyid_match(struct cflayer *layer, int phyid)
199 {
200 	struct cfsrvl *servl = container_obj(layer);
201 	return servl->dev_info.id == phyid;
202 }
203 
204 void caif_free_client(struct cflayer *adap_layer)
205 {
206 	struct cfsrvl *servl;
207 	if (adap_layer == NULL || adap_layer->dn == NULL)
208 		return;
209 	servl = container_obj(adap_layer->dn);
210 	servl->release(&servl->layer);
211 }
212 EXPORT_SYMBOL(caif_free_client);
213 
214 void caif_client_register_refcnt(struct cflayer *adapt_layer,
215 					void (*hold)(struct cflayer *lyr),
216 					void (*put)(struct cflayer *lyr))
217 {
218 	struct cfsrvl *service;
219 	service = container_of(adapt_layer->dn, struct cfsrvl, layer);
220 
221 	WARN_ON(adapt_layer == NULL || adapt_layer->dn == NULL);
222 	service->hold = hold;
223 	service->put = put;
224 }
225 EXPORT_SYMBOL(caif_client_register_refcnt);
226