blob: 353f793d1b3bb7285d466f0f67f3bc6023e10e49 [file] [log] [blame]
Sjur Braendelandb482cd22010-03-30 13:56:23 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
sjur.brandeland@stericsson.com26ee65e2013-04-22 23:57:01 +00003 * Author: Sjur Brendeland
Sjur Braendelandb482cd22010-03-30 13:56:23 +00004 * 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>
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +000013#include <linux/module.h>
Dmitry Tarnyagin44764812012-04-12 08:27:24 +000014#include <linux/pkt_sched.h>
Sjur Braendelandb482cd22010-03-30 13:56:23 +000015#include <net/caif/caif_layer.h>
16#include <net/caif/cfsrvl.h>
17#include <net/caif/cfpkt.h>
18
19#define SRVL_CTRL_PKT_SIZE 1
20#define SRVL_FLOW_OFF 0x81
21#define SRVL_FLOW_ON 0x80
22#define SRVL_SET_PIN 0x82
23#define SRVL_CTRL_PKT_SIZE 1
24
25#define container_obj(layr) container_of(layr, struct cfsrvl, layer)
26
27static void cfservl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +000028 int phyid)
Sjur Braendelandb482cd22010-03-30 13:56:23 +000029{
30 struct cfsrvl *service = container_obj(layr);
Sjur Braendelandb1c74242010-06-17 06:55:38 +000031
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +000032 if (layr->up == NULL || layr->up->ctrlcmd == NULL)
33 return;
Sjur Braendelandb1c74242010-06-17 06:55:38 +000034
Sjur Braendelandb482cd22010-03-30 13:56:23 +000035 switch (ctrl) {
36 case CAIF_CTRLCMD_INIT_RSP:
37 service->open = true;
38 layr->up->ctrlcmd(layr->up, ctrl, phyid);
39 break;
40 case CAIF_CTRLCMD_DEINIT_RSP:
41 case CAIF_CTRLCMD_INIT_FAIL_RSP:
42 service->open = false;
43 layr->up->ctrlcmd(layr->up, ctrl, phyid);
44 break;
45 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
46 if (phyid != service->dev_info.id)
47 break;
48 if (service->modem_flow_on)
49 layr->up->ctrlcmd(layr->up,
50 CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
51 service->phy_flow_on = false;
52 break;
53 case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND:
54 if (phyid != service->dev_info.id)
55 return;
56 if (service->modem_flow_on) {
57 layr->up->ctrlcmd(layr->up,
58 CAIF_CTRLCMD_FLOW_ON_IND,
59 phyid);
60 }
61 service->phy_flow_on = true;
62 break;
63 case CAIF_CTRLCMD_FLOW_OFF_IND:
64 if (service->phy_flow_on) {
65 layr->up->ctrlcmd(layr->up,
66 CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
67 }
68 service->modem_flow_on = false;
69 break;
70 case CAIF_CTRLCMD_FLOW_ON_IND:
71 if (service->phy_flow_on) {
72 layr->up->ctrlcmd(layr->up,
73 CAIF_CTRLCMD_FLOW_ON_IND, phyid);
74 }
75 service->modem_flow_on = true;
76 break;
77 case _CAIF_CTRLCMD_PHYIF_DOWN_IND:
78 /* In case interface is down, let's fake a remove shutdown */
79 layr->up->ctrlcmd(layr->up,
80 CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, phyid);
81 break;
82 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
83 layr->up->ctrlcmd(layr->up, ctrl, phyid);
84 break;
85 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +000086 pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000087 /* We have both modem and phy flow on, send flow on */
88 layr->up->ctrlcmd(layr->up, ctrl, phyid);
89 service->phy_flow_on = true;
90 break;
91 }
92}
93
94static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
95{
96 struct cfsrvl *service = container_obj(layr);
Sjur Braendelandb1c74242010-06-17 06:55:38 +000097
Sjur Braendelandb482cd22010-03-30 13:56:23 +000098 caif_assert(layr != NULL);
99 caif_assert(layr->dn != NULL);
100 caif_assert(layr->dn->transmit != NULL);
Sjur Braendelandb1c74242010-06-17 06:55:38 +0000101
102 if (!service->supports_flowctrl)
103 return 0;
104
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000105 switch (ctrl) {
106 case CAIF_MODEMCMD_FLOW_ON_REQ:
107 {
108 struct cfpkt *pkt;
109 struct caif_payload_info *info;
110 u8 flow_on = SRVL_FLOW_ON;
111 pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
Joe Perches7ac2ed02011-08-25 13:22:24 +0000112 if (!pkt)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000113 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000114
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;
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000124 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000125 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);
Joe Perches7ac2ed02011-08-25 13:22:24 +0000133 if (!pkt)
Sjur Braendeland638e6282010-05-21 02:16:09 +0000134 return -ENOMEM;
Sjur Braendeland638e6282010-05-21 02:16:09 +0000135
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000136 if (cfpkt_add_head(pkt, &flow_off, 1) < 0) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000137 pr_err("Packet is erroneous!\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000138 cfpkt_destroy(pkt);
139 return -EPROTO;
140 }
141 info = cfpkt_info(pkt);
142 info->channel_id = service->layer.id;
143 info->hdr_len = 1;
144 info->dev_info = &service->dev_info;
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000145 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000146 return layr->dn->transmit(layr->dn, pkt);
147 }
148 default:
149 break;
150 }
151 return -EINVAL;
152}
153
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +0000154static void cfsrvl_release(struct cflayer *layer)
Sjur Braendelanda7da1f52010-06-17 06:55:39 +0000155{
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +0000156 struct cfsrvl *service = container_of(layer, struct cfsrvl, layer);
Sjur Braendelanda7da1f52010-06-17 06:55:39 +0000157 kfree(service);
158}
159
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000160void cfsrvl_init(struct cfsrvl *service,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000161 u8 channel_id,
162 struct dev_info *dev_info,
163 bool supports_flowctrl)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000164{
165 caif_assert(offsetof(struct cfsrvl, layer) == 0);
166 service->open = false;
167 service->modem_flow_on = true;
168 service->phy_flow_on = true;
169 service->layer.id = channel_id;
170 service->layer.ctrlcmd = cfservl_ctrlcmd;
171 service->layer.modemcmd = cfservl_modemcmd;
172 service->dev_info = *dev_info;
Sjur Braendelandb1c74242010-06-17 06:55:38 +0000173 service->supports_flowctrl = supports_flowctrl;
Sjur Braendelanda7da1f52010-06-17 06:55:39 +0000174 service->release = cfsrvl_release;
Sjur Braendeland5b208652010-04-28 08:54:36 +0000175}
176
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000177bool cfsrvl_ready(struct cfsrvl *service, int *err)
178{
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000179 if (!service->open) {
180 *err = -ENOTCONN;
181 return false;
182 }
Dmitry Tarnyagin374458b2012-03-11 10:28:31 +0000183 return true;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000184}
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +0000185
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000186u8 cfsrvl_getphyid(struct cflayer *layer)
187{
188 struct cfsrvl *servl = container_obj(layer);
189 return servl->dev_info.id;
190}
191
192bool cfsrvl_phyid_match(struct cflayer *layer, int phyid)
193{
194 struct cfsrvl *servl = container_obj(layer);
195 return servl->dev_info.id == phyid;
196}
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +0000197
198void caif_free_client(struct cflayer *adap_layer)
199{
200 struct cfsrvl *servl;
201 if (adap_layer == NULL || adap_layer->dn == NULL)
202 return;
203 servl = container_obj(adap_layer->dn);
204 servl->release(&servl->layer);
205}
206EXPORT_SYMBOL(caif_free_client);
207
208void caif_client_register_refcnt(struct cflayer *adapt_layer,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000209 void (*hold)(struct cflayer *lyr),
210 void (*put)(struct cflayer *lyr))
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +0000211{
212 struct cfsrvl *service;
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +0000213
Wei Yongjun566f26a2012-09-09 18:38:27 +0000214 if (WARN_ON(adapt_layer == NULL || adapt_layer->dn == NULL))
215 return;
216 service = container_of(adapt_layer->dn, struct cfsrvl, layer);
sjur.brandeland@stericsson.com43e36922011-05-13 02:44:03 +0000217 service->hold = hold;
218 service->put = put;
219}
220EXPORT_SYMBOL(caif_client_register_refcnt);