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