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
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
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>
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
21 static int handle_loop(struct cfctrl *ctrl,
22 int cmd, struct cfpkt *pkt){
26 static int handle_loop(struct cfctrl *ctrl,
27 int cmd, struct cfpkt *pkt);
29 static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt);
30 static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
34 struct cflayer *cfctrl_create(void)
36 struct dev_info dev_info;
38 kzalloc(sizeof(struct cfctrl), GFP_ATOMIC);
41 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
42 memset(&dev_info, 0, sizeof(dev_info));
44 cfsrvl_init(&this->serv, 0, &dev_info, false);
45 atomic_set(&this->req_seq_no, 1);
46 atomic_set(&this->rsp_seq_no, 1);
47 this->serv.layer.receive = cfctrl_recv;
48 sprintf(this->serv.layer.name, "ctrl");
49 this->serv.layer.ctrlcmd = cfctrl_ctrlcmd;
51 spin_lock_init(&this->loop_linkid_lock);
52 this->loop_linkid = 1;
54 spin_lock_init(&this->info_list_lock);
55 INIT_LIST_HEAD(&this->list);
56 return &this->serv.layer;
59 void cfctrl_remove(struct cflayer *layer)
61 struct cfctrl_request_info *p, *tmp;
62 struct cfctrl *ctrl = container_obj(layer);
64 spin_lock_bh(&ctrl->info_list_lock);
65 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
69 spin_unlock_bh(&ctrl->info_list_lock);
73 static bool param_eq(const struct cfctrl_link_param *p1,
74 const struct cfctrl_link_param *p2)
77 p1->linktype == p2->linktype &&
78 p1->priority == p2->priority &&
79 p1->phyid == p2->phyid &&
80 p1->endpoint == p2->endpoint && p1->chtype == p2->chtype;
85 switch (p1->linktype) {
88 case CFCTRL_SRV_DATAGRAM:
89 return p1->u.datagram.connid == p2->u.datagram.connid;
92 p1->u.rfm.connid == p2->u.rfm.connid &&
93 strcmp(p1->u.rfm.volume, p2->u.rfm.volume) == 0;
96 p1->u.utility.fifosize_kb == p2->u.utility.fifosize_kb
97 && p1->u.utility.fifosize_bufs ==
98 p2->u.utility.fifosize_bufs
99 && strcmp(p1->u.utility.name, p2->u.utility.name) == 0
100 && p1->u.utility.paramlen == p2->u.utility.paramlen
101 && memcmp(p1->u.utility.params, p2->u.utility.params,
102 p1->u.utility.paramlen) == 0;
104 case CFCTRL_SRV_VIDEO:
105 return p1->u.video.connid == p2->u.video.connid;
108 case CFCTRL_SRV_DECM:
116 static bool cfctrl_req_eq(const struct cfctrl_request_info *r1,
117 const struct cfctrl_request_info *r2)
119 if (r1->cmd != r2->cmd)
121 if (r1->cmd == CFCTRL_CMD_LINK_SETUP)
122 return param_eq(&r1->param, &r2->param);
124 return r1->channel_id == r2->channel_id;
127 /* Insert request at the end */
128 static void cfctrl_insert_req(struct cfctrl *ctrl,
129 struct cfctrl_request_info *req)
131 spin_lock_bh(&ctrl->info_list_lock);
132 atomic_inc(&ctrl->req_seq_no);
133 req->sequence_no = atomic_read(&ctrl->req_seq_no);
134 list_add_tail(&req->list, &ctrl->list);
135 spin_unlock_bh(&ctrl->info_list_lock);
138 /* Compare and remove request */
139 static struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl,
140 struct cfctrl_request_info *req)
142 struct cfctrl_request_info *p, *tmp, *first;
144 first = list_first_entry(&ctrl->list, struct cfctrl_request_info, list);
146 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
147 if (cfctrl_req_eq(req, p)) {
149 pr_warn("Requests are not received in order\n");
151 atomic_set(&ctrl->rsp_seq_no,
162 struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer)
164 struct cfctrl *this = container_obj(layer);
168 static void init_info(struct caif_payload_info *info, struct cfctrl *cfctrl)
171 info->channel_id = cfctrl->serv.layer.id;
172 info->dev_info = &cfctrl->serv.dev_info;
175 void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid)
177 struct cfctrl *cfctrl = container_obj(layer);
178 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
179 struct cflayer *dn = cfctrl->serv.layer.dn;
183 pr_debug("not able to send enum request\n");
186 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
187 init_info(cfpkt_info(pkt), cfctrl);
188 cfpkt_info(pkt)->dev_info->id = physlinkid;
189 cfctrl->serv.dev_info.id = physlinkid;
190 cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM);
191 cfpkt_addbdy(pkt, physlinkid);
192 dn->transmit(dn, pkt);
195 int cfctrl_linkup_request(struct cflayer *layer,
196 struct cfctrl_link_param *param,
197 struct cflayer *user_layer)
199 struct cfctrl *cfctrl = container_obj(layer);
203 struct cfctrl_request_info *req;
205 char utility_name[16];
207 struct cflayer *dn = cfctrl->serv.layer.dn;
210 pr_debug("not able to send linkup request\n");
214 if (cfctrl_cancel_req(layer, user_layer) > 0) {
215 /* Slight Paranoia, check if already connecting */
216 pr_err("Duplicate connect request for same client\n");
221 pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
224 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
225 cfpkt_addbdy(pkt, (param->chtype << 4) | param->linktype);
226 cfpkt_addbdy(pkt, (param->priority << 3) | param->phyid);
227 cfpkt_addbdy(pkt, param->endpoint & 0x03);
229 switch (param->linktype) {
232 case CFCTRL_SRV_VIDEO:
233 cfpkt_addbdy(pkt, (u8) param->u.video.connid);
237 case CFCTRL_SRV_DATAGRAM:
238 tmp32 = cpu_to_le32(param->u.datagram.connid);
239 cfpkt_add_body(pkt, &tmp32, 4);
242 /* Construct a frame, convert DatagramConnectionID to network
243 * format long and copy it out...
245 tmp32 = cpu_to_le32(param->u.rfm.connid);
246 cfpkt_add_body(pkt, &tmp32, 4);
247 /* Add volume name, including zero termination... */
248 cfpkt_add_body(pkt, param->u.rfm.volume,
249 strlen(param->u.rfm.volume) + 1);
251 case CFCTRL_SRV_UTIL:
252 tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
253 cfpkt_add_body(pkt, &tmp16, 2);
254 tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
255 cfpkt_add_body(pkt, &tmp16, 2);
256 memset(utility_name, 0, sizeof(utility_name));
257 strncpy(utility_name, param->u.utility.name,
258 UTILITY_NAME_LENGTH - 1);
259 cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
260 tmp8 = param->u.utility.paramlen;
261 cfpkt_add_body(pkt, &tmp8, 1);
262 cfpkt_add_body(pkt, param->u.utility.params,
263 param->u.utility.paramlen);
266 pr_warn("Request setup of bad link type = %d\n",
270 req = kzalloc(sizeof(*req), GFP_KERNEL);
273 req->client_layer = user_layer;
274 req->cmd = CFCTRL_CMD_LINK_SETUP;
276 cfctrl_insert_req(cfctrl, req);
277 init_info(cfpkt_info(pkt), cfctrl);
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.
283 cfpkt_info(pkt)->dev_info->id = param->phyid;
285 dn->transmit(dn, pkt);
289 count = cfctrl_cancel_req(&cfctrl->serv.layer,
292 pr_err("Could not remove request (%d)", count);
299 int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
300 struct cflayer *client)
303 struct cfctrl *cfctrl = container_obj(layer);
304 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
305 struct cflayer *dn = cfctrl->serv.layer.dn;
311 pr_debug("not able to send link-down request\n");
315 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
316 cfpkt_addbdy(pkt, channelid);
317 init_info(cfpkt_info(pkt), cfctrl);
319 dn->transmit(dn, pkt);
321 cfctrl->loop_linkused[channelid] = 0;
326 int cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
328 struct cfctrl_request_info *p, *tmp;
329 struct cfctrl *ctrl = container_obj(layr);
331 spin_lock_bh(&ctrl->info_list_lock);
333 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
334 if (p->client_layer == adap_layer) {
341 spin_unlock_bh(&ctrl->info_list_lock);
345 static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
354 struct cfctrl *cfctrl = container_obj(layer);
355 struct cfctrl_request_info rsp, *req;
358 cfpkt_extr_head(pkt, &cmdrsp, 1);
359 cmd = cmdrsp & CFCTRL_CMD_MASK;
360 if (cmd != CFCTRL_CMD_LINK_ERR
361 && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)
362 && CFCTRL_ERR_BIT != (CFCTRL_ERR_BIT & cmdrsp)) {
363 if (handle_loop(cfctrl, cmd, pkt) != 0)
364 cmdrsp |= CFCTRL_ERR_BIT;
368 case CFCTRL_CMD_LINK_SETUP:
370 enum cfctrl_srv serv;
371 enum cfctrl_srv servtype;
379 struct cfctrl_link_param linkparam;
380 memset(&linkparam, 0, sizeof(linkparam));
382 cfpkt_extr_head(pkt, &tmp, 1);
384 serv = tmp & CFCTRL_SRV_MASK;
385 linkparam.linktype = serv;
388 linkparam.chtype = servtype;
390 cfpkt_extr_head(pkt, &tmp, 1);
391 physlinkid = tmp & 0x07;
394 linkparam.priority = prio;
395 linkparam.phyid = physlinkid;
396 cfpkt_extr_head(pkt, &endpoint, 1);
397 linkparam.endpoint = endpoint & 0x03;
402 if (CFCTRL_ERR_BIT & cmdrsp)
405 cfpkt_extr_head(pkt, &linkid, 1);
407 case CFCTRL_SRV_VIDEO:
408 cfpkt_extr_head(pkt, &tmp, 1);
409 linkparam.u.video.connid = tmp;
410 if (CFCTRL_ERR_BIT & cmdrsp)
413 cfpkt_extr_head(pkt, &linkid, 1);
416 case CFCTRL_SRV_DATAGRAM:
417 cfpkt_extr_head(pkt, &tmp32, 4);
418 linkparam.u.datagram.connid =
420 if (CFCTRL_ERR_BIT & cmdrsp)
423 cfpkt_extr_head(pkt, &linkid, 1);
426 /* Construct a frame, convert
427 * DatagramConnectionID
428 * to network format long and copy it out...
430 cfpkt_extr_head(pkt, &tmp32, 4);
431 linkparam.u.rfm.connid =
433 cp = (u8 *) linkparam.u.rfm.volume;
434 for (cfpkt_extr_head(pkt, &tmp, 1);
435 cfpkt_more(pkt) && tmp != '\0';
436 cfpkt_extr_head(pkt, &tmp, 1))
440 if (CFCTRL_ERR_BIT & cmdrsp)
443 cfpkt_extr_head(pkt, &linkid, 1);
446 case CFCTRL_SRV_UTIL:
447 /* Construct a frame, convert
448 * DatagramConnectionID
449 * to network format long and copy it out...
452 cfpkt_extr_head(pkt, &tmp16, 2);
453 linkparam.u.utility.fifosize_kb =
456 cfpkt_extr_head(pkt, &tmp16, 2);
457 linkparam.u.utility.fifosize_bufs =
460 cp = (u8 *) linkparam.u.utility.name;
461 caif_assert(sizeof(linkparam.u.utility.name)
462 >= UTILITY_NAME_LENGTH);
464 i < UTILITY_NAME_LENGTH
465 && cfpkt_more(pkt); i++) {
466 cfpkt_extr_head(pkt, &tmp, 1);
470 cfpkt_extr_head(pkt, &len, 1);
471 linkparam.u.utility.paramlen = len;
473 cp = linkparam.u.utility.params;
474 while (cfpkt_more(pkt) && len--) {
475 cfpkt_extr_head(pkt, &tmp, 1);
478 if (CFCTRL_ERR_BIT & cmdrsp)
481 cfpkt_extr_head(pkt, &linkid, 1);
483 cfpkt_extr_head(pkt, &len, 1);
485 cfpkt_extr_head(pkt, ¶m, len);
488 pr_warn("Request setup, invalid type (%d)\n",
494 rsp.param = linkparam;
495 spin_lock_bh(&cfctrl->info_list_lock);
496 req = cfctrl_remove_req(cfctrl, &rsp);
498 if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
499 cfpkt_erroneous(pkt)) {
500 pr_err("Invalid O/E bit or parse error "
501 "on CAIF control channel\n");
502 cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
504 req ? req->client_layer
507 cfctrl->res.linksetup_rsp(cfctrl->serv.
511 client_layer : NULL);
517 spin_unlock_bh(&cfctrl->info_list_lock);
520 case CFCTRL_CMD_LINK_DESTROY:
521 cfpkt_extr_head(pkt, &linkid, 1);
522 cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
524 case CFCTRL_CMD_LINK_ERR:
525 pr_err("Frame Error Indication received\n");
526 cfctrl->res.linkerror_ind();
528 case CFCTRL_CMD_ENUM:
529 cfctrl->res.enum_rsp();
531 case CFCTRL_CMD_SLEEP:
532 cfctrl->res.sleep_rsp();
534 case CFCTRL_CMD_WAKE:
535 cfctrl->res.wake_rsp();
537 case CFCTRL_CMD_LINK_RECONF:
538 cfctrl->res.restart_rsp();
540 case CFCTRL_CMD_RADIO_SET:
541 cfctrl->res.radioset_rsp();
544 pr_err("Unrecognized Control Frame\n");
554 static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
557 struct cfctrl *this = container_obj(layr);
559 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
560 case CAIF_CTRLCMD_FLOW_OFF_IND:
561 spin_lock_bh(&this->info_list_lock);
562 if (!list_empty(&this->list))
563 pr_debug("Received flow off in control layer\n");
564 spin_unlock_bh(&this->info_list_lock);
566 case _CAIF_CTRLCMD_PHYIF_DOWN_IND: {
567 struct cfctrl_request_info *p, *tmp;
569 /* Find all connect request and report failure */
570 spin_lock_bh(&this->info_list_lock);
571 list_for_each_entry_safe(p, tmp, &this->list, list) {
572 if (p->param.phyid == phyid) {
574 p->client_layer->ctrlcmd(p->client_layer,
575 CAIF_CTRLCMD_INIT_FAIL_RSP,
580 spin_unlock_bh(&this->info_list_lock);
589 static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
591 static int last_linkid;
593 u8 linkid, linktype, tmp;
595 case CFCTRL_CMD_LINK_SETUP:
596 spin_lock_bh(&ctrl->loop_linkid_lock);
598 for (linkid = last_linkid + 1; linkid < 254; linkid++)
599 if (!ctrl->loop_linkused[linkid])
603 for (linkid = last_linkid - 1; linkid > 1; linkid--)
604 if (!ctrl->loop_linkused[linkid])
606 spin_unlock_bh(&ctrl->loop_linkid_lock);
612 if (!ctrl->loop_linkused[linkid])
613 ctrl->loop_linkused[linkid] = 1;
615 last_linkid = linkid;
617 cfpkt_add_trail(pkt, &linkid, 1);
618 spin_unlock_bh(&ctrl->loop_linkid_lock);
619 cfpkt_peek_head(pkt, &linktype, 1);
620 if (linktype == CFCTRL_SRV_UTIL) {
622 cfpkt_add_trail(pkt, &tmp, 1);
623 cfpkt_add_trail(pkt, &tmp, 1);
627 case CFCTRL_CMD_LINK_DESTROY:
628 spin_lock_bh(&ctrl->loop_linkid_lock);
629 cfpkt_peek_head(pkt, &linkid, 1);
630 ctrl->loop_linkused[linkid] = 0;
631 spin_unlock_bh(&ctrl->loop_linkid_lock);