blob: f8ac31371a4a9f4bc9e62579e645f8a0fcdee706 [file] [log] [blame]
Sjur Braendelandb482cd22010-03-30 13:56:23 +00001/*
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 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/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 Braendelandb482cd22010-03-30 13:56:23 +000020#ifdef CAIF_NO_LOOP
21static int handle_loop(struct cfctrl *ctrl,
22 int cmd, struct cfpkt *pkt){
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000023 return -1;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000024}
25#else
26static int handle_loop(struct cfctrl *ctrl,
27 int cmd, struct cfpkt *pkt);
28#endif
29static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt);
30static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
31 int phyid);
32
33
34struct cflayer *cfctrl_create(void)
35{
Sjur Braendeland8d545c82010-04-28 08:54:37 +000036 struct dev_info dev_info;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000037 struct cfctrl *this =
38 kmalloc(sizeof(struct cfctrl), GFP_ATOMIC);
39 if (!this) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +000040 pr_warn("Out of memory\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +000041 return NULL;
42 }
43 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
Sjur Braendeland8d545c82010-04-28 08:54:37 +000044 memset(&dev_info, 0, sizeof(dev_info));
45 dev_info.id = 0xff;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000046 memset(this, 0, sizeof(*this));
Sjur Braendelandb1c74242010-06-17 06:55:38 +000047 cfsrvl_init(&this->serv, 0, &dev_info, false);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000048 atomic_set(&this->req_seq_no, 1);
49 atomic_set(&this->rsp_seq_no, 1);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000050 this->serv.layer.receive = cfctrl_recv;
51 sprintf(this->serv.layer.name, "ctrl");
52 this->serv.layer.ctrlcmd = cfctrl_ctrlcmd;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +000053#ifndef CAIF_NO_LOOP
Sjur Braendelandb482cd22010-03-30 13:56:23 +000054 spin_lock_init(&this->loop_linkid_lock);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +000055 this->loop_linkid = 1;
56#endif
Sjur Braendeland7aecf492010-05-21 02:16:08 +000057 spin_lock_init(&this->info_list_lock);
58 INIT_LIST_HEAD(&this->list);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000059 return &this->serv.layer;
60}
61
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +000062void 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 Hemminger73d6ac62011-04-11 10:43:50 +000076static bool param_eq(const struct cfctrl_link_param *p1,
77 const struct cfctrl_link_param *p2)
Sjur Braendelandb482cd22010-03-30 13:56:23 +000078{
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 Hemminger73d6ac62011-04-11 10:43:50 +0000119static bool cfctrl_req_eq(const struct cfctrl_request_info *r1,
120 const struct cfctrl_request_info *r2)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000121{
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 Hemminger73d6ac62011-04-11 10:43:50 +0000131static void cfctrl_insert_req(struct cfctrl *ctrl,
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000132 struct cfctrl_request_info *req)
133{
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000134 spin_lock_bh(&ctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000135 atomic_inc(&ctrl->req_seq_no);
136 req->sequence_no = atomic_read(&ctrl->req_seq_no);
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000137 list_add_tail(&req->list, &ctrl->list);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000138 spin_unlock_bh(&ctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000139}
140
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000141/* Compare and remove request */
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000142static struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl,
143 struct cfctrl_request_info *req)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000144{
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000145 struct cfctrl_request_info *p, *tmp, *first;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000146
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000147 first = list_first_entry(&ctrl->list, struct cfctrl_request_info, list);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000148
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000149 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
150 if (cfctrl_req_eq(req, p)) {
151 if (p != first)
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000152 pr_warn("Requests are not received in order\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000153
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000154 atomic_set(&ctrl->rsp_seq_no,
155 p->sequence_no);
156 list_del(&p->list);
157 goto out;
158 }
159 }
160 p = NULL;
161out:
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000162 return p;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000163}
164
165struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer)
166{
167 struct cfctrl *this = container_obj(layer);
168 return &this->res;
169}
170
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000171static 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
178void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid)
179{
180 struct cfctrl *cfctrl = container_obj(layer);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000181 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000182 struct cflayer *dn = cfctrl->serv.layer.dn;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000183 if (!pkt) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000184 pr_warn("Out of memory\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000185 return;
186 }
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000187 if (!dn) {
188 pr_debug("not able to send enum request\n");
189 return;
190 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000191 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
192 init_info(cfpkt_info(pkt), cfctrl);
193 cfpkt_info(pkt)->dev_info->id = physlinkid;
194 cfctrl->serv.dev_info.id = physlinkid;
195 cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM);
196 cfpkt_addbdy(pkt, physlinkid);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000197 dn->transmit(dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000198}
199
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000200int cfctrl_linkup_request(struct cflayer *layer,
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000201 struct cfctrl_link_param *param,
202 struct cflayer *user_layer)
203{
204 struct cfctrl *cfctrl = container_obj(layer);
205 u32 tmp32;
206 u16 tmp16;
207 u8 tmp8;
208 struct cfctrl_request_info *req;
209 int ret;
210 char utility_name[16];
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000211 struct cfpkt *pkt;
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000212 struct cflayer *dn = cfctrl->serv.layer.dn;
213
214 if (!dn) {
215 pr_debug("not able to send linkup request\n");
216 return -ENODEV;
217 }
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000218
219 if (cfctrl_cancel_req(layer, user_layer) > 0) {
220 /* Slight Paranoia, check if already connecting */
221 pr_err("Duplicate connect request for same client\n");
222 WARN_ON(1);
223 return -EALREADY;
224 }
225
226 pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000227 if (!pkt) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000228 pr_warn("Out of memory\n");
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000229 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000230 }
231 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000232 cfpkt_addbdy(pkt, (param->chtype << 4) | param->linktype);
233 cfpkt_addbdy(pkt, (param->priority << 3) | param->phyid);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000234 cfpkt_addbdy(pkt, param->endpoint & 0x03);
235
236 switch (param->linktype) {
237 case CFCTRL_SRV_VEI:
238 break;
239 case CFCTRL_SRV_VIDEO:
240 cfpkt_addbdy(pkt, (u8) param->u.video.connid);
241 break;
242 case CFCTRL_SRV_DBG:
243 break;
244 case CFCTRL_SRV_DATAGRAM:
245 tmp32 = cpu_to_le32(param->u.datagram.connid);
246 cfpkt_add_body(pkt, &tmp32, 4);
247 break;
248 case CFCTRL_SRV_RFM:
249 /* Construct a frame, convert DatagramConnectionID to network
250 * format long and copy it out...
251 */
252 tmp32 = cpu_to_le32(param->u.rfm.connid);
253 cfpkt_add_body(pkt, &tmp32, 4);
254 /* Add volume name, including zero termination... */
255 cfpkt_add_body(pkt, param->u.rfm.volume,
256 strlen(param->u.rfm.volume) + 1);
257 break;
258 case CFCTRL_SRV_UTIL:
259 tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
260 cfpkt_add_body(pkt, &tmp16, 2);
261 tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
262 cfpkt_add_body(pkt, &tmp16, 2);
263 memset(utility_name, 0, sizeof(utility_name));
264 strncpy(utility_name, param->u.utility.name,
265 UTILITY_NAME_LENGTH - 1);
266 cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
267 tmp8 = param->u.utility.paramlen;
268 cfpkt_add_body(pkt, &tmp8, 1);
269 cfpkt_add_body(pkt, param->u.utility.params,
270 param->u.utility.paramlen);
271 break;
272 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000273 pr_warn("Request setup of bad link type = %d\n",
274 param->linktype);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000275 return -EINVAL;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000276 }
Julia Lawall49afa552010-05-13 10:03:32 +0000277 req = kzalloc(sizeof(*req), GFP_KERNEL);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000278 if (!req) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000279 pr_warn("Out of memory\n");
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000280 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000281 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000282 req->client_layer = user_layer;
283 req->cmd = CFCTRL_CMD_LINK_SETUP;
284 req->param = *param;
285 cfctrl_insert_req(cfctrl, req);
286 init_info(cfpkt_info(pkt), cfctrl);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000287 /*
288 * NOTE:Always send linkup and linkdown request on the same
289 * device as the payload. Otherwise old queued up payload
290 * might arrive with the newly allocated channel ID.
291 */
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000292 cfpkt_info(pkt)->dev_info->id = param->phyid;
293 ret =
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000294 dn->transmit(dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000295 if (ret < 0) {
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000296 int count;
297
298 count = cfctrl_cancel_req(&cfctrl->serv.layer,
299 user_layer);
300 if (count != 1)
301 pr_err("Could not remove request (%d)", count);
302 return -ENODEV;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000303 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000304 return 0;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000305}
306
307int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
308 struct cflayer *client)
309{
310 int ret;
311 struct cfctrl *cfctrl = container_obj(layer);
312 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000313 struct cflayer *dn = cfctrl->serv.layer.dn;
314
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000315 if (!pkt) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000316 pr_warn("Out of memory\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000317 return -ENOMEM;
318 }
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000319
320 if (!dn) {
321 pr_debug("not able to send link-down request\n");
322 return -ENODEV;
323 }
324
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000325 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
326 cfpkt_addbdy(pkt, channelid);
327 init_info(cfpkt_info(pkt), cfctrl);
328 ret =
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000329 dn->transmit(dn, pkt);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000330#ifndef CAIF_NO_LOOP
331 cfctrl->loop_linkused[channelid] = 0;
332#endif
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000333 return ret;
334}
335
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000336int cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000337{
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000338 struct cfctrl_request_info *p, *tmp;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000339 struct cfctrl *ctrl = container_obj(layr);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000340 int found = 0;
341 spin_lock_bh(&ctrl->info_list_lock);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000342
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000343 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
344 if (p->client_layer == adap_layer) {
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000345 list_del(&p->list);
346 kfree(p);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000347 found++;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000348 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000349 }
350
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000351 spin_unlock_bh(&ctrl->info_list_lock);
352 return found;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000353}
354
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000355static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
356{
357 u8 cmdrsp;
358 u8 cmd;
359 int ret = -1;
360 u16 tmp16;
361 u8 len;
362 u8 param[255];
363 u8 linkid;
364 struct cfctrl *cfctrl = container_obj(layer);
365 struct cfctrl_request_info rsp, *req;
366
367
368 cfpkt_extr_head(pkt, &cmdrsp, 1);
369 cmd = cmdrsp & CFCTRL_CMD_MASK;
370 if (cmd != CFCTRL_CMD_LINK_ERR
371 && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)) {
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000372 if (handle_loop(cfctrl, cmd, pkt) != 0)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000373 cmdrsp |= CFCTRL_ERR_BIT;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000374 }
375
376 switch (cmd) {
377 case CFCTRL_CMD_LINK_SETUP:
378 {
379 enum cfctrl_srv serv;
380 enum cfctrl_srv servtype;
381 u8 endpoint;
382 u8 physlinkid;
383 u8 prio;
384 u8 tmp;
385 u32 tmp32;
386 u8 *cp;
387 int i;
388 struct cfctrl_link_param linkparam;
389 memset(&linkparam, 0, sizeof(linkparam));
390
391 cfpkt_extr_head(pkt, &tmp, 1);
392
393 serv = tmp & CFCTRL_SRV_MASK;
394 linkparam.linktype = serv;
395
396 servtype = tmp >> 4;
397 linkparam.chtype = servtype;
398
399 cfpkt_extr_head(pkt, &tmp, 1);
400 physlinkid = tmp & 0x07;
401 prio = tmp >> 3;
402
403 linkparam.priority = prio;
404 linkparam.phyid = physlinkid;
405 cfpkt_extr_head(pkt, &endpoint, 1);
406 linkparam.endpoint = endpoint & 0x03;
407
408 switch (serv) {
409 case CFCTRL_SRV_VEI:
410 case CFCTRL_SRV_DBG:
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000411 if (CFCTRL_ERR_BIT & cmdrsp)
412 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000413 /* Link ID */
414 cfpkt_extr_head(pkt, &linkid, 1);
415 break;
416 case CFCTRL_SRV_VIDEO:
417 cfpkt_extr_head(pkt, &tmp, 1);
418 linkparam.u.video.connid = tmp;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000419 if (CFCTRL_ERR_BIT & cmdrsp)
420 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000421 /* Link ID */
422 cfpkt_extr_head(pkt, &linkid, 1);
423 break;
424
425 case CFCTRL_SRV_DATAGRAM:
426 cfpkt_extr_head(pkt, &tmp32, 4);
427 linkparam.u.datagram.connid =
428 le32_to_cpu(tmp32);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000429 if (CFCTRL_ERR_BIT & cmdrsp)
430 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000431 /* Link ID */
432 cfpkt_extr_head(pkt, &linkid, 1);
433 break;
434 case CFCTRL_SRV_RFM:
435 /* Construct a frame, convert
436 * DatagramConnectionID
437 * to network format long and copy it out...
438 */
439 cfpkt_extr_head(pkt, &tmp32, 4);
440 linkparam.u.rfm.connid =
441 le32_to_cpu(tmp32);
442 cp = (u8 *) linkparam.u.rfm.volume;
443 for (cfpkt_extr_head(pkt, &tmp, 1);
444 cfpkt_more(pkt) && tmp != '\0';
445 cfpkt_extr_head(pkt, &tmp, 1))
446 *cp++ = tmp;
447 *cp = '\0';
448
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000449 if (CFCTRL_ERR_BIT & cmdrsp)
450 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000451 /* Link ID */
452 cfpkt_extr_head(pkt, &linkid, 1);
453
454 break;
455 case CFCTRL_SRV_UTIL:
456 /* Construct a frame, convert
457 * DatagramConnectionID
458 * to network format long and copy it out...
459 */
460 /* Fifosize KB */
461 cfpkt_extr_head(pkt, &tmp16, 2);
462 linkparam.u.utility.fifosize_kb =
463 le16_to_cpu(tmp16);
464 /* Fifosize bufs */
465 cfpkt_extr_head(pkt, &tmp16, 2);
466 linkparam.u.utility.fifosize_bufs =
467 le16_to_cpu(tmp16);
468 /* name */
469 cp = (u8 *) linkparam.u.utility.name;
470 caif_assert(sizeof(linkparam.u.utility.name)
471 >= UTILITY_NAME_LENGTH);
472 for (i = 0;
473 i < UTILITY_NAME_LENGTH
474 && cfpkt_more(pkt); i++) {
475 cfpkt_extr_head(pkt, &tmp, 1);
476 *cp++ = tmp;
477 }
478 /* Length */
479 cfpkt_extr_head(pkt, &len, 1);
480 linkparam.u.utility.paramlen = len;
481 /* Param Data */
482 cp = linkparam.u.utility.params;
483 while (cfpkt_more(pkt) && len--) {
484 cfpkt_extr_head(pkt, &tmp, 1);
485 *cp++ = tmp;
486 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000487 if (CFCTRL_ERR_BIT & cmdrsp)
488 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000489 /* Link ID */
490 cfpkt_extr_head(pkt, &linkid, 1);
491 /* Length */
492 cfpkt_extr_head(pkt, &len, 1);
493 /* Param Data */
494 cfpkt_extr_head(pkt, &param, len);
495 break;
496 default:
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000497 pr_warn("Request setup, invalid type (%d)\n",
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000498 serv);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000499 goto error;
500 }
501
502 rsp.cmd = cmd;
503 rsp.param = linkparam;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000504 spin_lock_bh(&cfctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000505 req = cfctrl_remove_req(cfctrl, &rsp);
506
507 if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
508 cfpkt_erroneous(pkt)) {
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000509 pr_err("Invalid O/E bit or parse error "
510 "on CAIF control channel\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000511 cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
512 0,
513 req ? req->client_layer
514 : NULL);
515 } else {
516 cfctrl->res.linksetup_rsp(cfctrl->serv.
517 layer.up, linkid,
518 serv, physlinkid,
519 req ? req->
520 client_layer : NULL);
521 }
522
523 if (req != NULL)
524 kfree(req);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000525
526 spin_unlock_bh(&cfctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000527 }
528 break;
529 case CFCTRL_CMD_LINK_DESTROY:
530 cfpkt_extr_head(pkt, &linkid, 1);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000531 cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000532 break;
533 case CFCTRL_CMD_LINK_ERR:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000534 pr_err("Frame Error Indication received\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000535 cfctrl->res.linkerror_ind();
536 break;
537 case CFCTRL_CMD_ENUM:
538 cfctrl->res.enum_rsp();
539 break;
540 case CFCTRL_CMD_SLEEP:
541 cfctrl->res.sleep_rsp();
542 break;
543 case CFCTRL_CMD_WAKE:
544 cfctrl->res.wake_rsp();
545 break;
546 case CFCTRL_CMD_LINK_RECONF:
547 cfctrl->res.restart_rsp();
548 break;
549 case CFCTRL_CMD_RADIO_SET:
550 cfctrl->res.radioset_rsp();
551 break;
552 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000553 pr_err("Unrecognized Control Frame\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000554 goto error;
555 break;
556 }
557 ret = 0;
558error:
559 cfpkt_destroy(pkt);
560 return ret;
561}
562
563static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
564 int phyid)
565{
566 struct cfctrl *this = container_obj(layr);
567 switch (ctrl) {
568 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
569 case CAIF_CTRLCMD_FLOW_OFF_IND:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000570 spin_lock_bh(&this->info_list_lock);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000571 if (!list_empty(&this->list))
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000572 pr_debug("Received flow off in control layer\n");
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000573 spin_unlock_bh(&this->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000574 break;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000575 case _CAIF_CTRLCMD_PHYIF_DOWN_IND: {
576 struct cfctrl_request_info *p, *tmp;
577
578 /* Find all connect request and report failure */
579 spin_lock_bh(&this->info_list_lock);
580 list_for_each_entry_safe(p, tmp, &this->list, list) {
581 if (p->param.phyid == phyid) {
582 list_del(&p->list);
583 p->client_layer->ctrlcmd(p->client_layer,
584 CAIF_CTRLCMD_INIT_FAIL_RSP,
585 phyid);
586 kfree(p);
587 }
588 }
589 spin_unlock_bh(&this->info_list_lock);
590 break;
591 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000592 default:
593 break;
594 }
595}
596
597#ifndef CAIF_NO_LOOP
598static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
599{
600 static int last_linkid;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000601 static int dec;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000602 u8 linkid, linktype, tmp;
603 switch (cmd) {
604 case CFCTRL_CMD_LINK_SETUP:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000605 spin_lock_bh(&ctrl->loop_linkid_lock);
606 if (!dec) {
607 for (linkid = last_linkid + 1; linkid < 255; linkid++)
608 if (!ctrl->loop_linkused[linkid])
609 goto found;
610 }
611 dec = 1;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000612 for (linkid = last_linkid - 1; linkid > 0; linkid--)
613 if (!ctrl->loop_linkused[linkid])
614 goto found;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000615 spin_unlock_bh(&ctrl->loop_linkid_lock);
616
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000617found:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000618 if (linkid < 10)
619 dec = 0;
620
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000621 if (!ctrl->loop_linkused[linkid])
622 ctrl->loop_linkused[linkid] = 1;
623
624 last_linkid = linkid;
625
626 cfpkt_add_trail(pkt, &linkid, 1);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000627 spin_unlock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000628 cfpkt_peek_head(pkt, &linktype, 1);
629 if (linktype == CFCTRL_SRV_UTIL) {
630 tmp = 0x01;
631 cfpkt_add_trail(pkt, &tmp, 1);
632 cfpkt_add_trail(pkt, &tmp, 1);
633 }
634 break;
635
636 case CFCTRL_CMD_LINK_DESTROY:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000637 spin_lock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000638 cfpkt_peek_head(pkt, &linkid, 1);
639 ctrl->loop_linkused[linkid] = 0;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000640 spin_unlock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000641 break;
642 default:
643 break;
644 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000645 return 0;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000646}
647#endif