cfsrvl.c 5.44 KB
Newer Older
1 2
/*
 * Copyright (C) ST-Ericsson AB 2010
3
 * Author:	Sjur Brendeland
4 5 6
 * License terms: GNU General Public License (GPL) version 2
 */

Joe Perches's avatar
Joe Perches committed
7 8
#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__

9 10 11 12
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/slab.h>
13
#include <linux/module.h>
14
#include <linux/pkt_sched.h>
15 16 17 18 19 20 21 22 23 24 25 26 27
#include <net/caif/caif_layer.h>
#include <net/caif/cfsrvl.h>
#include <net/caif/cfpkt.h>

#define SRVL_CTRL_PKT_SIZE 1
#define SRVL_FLOW_OFF 0x81
#define SRVL_FLOW_ON  0x80
#define SRVL_SET_PIN  0x82
#define SRVL_CTRL_PKT_SIZE 1

#define container_obj(layr) container_of(layr, struct cfsrvl, layer)

static void cfservl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
28
			    int phyid)
29 30
{
	struct cfsrvl *service = container_obj(layr);
31

32 33
	if (layr->up == NULL || layr->up->ctrlcmd == NULL)
		return;
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	switch (ctrl) {
	case CAIF_CTRLCMD_INIT_RSP:
		service->open = true;
		layr->up->ctrlcmd(layr->up, ctrl, phyid);
		break;
	case CAIF_CTRLCMD_DEINIT_RSP:
	case CAIF_CTRLCMD_INIT_FAIL_RSP:
		service->open = false;
		layr->up->ctrlcmd(layr->up, ctrl, phyid);
		break;
	case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
		if (phyid != service->dev_info.id)
			break;
		if (service->modem_flow_on)
			layr->up->ctrlcmd(layr->up,
					  CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
		service->phy_flow_on = false;
		break;
	case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND:
		if (phyid != service->dev_info.id)
			return;
		if (service->modem_flow_on) {
			layr->up->ctrlcmd(layr->up,
					   CAIF_CTRLCMD_FLOW_ON_IND,
					   phyid);
		}
		service->phy_flow_on = true;
		break;
	case CAIF_CTRLCMD_FLOW_OFF_IND:
		if (service->phy_flow_on) {
			layr->up->ctrlcmd(layr->up,
					  CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
		}
		service->modem_flow_on = false;
		break;
	case CAIF_CTRLCMD_FLOW_ON_IND:
		if (service->phy_flow_on) {
			layr->up->ctrlcmd(layr->up,
					  CAIF_CTRLCMD_FLOW_ON_IND, phyid);
		}
		service->modem_flow_on = true;
		break;
	case _CAIF_CTRLCMD_PHYIF_DOWN_IND:
		/* In case interface is down, let's fake a remove shutdown */
		layr->up->ctrlcmd(layr->up,
				CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, phyid);
		break;
	case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
		layr->up->ctrlcmd(layr->up, ctrl, phyid);
		break;
	default:
Joe Perches's avatar
Joe Perches committed
86
		pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl);
87 88 89 90 91 92 93 94 95 96
		/* We have both modem and phy flow on, send flow on */
		layr->up->ctrlcmd(layr->up, ctrl, phyid);
		service->phy_flow_on = true;
		break;
	}
}

static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
{
	struct cfsrvl *service = container_obj(layr);
97

98 99 100
	caif_assert(layr != NULL);
	caif_assert(layr->dn != NULL);
	caif_assert(layr->dn->transmit != NULL);
101 102 103 104

	if (!service->supports_flowctrl)
		return 0;

105 106 107 108 109 110 111
	switch (ctrl) {
	case CAIF_MODEMCMD_FLOW_ON_REQ:
		{
			struct cfpkt *pkt;
			struct caif_payload_info *info;
			u8 flow_on = SRVL_FLOW_ON;
			pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
112
			if (!pkt)
113 114 115
				return -ENOMEM;

			if (cfpkt_add_head(pkt, &flow_on, 1) < 0) {
Joe Perches's avatar
Joe Perches committed
116
				pr_err("Packet is erroneous!\n");
117 118 119 120 121 122 123
				cfpkt_destroy(pkt);
				return -EPROTO;
			}
			info = cfpkt_info(pkt);
			info->channel_id = service->layer.id;
			info->hdr_len = 1;
			info->dev_info = &service->dev_info;
124
			cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
125 126 127 128 129 130 131 132
			return layr->dn->transmit(layr->dn, pkt);
		}
	case CAIF_MODEMCMD_FLOW_OFF_REQ:
		{
			struct cfpkt *pkt;
			struct caif_payload_info *info;
			u8 flow_off = SRVL_FLOW_OFF;
			pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
133
			if (!pkt)
134 135
				return -ENOMEM;

136
			if (cfpkt_add_head(pkt, &flow_off, 1) < 0) {
Joe Perches's avatar
Joe Perches committed
137
				pr_err("Packet is erroneous!\n");
138 139 140 141 142 143 144
				cfpkt_destroy(pkt);
				return -EPROTO;
			}
			info = cfpkt_info(pkt);
			info->channel_id = service->layer.id;
			info->hdr_len = 1;
			info->dev_info = &service->dev_info;
145
			cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
146 147 148 149 150 151 152 153
			return layr->dn->transmit(layr->dn, pkt);
		}
	default:
	  break;
	}
	return -EINVAL;
}

154
static void cfsrvl_release(struct cflayer *layer)
155
{
156
	struct cfsrvl *service = container_of(layer, struct cfsrvl, layer);
157 158 159
	kfree(service);
}

160
void cfsrvl_init(struct cfsrvl *service,
161 162 163
		 u8 channel_id,
		 struct dev_info *dev_info,
		 bool supports_flowctrl)
164 165 166 167 168 169 170 171 172
{
	caif_assert(offsetof(struct cfsrvl, layer) == 0);
	service->open = false;
	service->modem_flow_on = true;
	service->phy_flow_on = true;
	service->layer.id = channel_id;
	service->layer.ctrlcmd = cfservl_ctrlcmd;
	service->layer.modemcmd = cfservl_modemcmd;
	service->dev_info = *dev_info;
173
	service->supports_flowctrl = supports_flowctrl;
174
	service->release = cfsrvl_release;
175 176
}

177 178 179 180 181 182
bool cfsrvl_ready(struct cfsrvl *service, int *err)
{
	if (!service->open) {
		*err = -ENOTCONN;
		return false;
	}
183
	return true;
184
}
185

186 187 188 189 190 191 192 193 194 195 196
u8 cfsrvl_getphyid(struct cflayer *layer)
{
	struct cfsrvl *servl = container_obj(layer);
	return servl->dev_info.id;
}

bool cfsrvl_phyid_match(struct cflayer *layer, int phyid)
{
	struct cfsrvl *servl = container_obj(layer);
	return servl->dev_info.id == phyid;
}
197 198 199 200 201 202 203 204 205 206 207 208

void caif_free_client(struct cflayer *adap_layer)
{
	struct cfsrvl *servl;
	if (adap_layer == NULL || adap_layer->dn == NULL)
		return;
	servl = container_obj(adap_layer->dn);
	servl->release(&servl->layer);
}
EXPORT_SYMBOL(caif_free_client);

void caif_client_register_refcnt(struct cflayer *adapt_layer,
209 210
				 void (*hold)(struct cflayer *lyr),
				 void (*put)(struct cflayer *lyr))
211 212 213
{
	struct cfsrvl *service;

214 215 216
	if (WARN_ON(adapt_layer == NULL || adapt_layer->dn == NULL))
		return;
	service = container_of(adapt_layer->dn, struct cfsrvl, layer);
217 218 219 220
	service->hold = hold;
	service->put = put;
}
EXPORT_SYMBOL(caif_client_register_refcnt);