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> |
sjur.brandeland@stericsson.com | 43e3692 | 2011-05-13 02:44:03 +0000 | [diff] [blame] | 13 | #include <linux/module.h> |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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); |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 30 | |
sjur.brandeland@stericsson.com | 43e3692 | 2011-05-13 02:44:03 +0000 | [diff] [blame] | 31 | if (layr->up == NULL || layr->up->ctrlcmd == NULL) |
| 32 | return; |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 33 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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: |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 85 | pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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); |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 96 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 97 | caif_assert(layr != NULL); |
| 98 | caif_assert(layr->dn != NULL); |
| 99 | caif_assert(layr->dn->transmit != NULL); |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 100 | |
| 101 | if (!service->supports_flowctrl) |
| 102 | return 0; |
| 103 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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); |
Joe Perches | 7ac2ed0 | 2011-08-25 13:22:24 +0000 | [diff] [blame] | 111 | if (!pkt) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 112 | return -ENOMEM; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 113 | |
| 114 | if (cfpkt_add_head(pkt, &flow_on, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 115 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 116 | cfpkt_destroy(pkt); |
| 117 | return -EPROTO; |
| 118 | } |
| 119 | info = cfpkt_info(pkt); |
| 120 | info->channel_id = service->layer.id; |
| 121 | info->hdr_len = 1; |
| 122 | info->dev_info = &service->dev_info; |
| 123 | return layr->dn->transmit(layr->dn, pkt); |
| 124 | } |
| 125 | case CAIF_MODEMCMD_FLOW_OFF_REQ: |
| 126 | { |
| 127 | struct cfpkt *pkt; |
| 128 | struct caif_payload_info *info; |
| 129 | u8 flow_off = SRVL_FLOW_OFF; |
| 130 | pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE); |
Joe Perches | 7ac2ed0 | 2011-08-25 13:22:24 +0000 | [diff] [blame] | 131 | if (!pkt) |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 132 | return -ENOMEM; |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 133 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 134 | if (cfpkt_add_head(pkt, &flow_off, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 135 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 136 | cfpkt_destroy(pkt); |
| 137 | return -EPROTO; |
| 138 | } |
| 139 | info = cfpkt_info(pkt); |
| 140 | info->channel_id = service->layer.id; |
| 141 | info->hdr_len = 1; |
| 142 | info->dev_info = &service->dev_info; |
| 143 | return layr->dn->transmit(layr->dn, pkt); |
| 144 | } |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | |
sjur.brandeland@stericsson.com | 43e3692 | 2011-05-13 02:44:03 +0000 | [diff] [blame] | 151 | static void cfsrvl_release(struct cflayer *layer) |
Sjur Braendeland | a7da1f5 | 2010-06-17 06:55:39 +0000 | [diff] [blame] | 152 | { |
sjur.brandeland@stericsson.com | 43e3692 | 2011-05-13 02:44:03 +0000 | [diff] [blame] | 153 | struct cfsrvl *service = container_of(layer, struct cfsrvl, layer); |
Sjur Braendeland | a7da1f5 | 2010-06-17 06:55:39 +0000 | [diff] [blame] | 154 | kfree(service); |
| 155 | } |
| 156 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 157 | void cfsrvl_init(struct cfsrvl *service, |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 158 | u8 channel_id, |
| 159 | struct dev_info *dev_info, |
| 160 | bool supports_flowctrl |
| 161 | ) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 162 | { |
| 163 | caif_assert(offsetof(struct cfsrvl, layer) == 0); |
| 164 | service->open = false; |
| 165 | service->modem_flow_on = true; |
| 166 | service->phy_flow_on = true; |
| 167 | service->layer.id = channel_id; |
| 168 | service->layer.ctrlcmd = cfservl_ctrlcmd; |
| 169 | service->layer.modemcmd = cfservl_modemcmd; |
| 170 | service->dev_info = *dev_info; |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 171 | service->supports_flowctrl = supports_flowctrl; |
Sjur Braendeland | a7da1f5 | 2010-06-17 06:55:39 +0000 | [diff] [blame] | 172 | service->release = cfsrvl_release; |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 175 | bool cfsrvl_ready(struct cfsrvl *service, int *err) |
| 176 | { |
| 177 | if (service->open && service->modem_flow_on && service->phy_flow_on) |
| 178 | return true; |
| 179 | if (!service->open) { |
| 180 | *err = -ENOTCONN; |
| 181 | return false; |
| 182 | } |
| 183 | caif_assert(!(service->modem_flow_on && service->phy_flow_on)); |
| 184 | *err = -EAGAIN; |
| 185 | return false; |
| 186 | } |
sjur.brandeland@stericsson.com | 43e3692 | 2011-05-13 02:44:03 +0000 | [diff] [blame] | 187 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 188 | u8 cfsrvl_getphyid(struct cflayer *layer) |
| 189 | { |
| 190 | struct cfsrvl *servl = container_obj(layer); |
| 191 | return servl->dev_info.id; |
| 192 | } |
| 193 | |
| 194 | bool cfsrvl_phyid_match(struct cflayer *layer, int phyid) |
| 195 | { |
| 196 | struct cfsrvl *servl = container_obj(layer); |
| 197 | return servl->dev_info.id == phyid; |
| 198 | } |
sjur.brandeland@stericsson.com | 43e3692 | 2011-05-13 02:44:03 +0000 | [diff] [blame] | 199 | |
| 200 | void caif_free_client(struct cflayer *adap_layer) |
| 201 | { |
| 202 | struct cfsrvl *servl; |
| 203 | if (adap_layer == NULL || adap_layer->dn == NULL) |
| 204 | return; |
| 205 | servl = container_obj(adap_layer->dn); |
| 206 | servl->release(&servl->layer); |
| 207 | } |
| 208 | EXPORT_SYMBOL(caif_free_client); |
| 209 | |
| 210 | void caif_client_register_refcnt(struct cflayer *adapt_layer, |
| 211 | void (*hold)(struct cflayer *lyr), |
| 212 | void (*put)(struct cflayer *lyr)) |
| 213 | { |
| 214 | struct cfsrvl *service; |
| 215 | service = container_of(adapt_layer->dn, struct cfsrvl, layer); |
| 216 | |
| 217 | WARN_ON(adapt_layer == NULL || adapt_layer->dn == NULL); |
| 218 | service->hold = hold; |
| 219 | service->put = put; |
| 220 | } |
| 221 | EXPORT_SYMBOL(caif_client_register_refcnt); |