blob: 047cd0eec022f2465b55637f8d374c8540138fe5 [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>
Dmitry Tarnyagin44764812012-04-12 08:27:24 +000012#include <linux/pkt_sched.h>
Sjur Braendelandb482cd22010-03-30 13:56:23 +000013#include <net/caif/caif_layer.h>
14#include <net/caif/cfpkt.h>
15#include <net/caif/cfctrl.h>
16
17#define container_obj(layr) container_of(layr, struct cfctrl, serv.layer)
18#define UTILITY_NAME_LENGTH 16
19#define CFPKT_CTRL_PKT_LEN 20
20
Sjur Braendelandb482cd22010-03-30 13:56:23 +000021#ifdef CAIF_NO_LOOP
22static int handle_loop(struct cfctrl *ctrl,
23 int cmd, struct cfpkt *pkt){
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000024 return -1;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000025}
26#else
27static int handle_loop(struct cfctrl *ctrl,
28 int cmd, struct cfpkt *pkt);
29#endif
30static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt);
31static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
32 int phyid);
33
34
35struct cflayer *cfctrl_create(void)
36{
Sjur Braendeland8d545c82010-04-28 08:54:37 +000037 struct dev_info dev_info;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000038 struct cfctrl *this =
Joe Perches7ac2ed02011-08-25 13:22:24 +000039 kzalloc(sizeof(struct cfctrl), GFP_ATOMIC);
40 if (!this)
Sjur Braendelandb482cd22010-03-30 13:56:23 +000041 return NULL;
Sjur Braendelandb482cd22010-03-30 13:56:23 +000042 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
Sjur Braendeland8d545c82010-04-28 08:54:37 +000043 memset(&dev_info, 0, sizeof(dev_info));
44 dev_info.id = 0xff;
Sjur Braendelandb1c74242010-06-17 06:55:38 +000045 cfsrvl_init(&this->serv, 0, &dev_info, false);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000046 atomic_set(&this->req_seq_no, 1);
47 atomic_set(&this->rsp_seq_no, 1);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000048 this->serv.layer.receive = cfctrl_recv;
49 sprintf(this->serv.layer.name, "ctrl");
50 this->serv.layer.ctrlcmd = cfctrl_ctrlcmd;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +000051#ifndef CAIF_NO_LOOP
Sjur Braendelandb482cd22010-03-30 13:56:23 +000052 spin_lock_init(&this->loop_linkid_lock);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +000053 this->loop_linkid = 1;
54#endif
Sjur Braendeland7aecf492010-05-21 02:16:08 +000055 spin_lock_init(&this->info_list_lock);
56 INIT_LIST_HEAD(&this->list);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000057 return &this->serv.layer;
58}
59
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +000060void cfctrl_remove(struct cflayer *layer)
61{
62 struct cfctrl_request_info *p, *tmp;
63 struct cfctrl *ctrl = container_obj(layer);
64
65 spin_lock_bh(&ctrl->info_list_lock);
66 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
67 list_del(&p->list);
68 kfree(p);
69 }
70 spin_unlock_bh(&ctrl->info_list_lock);
71 kfree(layer);
72}
73
Stephen Hemminger73d6ac62011-04-11 10:43:50 +000074static bool param_eq(const struct cfctrl_link_param *p1,
75 const struct cfctrl_link_param *p2)
Sjur Braendelandb482cd22010-03-30 13:56:23 +000076{
77 bool eq =
78 p1->linktype == p2->linktype &&
79 p1->priority == p2->priority &&
80 p1->phyid == p2->phyid &&
81 p1->endpoint == p2->endpoint && p1->chtype == p2->chtype;
82
83 if (!eq)
84 return false;
85
86 switch (p1->linktype) {
87 case CFCTRL_SRV_VEI:
88 return true;
89 case CFCTRL_SRV_DATAGRAM:
90 return p1->u.datagram.connid == p2->u.datagram.connid;
91 case CFCTRL_SRV_RFM:
92 return
93 p1->u.rfm.connid == p2->u.rfm.connid &&
94 strcmp(p1->u.rfm.volume, p2->u.rfm.volume) == 0;
95 case CFCTRL_SRV_UTIL:
96 return
97 p1->u.utility.fifosize_kb == p2->u.utility.fifosize_kb
98 && p1->u.utility.fifosize_bufs ==
99 p2->u.utility.fifosize_bufs
100 && strcmp(p1->u.utility.name, p2->u.utility.name) == 0
101 && p1->u.utility.paramlen == p2->u.utility.paramlen
102 && memcmp(p1->u.utility.params, p2->u.utility.params,
103 p1->u.utility.paramlen) == 0;
104
105 case CFCTRL_SRV_VIDEO:
106 return p1->u.video.connid == p2->u.video.connid;
107 case CFCTRL_SRV_DBG:
108 return true;
109 case CFCTRL_SRV_DECM:
110 return false;
111 default:
112 return false;
113 }
114 return false;
115}
116
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000117static bool cfctrl_req_eq(const struct cfctrl_request_info *r1,
118 const struct cfctrl_request_info *r2)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000119{
120 if (r1->cmd != r2->cmd)
121 return false;
122 if (r1->cmd == CFCTRL_CMD_LINK_SETUP)
123 return param_eq(&r1->param, &r2->param);
124 else
125 return r1->channel_id == r2->channel_id;
126}
127
128/* Insert request at the end */
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000129static void cfctrl_insert_req(struct cfctrl *ctrl,
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000130 struct cfctrl_request_info *req)
131{
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000132 spin_lock_bh(&ctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000133 atomic_inc(&ctrl->req_seq_no);
134 req->sequence_no = atomic_read(&ctrl->req_seq_no);
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000135 list_add_tail(&req->list, &ctrl->list);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000136 spin_unlock_bh(&ctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000137}
138
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000139/* Compare and remove request */
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000140static struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl,
141 struct cfctrl_request_info *req)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000142{
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000143 struct cfctrl_request_info *p, *tmp, *first;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000144
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000145 first = list_first_entry(&ctrl->list, struct cfctrl_request_info, list);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000146
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000147 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
148 if (cfctrl_req_eq(req, p)) {
149 if (p != first)
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000150 pr_warn("Requests are not received in order\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000151
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000152 atomic_set(&ctrl->rsp_seq_no,
153 p->sequence_no);
154 list_del(&p->list);
155 goto out;
156 }
157 }
158 p = NULL;
159out:
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000160 return p;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000161}
162
163struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer)
164{
165 struct cfctrl *this = container_obj(layer);
166 return &this->res;
167}
168
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000169static void init_info(struct caif_payload_info *info, struct cfctrl *cfctrl)
170{
171 info->hdr_len = 0;
172 info->channel_id = cfctrl->serv.layer.id;
173 info->dev_info = &cfctrl->serv.dev_info;
174}
175
176void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid)
177{
178 struct cfctrl *cfctrl = container_obj(layer);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000179 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000180 struct cflayer *dn = cfctrl->serv.layer.dn;
Joe Perches7ac2ed02011-08-25 13:22:24 +0000181 if (!pkt)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000182 return;
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000183 if (!dn) {
184 pr_debug("not able to send enum request\n");
185 return;
186 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000187 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);
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000193 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000194 dn->transmit(dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000195}
196
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000197int cfctrl_linkup_request(struct cflayer *layer,
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000198 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.comc85c2952011-05-13 02:44:06 +0000208 struct cfpkt *pkt;
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000209 struct cflayer *dn = cfctrl->serv.layer.dn;
210
211 if (!dn) {
212 pr_debug("not able to send linkup request\n");
213 return -ENODEV;
214 }
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000215
216 if (cfctrl_cancel_req(layer, user_layer) > 0) {
217 /* Slight Paranoia, check if already connecting */
218 pr_err("Duplicate connect request for same client\n");
219 WARN_ON(1);
220 return -EALREADY;
221 }
222
223 pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
Joe Perches7ac2ed02011-08-25 13:22:24 +0000224 if (!pkt)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000225 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000226 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000227 cfpkt_addbdy(pkt, (param->chtype << 4) | param->linktype);
228 cfpkt_addbdy(pkt, (param->priority << 3) | param->phyid);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000229 cfpkt_addbdy(pkt, param->endpoint & 0x03);
230
231 switch (param->linktype) {
232 case CFCTRL_SRV_VEI:
233 break;
234 case CFCTRL_SRV_VIDEO:
235 cfpkt_addbdy(pkt, (u8) param->u.video.connid);
236 break;
237 case CFCTRL_SRV_DBG:
238 break;
239 case CFCTRL_SRV_DATAGRAM:
240 tmp32 = cpu_to_le32(param->u.datagram.connid);
241 cfpkt_add_body(pkt, &tmp32, 4);
242 break;
243 case CFCTRL_SRV_RFM:
244 /* Construct a frame, convert DatagramConnectionID to network
245 * format long and copy it out...
246 */
247 tmp32 = cpu_to_le32(param->u.rfm.connid);
248 cfpkt_add_body(pkt, &tmp32, 4);
249 /* Add volume name, including zero termination... */
250 cfpkt_add_body(pkt, param->u.rfm.volume,
251 strlen(param->u.rfm.volume) + 1);
252 break;
253 case CFCTRL_SRV_UTIL:
254 tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
255 cfpkt_add_body(pkt, &tmp16, 2);
256 tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
257 cfpkt_add_body(pkt, &tmp16, 2);
258 memset(utility_name, 0, sizeof(utility_name));
259 strncpy(utility_name, param->u.utility.name,
260 UTILITY_NAME_LENGTH - 1);
261 cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
262 tmp8 = param->u.utility.paramlen;
263 cfpkt_add_body(pkt, &tmp8, 1);
264 cfpkt_add_body(pkt, param->u.utility.params,
265 param->u.utility.paramlen);
266 break;
267 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000268 pr_warn("Request setup of bad link type = %d\n",
269 param->linktype);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000270 return -EINVAL;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000271 }
Julia Lawall49afa552010-05-13 10:03:32 +0000272 req = kzalloc(sizeof(*req), GFP_KERNEL);
Joe Perches7ac2ed02011-08-25 13:22:24 +0000273 if (!req)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000274 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000275 req->client_layer = user_layer;
276 req->cmd = CFCTRL_CMD_LINK_SETUP;
277 req->param = *param;
278 cfctrl_insert_req(cfctrl, req);
279 init_info(cfpkt_info(pkt), cfctrl);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000280 /*
281 * NOTE:Always send linkup and linkdown request on the same
282 * device as the payload. Otherwise old queued up payload
283 * might arrive with the newly allocated channel ID.
284 */
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000285 cfpkt_info(pkt)->dev_info->id = param->phyid;
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000286 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000287 ret =
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000288 dn->transmit(dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000289 if (ret < 0) {
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000290 int count;
291
292 count = cfctrl_cancel_req(&cfctrl->serv.layer,
293 user_layer);
294 if (count != 1)
295 pr_err("Could not remove request (%d)", count);
296 return -ENODEV;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000297 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000298 return 0;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000299}
300
301int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
302 struct cflayer *client)
303{
304 int ret;
305 struct cfctrl *cfctrl = container_obj(layer);
306 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000307 struct cflayer *dn = cfctrl->serv.layer.dn;
308
Joe Perches7ac2ed02011-08-25 13:22:24 +0000309 if (!pkt)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000310 return -ENOMEM;
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000311
312 if (!dn) {
313 pr_debug("not able to send link-down request\n");
314 return -ENODEV;
315 }
316
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000317 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
318 cfpkt_addbdy(pkt, channelid);
319 init_info(cfpkt_info(pkt), cfctrl);
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000320 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000321 ret =
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000322 dn->transmit(dn, pkt);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000323#ifndef CAIF_NO_LOOP
324 cfctrl->loop_linkused[channelid] = 0;
325#endif
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000326 return ret;
327}
328
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000329int cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000330{
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000331 struct cfctrl_request_info *p, *tmp;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000332 struct cfctrl *ctrl = container_obj(layr);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000333 int found = 0;
334 spin_lock_bh(&ctrl->info_list_lock);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000335
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000336 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
337 if (p->client_layer == adap_layer) {
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000338 list_del(&p->list);
339 kfree(p);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000340 found++;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000341 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000342 }
343
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000344 spin_unlock_bh(&ctrl->info_list_lock);
345 return found;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000346}
347
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000348static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
349{
350 u8 cmdrsp;
351 u8 cmd;
352 int ret = -1;
353 u16 tmp16;
354 u8 len;
355 u8 param[255];
356 u8 linkid;
357 struct cfctrl *cfctrl = container_obj(layer);
358 struct cfctrl_request_info rsp, *req;
359
360
361 cfpkt_extr_head(pkt, &cmdrsp, 1);
362 cmd = cmdrsp & CFCTRL_CMD_MASK;
363 if (cmd != CFCTRL_CMD_LINK_ERR
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000364 && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)
365 && CFCTRL_ERR_BIT != (CFCTRL_ERR_BIT & cmdrsp)) {
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000366 if (handle_loop(cfctrl, cmd, pkt) != 0)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000367 cmdrsp |= CFCTRL_ERR_BIT;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000368 }
369
370 switch (cmd) {
371 case CFCTRL_CMD_LINK_SETUP:
372 {
373 enum cfctrl_srv serv;
374 enum cfctrl_srv servtype;
375 u8 endpoint;
376 u8 physlinkid;
377 u8 prio;
378 u8 tmp;
379 u32 tmp32;
380 u8 *cp;
381 int i;
382 struct cfctrl_link_param linkparam;
383 memset(&linkparam, 0, sizeof(linkparam));
384
385 cfpkt_extr_head(pkt, &tmp, 1);
386
387 serv = tmp & CFCTRL_SRV_MASK;
388 linkparam.linktype = serv;
389
390 servtype = tmp >> 4;
391 linkparam.chtype = servtype;
392
393 cfpkt_extr_head(pkt, &tmp, 1);
394 physlinkid = tmp & 0x07;
395 prio = tmp >> 3;
396
397 linkparam.priority = prio;
398 linkparam.phyid = physlinkid;
399 cfpkt_extr_head(pkt, &endpoint, 1);
400 linkparam.endpoint = endpoint & 0x03;
401
402 switch (serv) {
403 case CFCTRL_SRV_VEI:
404 case CFCTRL_SRV_DBG:
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000405 if (CFCTRL_ERR_BIT & cmdrsp)
406 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000407 /* Link ID */
408 cfpkt_extr_head(pkt, &linkid, 1);
409 break;
410 case CFCTRL_SRV_VIDEO:
411 cfpkt_extr_head(pkt, &tmp, 1);
412 linkparam.u.video.connid = tmp;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000413 if (CFCTRL_ERR_BIT & cmdrsp)
414 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000415 /* Link ID */
416 cfpkt_extr_head(pkt, &linkid, 1);
417 break;
418
419 case CFCTRL_SRV_DATAGRAM:
420 cfpkt_extr_head(pkt, &tmp32, 4);
421 linkparam.u.datagram.connid =
422 le32_to_cpu(tmp32);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000423 if (CFCTRL_ERR_BIT & cmdrsp)
424 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000425 /* Link ID */
426 cfpkt_extr_head(pkt, &linkid, 1);
427 break;
428 case CFCTRL_SRV_RFM:
429 /* Construct a frame, convert
430 * DatagramConnectionID
431 * to network format long and copy it out...
432 */
433 cfpkt_extr_head(pkt, &tmp32, 4);
434 linkparam.u.rfm.connid =
435 le32_to_cpu(tmp32);
436 cp = (u8 *) linkparam.u.rfm.volume;
437 for (cfpkt_extr_head(pkt, &tmp, 1);
438 cfpkt_more(pkt) && tmp != '\0';
439 cfpkt_extr_head(pkt, &tmp, 1))
440 *cp++ = tmp;
441 *cp = '\0';
442
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000443 if (CFCTRL_ERR_BIT & cmdrsp)
444 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000445 /* Link ID */
446 cfpkt_extr_head(pkt, &linkid, 1);
447
448 break;
449 case CFCTRL_SRV_UTIL:
450 /* Construct a frame, convert
451 * DatagramConnectionID
452 * to network format long and copy it out...
453 */
454 /* Fifosize KB */
455 cfpkt_extr_head(pkt, &tmp16, 2);
456 linkparam.u.utility.fifosize_kb =
457 le16_to_cpu(tmp16);
458 /* Fifosize bufs */
459 cfpkt_extr_head(pkt, &tmp16, 2);
460 linkparam.u.utility.fifosize_bufs =
461 le16_to_cpu(tmp16);
462 /* name */
463 cp = (u8 *) linkparam.u.utility.name;
464 caif_assert(sizeof(linkparam.u.utility.name)
465 >= UTILITY_NAME_LENGTH);
466 for (i = 0;
467 i < UTILITY_NAME_LENGTH
468 && cfpkt_more(pkt); i++) {
469 cfpkt_extr_head(pkt, &tmp, 1);
470 *cp++ = tmp;
471 }
472 /* Length */
473 cfpkt_extr_head(pkt, &len, 1);
474 linkparam.u.utility.paramlen = len;
475 /* Param Data */
476 cp = linkparam.u.utility.params;
477 while (cfpkt_more(pkt) && len--) {
478 cfpkt_extr_head(pkt, &tmp, 1);
479 *cp++ = tmp;
480 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000481 if (CFCTRL_ERR_BIT & cmdrsp)
482 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000483 /* Link ID */
484 cfpkt_extr_head(pkt, &linkid, 1);
485 /* Length */
486 cfpkt_extr_head(pkt, &len, 1);
487 /* Param Data */
488 cfpkt_extr_head(pkt, &param, len);
489 break;
490 default:
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000491 pr_warn("Request setup, invalid type (%d)\n",
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000492 serv);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000493 goto error;
494 }
495
496 rsp.cmd = cmd;
497 rsp.param = linkparam;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000498 spin_lock_bh(&cfctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000499 req = cfctrl_remove_req(cfctrl, &rsp);
500
501 if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
502 cfpkt_erroneous(pkt)) {
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000503 pr_err("Invalid O/E bit or parse error "
504 "on CAIF control channel\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000505 cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
506 0,
507 req ? req->client_layer
508 : NULL);
509 } else {
510 cfctrl->res.linksetup_rsp(cfctrl->serv.
511 layer.up, linkid,
512 serv, physlinkid,
513 req ? req->
514 client_layer : NULL);
515 }
516
517 if (req != NULL)
518 kfree(req);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000519
520 spin_unlock_bh(&cfctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000521 }
522 break;
523 case CFCTRL_CMD_LINK_DESTROY:
524 cfpkt_extr_head(pkt, &linkid, 1);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000525 cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000526 break;
527 case CFCTRL_CMD_LINK_ERR:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000528 pr_err("Frame Error Indication received\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000529 cfctrl->res.linkerror_ind();
530 break;
531 case CFCTRL_CMD_ENUM:
532 cfctrl->res.enum_rsp();
533 break;
534 case CFCTRL_CMD_SLEEP:
535 cfctrl->res.sleep_rsp();
536 break;
537 case CFCTRL_CMD_WAKE:
538 cfctrl->res.wake_rsp();
539 break;
540 case CFCTRL_CMD_LINK_RECONF:
541 cfctrl->res.restart_rsp();
542 break;
543 case CFCTRL_CMD_RADIO_SET:
544 cfctrl->res.radioset_rsp();
545 break;
546 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000547 pr_err("Unrecognized Control Frame\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000548 goto error;
549 break;
550 }
551 ret = 0;
552error:
553 cfpkt_destroy(pkt);
554 return ret;
555}
556
557static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
558 int phyid)
559{
560 struct cfctrl *this = container_obj(layr);
561 switch (ctrl) {
562 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
563 case CAIF_CTRLCMD_FLOW_OFF_IND:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000564 spin_lock_bh(&this->info_list_lock);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000565 if (!list_empty(&this->list))
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000566 pr_debug("Received flow off in control layer\n");
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000567 spin_unlock_bh(&this->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000568 break;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000569 case _CAIF_CTRLCMD_PHYIF_DOWN_IND: {
570 struct cfctrl_request_info *p, *tmp;
571
572 /* Find all connect request and report failure */
573 spin_lock_bh(&this->info_list_lock);
574 list_for_each_entry_safe(p, tmp, &this->list, list) {
575 if (p->param.phyid == phyid) {
576 list_del(&p->list);
577 p->client_layer->ctrlcmd(p->client_layer,
578 CAIF_CTRLCMD_INIT_FAIL_RSP,
579 phyid);
580 kfree(p);
581 }
582 }
583 spin_unlock_bh(&this->info_list_lock);
584 break;
585 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000586 default:
587 break;
588 }
589}
590
591#ifndef CAIF_NO_LOOP
592static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
593{
594 static int last_linkid;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000595 static int dec;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000596 u8 linkid, linktype, tmp;
597 switch (cmd) {
598 case CFCTRL_CMD_LINK_SETUP:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000599 spin_lock_bh(&ctrl->loop_linkid_lock);
600 if (!dec) {
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000601 for (linkid = last_linkid + 1; linkid < 254; linkid++)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000602 if (!ctrl->loop_linkused[linkid])
603 goto found;
604 }
605 dec = 1;
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000606 for (linkid = last_linkid - 1; linkid > 1; linkid--)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000607 if (!ctrl->loop_linkused[linkid])
608 goto found;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000609 spin_unlock_bh(&ctrl->loop_linkid_lock);
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000610 return -1;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000611found:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000612 if (linkid < 10)
613 dec = 0;
614
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000615 if (!ctrl->loop_linkused[linkid])
616 ctrl->loop_linkused[linkid] = 1;
617
618 last_linkid = linkid;
619
620 cfpkt_add_trail(pkt, &linkid, 1);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000621 spin_unlock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000622 cfpkt_peek_head(pkt, &linktype, 1);
623 if (linktype == CFCTRL_SRV_UTIL) {
624 tmp = 0x01;
625 cfpkt_add_trail(pkt, &tmp, 1);
626 cfpkt_add_trail(pkt, &tmp, 1);
627 }
628 break;
629
630 case CFCTRL_CMD_LINK_DESTROY:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000631 spin_lock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000632 cfpkt_peek_head(pkt, &linkid, 1);
633 ctrl->loop_linkused[linkid] = 0;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000634 spin_unlock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000635 break;
636 default:
637 break;
638 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000639 return 0;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000640}
641#endif