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/stddef.h> |
| 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <net/caif/caif_layer.h> |
| 13 | #include <net/caif/cfpkt.h> |
| 14 | #include <net/caif/cfctrl.h> |
| 15 | |
| 16 | #define container_obj(layr) container_of(layr, struct cfctrl, serv.layer) |
| 17 | #define UTILITY_NAME_LENGTH 16 |
| 18 | #define CFPKT_CTRL_PKT_LEN 20 |
| 19 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 20 | #ifdef CAIF_NO_LOOP |
| 21 | static int handle_loop(struct cfctrl *ctrl, |
| 22 | int cmd, struct cfpkt *pkt){ |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 23 | return -1; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 24 | } |
| 25 | #else |
| 26 | static int handle_loop(struct cfctrl *ctrl, |
| 27 | int cmd, struct cfpkt *pkt); |
| 28 | #endif |
| 29 | static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt); |
| 30 | static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
| 31 | int phyid); |
| 32 | |
| 33 | |
| 34 | struct cflayer *cfctrl_create(void) |
| 35 | { |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 36 | struct dev_info dev_info; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 37 | struct cfctrl *this = |
| 38 | kmalloc(sizeof(struct cfctrl), GFP_ATOMIC); |
| 39 | if (!this) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 40 | pr_warn("Out of memory\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
| 43 | caif_assert(offsetof(struct cfctrl, serv.layer) == 0); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 44 | memset(&dev_info, 0, sizeof(dev_info)); |
| 45 | dev_info.id = 0xff; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 46 | memset(this, 0, sizeof(*this)); |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 47 | cfsrvl_init(&this->serv, 0, &dev_info, false); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 48 | atomic_set(&this->req_seq_no, 1); |
| 49 | atomic_set(&this->rsp_seq_no, 1); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 50 | this->serv.layer.receive = cfctrl_recv; |
| 51 | sprintf(this->serv.layer.name, "ctrl"); |
| 52 | this->serv.layer.ctrlcmd = cfctrl_ctrlcmd; |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 53 | #ifndef CAIF_NO_LOOP |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 54 | spin_lock_init(&this->loop_linkid_lock); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 55 | this->loop_linkid = 1; |
| 56 | #endif |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 57 | spin_lock_init(&this->info_list_lock); |
| 58 | INIT_LIST_HEAD(&this->list); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 59 | return &this->serv.layer; |
| 60 | } |
| 61 | |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 62 | void cfctrl_remove(struct cflayer *layer) |
| 63 | { |
| 64 | struct cfctrl_request_info *p, *tmp; |
| 65 | struct cfctrl *ctrl = container_obj(layer); |
| 66 | |
| 67 | spin_lock_bh(&ctrl->info_list_lock); |
| 68 | list_for_each_entry_safe(p, tmp, &ctrl->list, list) { |
| 69 | list_del(&p->list); |
| 70 | kfree(p); |
| 71 | } |
| 72 | spin_unlock_bh(&ctrl->info_list_lock); |
| 73 | kfree(layer); |
| 74 | } |
| 75 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 76 | static bool param_eq(const struct cfctrl_link_param *p1, |
| 77 | const struct cfctrl_link_param *p2) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 78 | { |
| 79 | bool eq = |
| 80 | p1->linktype == p2->linktype && |
| 81 | p1->priority == p2->priority && |
| 82 | p1->phyid == p2->phyid && |
| 83 | p1->endpoint == p2->endpoint && p1->chtype == p2->chtype; |
| 84 | |
| 85 | if (!eq) |
| 86 | return false; |
| 87 | |
| 88 | switch (p1->linktype) { |
| 89 | case CFCTRL_SRV_VEI: |
| 90 | return true; |
| 91 | case CFCTRL_SRV_DATAGRAM: |
| 92 | return p1->u.datagram.connid == p2->u.datagram.connid; |
| 93 | case CFCTRL_SRV_RFM: |
| 94 | return |
| 95 | p1->u.rfm.connid == p2->u.rfm.connid && |
| 96 | strcmp(p1->u.rfm.volume, p2->u.rfm.volume) == 0; |
| 97 | case CFCTRL_SRV_UTIL: |
| 98 | return |
| 99 | p1->u.utility.fifosize_kb == p2->u.utility.fifosize_kb |
| 100 | && p1->u.utility.fifosize_bufs == |
| 101 | p2->u.utility.fifosize_bufs |
| 102 | && strcmp(p1->u.utility.name, p2->u.utility.name) == 0 |
| 103 | && p1->u.utility.paramlen == p2->u.utility.paramlen |
| 104 | && memcmp(p1->u.utility.params, p2->u.utility.params, |
| 105 | p1->u.utility.paramlen) == 0; |
| 106 | |
| 107 | case CFCTRL_SRV_VIDEO: |
| 108 | return p1->u.video.connid == p2->u.video.connid; |
| 109 | case CFCTRL_SRV_DBG: |
| 110 | return true; |
| 111 | case CFCTRL_SRV_DECM: |
| 112 | return false; |
| 113 | default: |
| 114 | return false; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 119 | static bool cfctrl_req_eq(const struct cfctrl_request_info *r1, |
| 120 | const struct cfctrl_request_info *r2) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 121 | { |
| 122 | if (r1->cmd != r2->cmd) |
| 123 | return false; |
| 124 | if (r1->cmd == CFCTRL_CMD_LINK_SETUP) |
| 125 | return param_eq(&r1->param, &r2->param); |
| 126 | else |
| 127 | return r1->channel_id == r2->channel_id; |
| 128 | } |
| 129 | |
| 130 | /* Insert request at the end */ |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 131 | static void cfctrl_insert_req(struct cfctrl *ctrl, |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 132 | struct cfctrl_request_info *req) |
| 133 | { |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 134 | spin_lock_bh(&ctrl->info_list_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 135 | atomic_inc(&ctrl->req_seq_no); |
| 136 | req->sequence_no = atomic_read(&ctrl->req_seq_no); |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 137 | list_add_tail(&req->list, &ctrl->list); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 138 | spin_unlock_bh(&ctrl->info_list_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 141 | /* Compare and remove request */ |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 142 | static struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl, |
| 143 | struct cfctrl_request_info *req) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 144 | { |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 145 | struct cfctrl_request_info *p, *tmp, *first; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 146 | |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 147 | first = list_first_entry(&ctrl->list, struct cfctrl_request_info, list); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 148 | |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 149 | list_for_each_entry_safe(p, tmp, &ctrl->list, list) { |
| 150 | if (cfctrl_req_eq(req, p)) { |
| 151 | if (p != first) |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 152 | pr_warn("Requests are not received in order\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 153 | |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 154 | atomic_set(&ctrl->rsp_seq_no, |
| 155 | p->sequence_no); |
| 156 | list_del(&p->list); |
| 157 | goto out; |
| 158 | } |
| 159 | } |
| 160 | p = NULL; |
| 161 | out: |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 162 | return p; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer) |
| 166 | { |
| 167 | struct cfctrl *this = container_obj(layer); |
| 168 | return &this->res; |
| 169 | } |
| 170 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 171 | static void init_info(struct caif_payload_info *info, struct cfctrl *cfctrl) |
| 172 | { |
| 173 | info->hdr_len = 0; |
| 174 | info->channel_id = cfctrl->serv.layer.id; |
| 175 | info->dev_info = &cfctrl->serv.dev_info; |
| 176 | } |
| 177 | |
| 178 | void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid) |
| 179 | { |
| 180 | struct cfctrl *cfctrl = container_obj(layer); |
| 181 | int ret; |
| 182 | struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN); |
| 183 | if (!pkt) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 184 | pr_warn("Out of memory\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | caif_assert(offsetof(struct cfctrl, serv.layer) == 0); |
| 188 | init_info(cfpkt_info(pkt), cfctrl); |
| 189 | cfpkt_info(pkt)->dev_info->id = physlinkid; |
| 190 | cfctrl->serv.dev_info.id = physlinkid; |
| 191 | cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM); |
| 192 | cfpkt_addbdy(pkt, physlinkid); |
| 193 | ret = |
| 194 | cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 197 | int cfctrl_linkup_request(struct cflayer *layer, |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 198 | struct cfctrl_link_param *param, |
| 199 | struct cflayer *user_layer) |
| 200 | { |
| 201 | struct cfctrl *cfctrl = container_obj(layer); |
| 202 | u32 tmp32; |
| 203 | u16 tmp16; |
| 204 | u8 tmp8; |
| 205 | struct cfctrl_request_info *req; |
| 206 | int ret; |
| 207 | char utility_name[16]; |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 208 | struct cfpkt *pkt; |
| 209 | |
| 210 | if (cfctrl_cancel_req(layer, user_layer) > 0) { |
| 211 | /* Slight Paranoia, check if already connecting */ |
| 212 | pr_err("Duplicate connect request for same client\n"); |
| 213 | WARN_ON(1); |
| 214 | return -EALREADY; |
| 215 | } |
| 216 | |
| 217 | pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 218 | if (!pkt) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 219 | pr_warn("Out of memory\n"); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 220 | return -ENOMEM; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 221 | } |
| 222 | cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 223 | cfpkt_addbdy(pkt, (param->chtype << 4) | param->linktype); |
| 224 | cfpkt_addbdy(pkt, (param->priority << 3) | param->phyid); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 225 | cfpkt_addbdy(pkt, param->endpoint & 0x03); |
| 226 | |
| 227 | switch (param->linktype) { |
| 228 | case CFCTRL_SRV_VEI: |
| 229 | break; |
| 230 | case CFCTRL_SRV_VIDEO: |
| 231 | cfpkt_addbdy(pkt, (u8) param->u.video.connid); |
| 232 | break; |
| 233 | case CFCTRL_SRV_DBG: |
| 234 | break; |
| 235 | case CFCTRL_SRV_DATAGRAM: |
| 236 | tmp32 = cpu_to_le32(param->u.datagram.connid); |
| 237 | cfpkt_add_body(pkt, &tmp32, 4); |
| 238 | break; |
| 239 | case CFCTRL_SRV_RFM: |
| 240 | /* Construct a frame, convert DatagramConnectionID to network |
| 241 | * format long and copy it out... |
| 242 | */ |
| 243 | tmp32 = cpu_to_le32(param->u.rfm.connid); |
| 244 | cfpkt_add_body(pkt, &tmp32, 4); |
| 245 | /* Add volume name, including zero termination... */ |
| 246 | cfpkt_add_body(pkt, param->u.rfm.volume, |
| 247 | strlen(param->u.rfm.volume) + 1); |
| 248 | break; |
| 249 | case CFCTRL_SRV_UTIL: |
| 250 | tmp16 = cpu_to_le16(param->u.utility.fifosize_kb); |
| 251 | cfpkt_add_body(pkt, &tmp16, 2); |
| 252 | tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs); |
| 253 | cfpkt_add_body(pkt, &tmp16, 2); |
| 254 | memset(utility_name, 0, sizeof(utility_name)); |
| 255 | strncpy(utility_name, param->u.utility.name, |
| 256 | UTILITY_NAME_LENGTH - 1); |
| 257 | cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH); |
| 258 | tmp8 = param->u.utility.paramlen; |
| 259 | cfpkt_add_body(pkt, &tmp8, 1); |
| 260 | cfpkt_add_body(pkt, param->u.utility.params, |
| 261 | param->u.utility.paramlen); |
| 262 | break; |
| 263 | default: |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 264 | pr_warn("Request setup of bad link type = %d\n", |
| 265 | param->linktype); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 266 | return -EINVAL; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 267 | } |
Julia Lawall | 49afa55 | 2010-05-13 10:03:32 +0000 | [diff] [blame] | 268 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 269 | if (!req) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 270 | pr_warn("Out of memory\n"); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 271 | return -ENOMEM; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 272 | } |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 273 | req->client_layer = user_layer; |
| 274 | req->cmd = CFCTRL_CMD_LINK_SETUP; |
| 275 | req->param = *param; |
| 276 | cfctrl_insert_req(cfctrl, req); |
| 277 | init_info(cfpkt_info(pkt), cfctrl); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 278 | /* |
| 279 | * NOTE:Always send linkup and linkdown request on the same |
| 280 | * device as the payload. Otherwise old queued up payload |
| 281 | * might arrive with the newly allocated channel ID. |
| 282 | */ |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 283 | cfpkt_info(pkt)->dev_info->id = param->phyid; |
| 284 | ret = |
| 285 | cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt); |
| 286 | if (ret < 0) { |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 287 | int count; |
| 288 | |
| 289 | count = cfctrl_cancel_req(&cfctrl->serv.layer, |
| 290 | user_layer); |
| 291 | if (count != 1) |
| 292 | pr_err("Could not remove request (%d)", count); |
| 293 | return -ENODEV; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 294 | } |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 295 | return 0; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid, |
| 299 | struct cflayer *client) |
| 300 | { |
| 301 | int ret; |
| 302 | struct cfctrl *cfctrl = container_obj(layer); |
| 303 | struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN); |
| 304 | if (!pkt) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 305 | pr_warn("Out of memory\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 306 | return -ENOMEM; |
| 307 | } |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 308 | cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY); |
| 309 | cfpkt_addbdy(pkt, channelid); |
| 310 | init_info(cfpkt_info(pkt), cfctrl); |
| 311 | ret = |
| 312 | cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 313 | #ifndef CAIF_NO_LOOP |
| 314 | cfctrl->loop_linkused[channelid] = 0; |
| 315 | #endif |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 319 | int cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer) |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 320 | { |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 321 | struct cfctrl_request_info *p, *tmp; |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 322 | struct cfctrl *ctrl = container_obj(layr); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 323 | int found = 0; |
| 324 | spin_lock_bh(&ctrl->info_list_lock); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 325 | |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 326 | list_for_each_entry_safe(p, tmp, &ctrl->list, list) { |
| 327 | if (p->client_layer == adap_layer) { |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 328 | list_del(&p->list); |
| 329 | kfree(p); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 330 | found++; |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 331 | } |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 332 | } |
| 333 | |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 334 | spin_unlock_bh(&ctrl->info_list_lock); |
| 335 | return found; |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 338 | static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt) |
| 339 | { |
| 340 | u8 cmdrsp; |
| 341 | u8 cmd; |
| 342 | int ret = -1; |
| 343 | u16 tmp16; |
| 344 | u8 len; |
| 345 | u8 param[255]; |
| 346 | u8 linkid; |
| 347 | struct cfctrl *cfctrl = container_obj(layer); |
| 348 | struct cfctrl_request_info rsp, *req; |
| 349 | |
| 350 | |
| 351 | cfpkt_extr_head(pkt, &cmdrsp, 1); |
| 352 | cmd = cmdrsp & CFCTRL_CMD_MASK; |
| 353 | if (cmd != CFCTRL_CMD_LINK_ERR |
| 354 | && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)) { |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 355 | if (handle_loop(cfctrl, cmd, pkt) != 0) |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 356 | cmdrsp |= CFCTRL_ERR_BIT; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | switch (cmd) { |
| 360 | case CFCTRL_CMD_LINK_SETUP: |
| 361 | { |
| 362 | enum cfctrl_srv serv; |
| 363 | enum cfctrl_srv servtype; |
| 364 | u8 endpoint; |
| 365 | u8 physlinkid; |
| 366 | u8 prio; |
| 367 | u8 tmp; |
| 368 | u32 tmp32; |
| 369 | u8 *cp; |
| 370 | int i; |
| 371 | struct cfctrl_link_param linkparam; |
| 372 | memset(&linkparam, 0, sizeof(linkparam)); |
| 373 | |
| 374 | cfpkt_extr_head(pkt, &tmp, 1); |
| 375 | |
| 376 | serv = tmp & CFCTRL_SRV_MASK; |
| 377 | linkparam.linktype = serv; |
| 378 | |
| 379 | servtype = tmp >> 4; |
| 380 | linkparam.chtype = servtype; |
| 381 | |
| 382 | cfpkt_extr_head(pkt, &tmp, 1); |
| 383 | physlinkid = tmp & 0x07; |
| 384 | prio = tmp >> 3; |
| 385 | |
| 386 | linkparam.priority = prio; |
| 387 | linkparam.phyid = physlinkid; |
| 388 | cfpkt_extr_head(pkt, &endpoint, 1); |
| 389 | linkparam.endpoint = endpoint & 0x03; |
| 390 | |
| 391 | switch (serv) { |
| 392 | case CFCTRL_SRV_VEI: |
| 393 | case CFCTRL_SRV_DBG: |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 394 | if (CFCTRL_ERR_BIT & cmdrsp) |
| 395 | break; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 396 | /* Link ID */ |
| 397 | cfpkt_extr_head(pkt, &linkid, 1); |
| 398 | break; |
| 399 | case CFCTRL_SRV_VIDEO: |
| 400 | cfpkt_extr_head(pkt, &tmp, 1); |
| 401 | linkparam.u.video.connid = tmp; |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 402 | if (CFCTRL_ERR_BIT & cmdrsp) |
| 403 | break; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 404 | /* Link ID */ |
| 405 | cfpkt_extr_head(pkt, &linkid, 1); |
| 406 | break; |
| 407 | |
| 408 | case CFCTRL_SRV_DATAGRAM: |
| 409 | cfpkt_extr_head(pkt, &tmp32, 4); |
| 410 | linkparam.u.datagram.connid = |
| 411 | le32_to_cpu(tmp32); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 412 | if (CFCTRL_ERR_BIT & cmdrsp) |
| 413 | break; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 414 | /* Link ID */ |
| 415 | cfpkt_extr_head(pkt, &linkid, 1); |
| 416 | break; |
| 417 | case CFCTRL_SRV_RFM: |
| 418 | /* Construct a frame, convert |
| 419 | * DatagramConnectionID |
| 420 | * to network format long and copy it out... |
| 421 | */ |
| 422 | cfpkt_extr_head(pkt, &tmp32, 4); |
| 423 | linkparam.u.rfm.connid = |
| 424 | le32_to_cpu(tmp32); |
| 425 | cp = (u8 *) linkparam.u.rfm.volume; |
| 426 | for (cfpkt_extr_head(pkt, &tmp, 1); |
| 427 | cfpkt_more(pkt) && tmp != '\0'; |
| 428 | cfpkt_extr_head(pkt, &tmp, 1)) |
| 429 | *cp++ = tmp; |
| 430 | *cp = '\0'; |
| 431 | |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 432 | if (CFCTRL_ERR_BIT & cmdrsp) |
| 433 | break; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 434 | /* Link ID */ |
| 435 | cfpkt_extr_head(pkt, &linkid, 1); |
| 436 | |
| 437 | break; |
| 438 | case CFCTRL_SRV_UTIL: |
| 439 | /* Construct a frame, convert |
| 440 | * DatagramConnectionID |
| 441 | * to network format long and copy it out... |
| 442 | */ |
| 443 | /* Fifosize KB */ |
| 444 | cfpkt_extr_head(pkt, &tmp16, 2); |
| 445 | linkparam.u.utility.fifosize_kb = |
| 446 | le16_to_cpu(tmp16); |
| 447 | /* Fifosize bufs */ |
| 448 | cfpkt_extr_head(pkt, &tmp16, 2); |
| 449 | linkparam.u.utility.fifosize_bufs = |
| 450 | le16_to_cpu(tmp16); |
| 451 | /* name */ |
| 452 | cp = (u8 *) linkparam.u.utility.name; |
| 453 | caif_assert(sizeof(linkparam.u.utility.name) |
| 454 | >= UTILITY_NAME_LENGTH); |
| 455 | for (i = 0; |
| 456 | i < UTILITY_NAME_LENGTH |
| 457 | && cfpkt_more(pkt); i++) { |
| 458 | cfpkt_extr_head(pkt, &tmp, 1); |
| 459 | *cp++ = tmp; |
| 460 | } |
| 461 | /* Length */ |
| 462 | cfpkt_extr_head(pkt, &len, 1); |
| 463 | linkparam.u.utility.paramlen = len; |
| 464 | /* Param Data */ |
| 465 | cp = linkparam.u.utility.params; |
| 466 | while (cfpkt_more(pkt) && len--) { |
| 467 | cfpkt_extr_head(pkt, &tmp, 1); |
| 468 | *cp++ = tmp; |
| 469 | } |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 470 | if (CFCTRL_ERR_BIT & cmdrsp) |
| 471 | break; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 472 | /* Link ID */ |
| 473 | cfpkt_extr_head(pkt, &linkid, 1); |
| 474 | /* Length */ |
| 475 | cfpkt_extr_head(pkt, &len, 1); |
| 476 | /* Param Data */ |
| 477 | cfpkt_extr_head(pkt, ¶m, len); |
| 478 | break; |
| 479 | default: |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 480 | pr_warn("Request setup - invalid link type (%d)\n", |
| 481 | serv); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 482 | goto error; |
| 483 | } |
| 484 | |
| 485 | rsp.cmd = cmd; |
| 486 | rsp.param = linkparam; |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 487 | spin_lock_bh(&cfctrl->info_list_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 488 | req = cfctrl_remove_req(cfctrl, &rsp); |
| 489 | |
| 490 | if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) || |
| 491 | cfpkt_erroneous(pkt)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 492 | pr_err("Invalid O/E bit or parse error on CAIF control channel\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 493 | cfctrl->res.reject_rsp(cfctrl->serv.layer.up, |
| 494 | 0, |
| 495 | req ? req->client_layer |
| 496 | : NULL); |
| 497 | } else { |
| 498 | cfctrl->res.linksetup_rsp(cfctrl->serv. |
| 499 | layer.up, linkid, |
| 500 | serv, physlinkid, |
| 501 | req ? req-> |
| 502 | client_layer : NULL); |
| 503 | } |
| 504 | |
| 505 | if (req != NULL) |
| 506 | kfree(req); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 507 | |
| 508 | spin_unlock_bh(&cfctrl->info_list_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 509 | } |
| 510 | break; |
| 511 | case CFCTRL_CMD_LINK_DESTROY: |
| 512 | cfpkt_extr_head(pkt, &linkid, 1); |
Sjur Braendeland | 8d545c8 | 2010-04-28 08:54:37 +0000 | [diff] [blame] | 513 | cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 514 | break; |
| 515 | case CFCTRL_CMD_LINK_ERR: |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 516 | pr_err("Frame Error Indication received\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 517 | cfctrl->res.linkerror_ind(); |
| 518 | break; |
| 519 | case CFCTRL_CMD_ENUM: |
| 520 | cfctrl->res.enum_rsp(); |
| 521 | break; |
| 522 | case CFCTRL_CMD_SLEEP: |
| 523 | cfctrl->res.sleep_rsp(); |
| 524 | break; |
| 525 | case CFCTRL_CMD_WAKE: |
| 526 | cfctrl->res.wake_rsp(); |
| 527 | break; |
| 528 | case CFCTRL_CMD_LINK_RECONF: |
| 529 | cfctrl->res.restart_rsp(); |
| 530 | break; |
| 531 | case CFCTRL_CMD_RADIO_SET: |
| 532 | cfctrl->res.radioset_rsp(); |
| 533 | break; |
| 534 | default: |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 535 | pr_err("Unrecognized Control Frame\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 536 | goto error; |
| 537 | break; |
| 538 | } |
| 539 | ret = 0; |
| 540 | error: |
| 541 | cfpkt_destroy(pkt); |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
| 546 | int phyid) |
| 547 | { |
| 548 | struct cfctrl *this = container_obj(layr); |
| 549 | switch (ctrl) { |
| 550 | case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND: |
| 551 | case CAIF_CTRLCMD_FLOW_OFF_IND: |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 552 | spin_lock_bh(&this->info_list_lock); |
Sjur Braendeland | 7aecf49 | 2010-05-21 02:16:08 +0000 | [diff] [blame] | 553 | if (!list_empty(&this->list)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 554 | pr_debug("Received flow off in control layer\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 555 | } |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 556 | spin_unlock_bh(&this->info_list_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 557 | break; |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 558 | case _CAIF_CTRLCMD_PHYIF_DOWN_IND: { |
| 559 | struct cfctrl_request_info *p, *tmp; |
| 560 | |
| 561 | /* Find all connect request and report failure */ |
| 562 | spin_lock_bh(&this->info_list_lock); |
| 563 | list_for_each_entry_safe(p, tmp, &this->list, list) { |
| 564 | if (p->param.phyid == phyid) { |
| 565 | list_del(&p->list); |
| 566 | p->client_layer->ctrlcmd(p->client_layer, |
| 567 | CAIF_CTRLCMD_INIT_FAIL_RSP, |
| 568 | phyid); |
| 569 | kfree(p); |
| 570 | } |
| 571 | } |
| 572 | spin_unlock_bh(&this->info_list_lock); |
| 573 | break; |
| 574 | } |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 575 | default: |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | #ifndef CAIF_NO_LOOP |
| 581 | static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt) |
| 582 | { |
| 583 | static int last_linkid; |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 584 | static int dec; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 585 | u8 linkid, linktype, tmp; |
| 586 | switch (cmd) { |
| 587 | case CFCTRL_CMD_LINK_SETUP: |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 588 | spin_lock_bh(&ctrl->loop_linkid_lock); |
| 589 | if (!dec) { |
| 590 | for (linkid = last_linkid + 1; linkid < 255; linkid++) |
| 591 | if (!ctrl->loop_linkused[linkid]) |
| 592 | goto found; |
| 593 | } |
| 594 | dec = 1; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 595 | for (linkid = last_linkid - 1; linkid > 0; linkid--) |
| 596 | if (!ctrl->loop_linkused[linkid]) |
| 597 | goto found; |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 598 | spin_unlock_bh(&ctrl->loop_linkid_lock); |
| 599 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 600 | found: |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 601 | if (linkid < 10) |
| 602 | dec = 0; |
| 603 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 604 | if (!ctrl->loop_linkused[linkid]) |
| 605 | ctrl->loop_linkused[linkid] = 1; |
| 606 | |
| 607 | last_linkid = linkid; |
| 608 | |
| 609 | cfpkt_add_trail(pkt, &linkid, 1); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 610 | spin_unlock_bh(&ctrl->loop_linkid_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 611 | cfpkt_peek_head(pkt, &linktype, 1); |
| 612 | if (linktype == CFCTRL_SRV_UTIL) { |
| 613 | tmp = 0x01; |
| 614 | cfpkt_add_trail(pkt, &tmp, 1); |
| 615 | cfpkt_add_trail(pkt, &tmp, 1); |
| 616 | } |
| 617 | break; |
| 618 | |
| 619 | case CFCTRL_CMD_LINK_DESTROY: |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 620 | spin_lock_bh(&ctrl->loop_linkid_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 621 | cfpkt_peek_head(pkt, &linkid, 1); |
| 622 | ctrl->loop_linkused[linkid] = 0; |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame^] | 623 | spin_unlock_bh(&ctrl->loop_linkid_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 624 | break; |
| 625 | default: |
| 626 | break; |
| 627 | } |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 628 | return 0; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 629 | } |
| 630 | #endif |