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 kmalloc(sizeof(struct cfctrl
), GFP_ATOMIC
);
40 pr_warn("Out of memory\n");
43 caif_assert(offsetof(struct cfctrl
, serv
.layer
) == 0);
44 memset(&dev_info
, 0, sizeof(dev_info
));
46 memset(this, 0, sizeof(*this));
47 cfsrvl_init(&this->serv
, 0, &dev_info
, false);
48 atomic_set(&this->req_seq_no
, 1);
49 atomic_set(&this->rsp_seq_no
, 1);
50 this->serv
.layer
.receive
= cfctrl_recv
;
51 sprintf(this->serv
.layer
.name
, "ctrl");
52 this->serv
.layer
.ctrlcmd
= cfctrl_ctrlcmd
;
54 spin_lock_init(&this->loop_linkid_lock
);
55 this->loop_linkid
= 1;
57 spin_lock_init(&this->info_list_lock
);
58 INIT_LIST_HEAD(&this->list
);
59 return &this->serv
.layer
;
62 void cfctrl_remove(struct cflayer
*layer
)
64 struct cfctrl_request_info
*p
, *tmp
;
65 struct cfctrl
*ctrl
= container_obj(layer
);
67 spin_lock_bh(&ctrl
->info_list_lock
);
68 list_for_each_entry_safe(p
, tmp
, &ctrl
->list
, list
) {
72 spin_unlock_bh(&ctrl
->info_list_lock
);
76 static bool param_eq(const struct cfctrl_link_param
*p1
,
77 const struct cfctrl_link_param
*p2
)
80 p1
->linktype
== p2
->linktype
&&
81 p1
->priority
== p2
->priority
&&
82 p1
->phyid
== p2
->phyid
&&
83 p1
->endpoint
== p2
->endpoint
&& p1
->chtype
== p2
->chtype
;
88 switch (p1
->linktype
) {
91 case CFCTRL_SRV_DATAGRAM
:
92 return p1
->u
.datagram
.connid
== p2
->u
.datagram
.connid
;
95 p1
->u
.rfm
.connid
== p2
->u
.rfm
.connid
&&
96 strcmp(p1
->u
.rfm
.volume
, p2
->u
.rfm
.volume
) == 0;
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;
107 case CFCTRL_SRV_VIDEO
:
108 return p1
->u
.video
.connid
== p2
->u
.video
.connid
;
111 case CFCTRL_SRV_DECM
:
119 static bool cfctrl_req_eq(const struct cfctrl_request_info
*r1
,
120 const struct cfctrl_request_info
*r2
)
122 if (r1
->cmd
!= r2
->cmd
)
124 if (r1
->cmd
== CFCTRL_CMD_LINK_SETUP
)
125 return param_eq(&r1
->param
, &r2
->param
);
127 return r1
->channel_id
== r2
->channel_id
;
130 /* Insert request at the end */
131 static void cfctrl_insert_req(struct cfctrl
*ctrl
,
132 struct cfctrl_request_info
*req
)
134 spin_lock_bh(&ctrl
->info_list_lock
);
135 atomic_inc(&ctrl
->req_seq_no
);
136 req
->sequence_no
= atomic_read(&ctrl
->req_seq_no
);
137 list_add_tail(&req
->list
, &ctrl
->list
);
138 spin_unlock_bh(&ctrl
->info_list_lock
);
141 /* Compare and remove request */
142 static struct cfctrl_request_info
*cfctrl_remove_req(struct cfctrl
*ctrl
,
143 struct cfctrl_request_info
*req
)
145 struct cfctrl_request_info
*p
, *tmp
, *first
;
147 first
= list_first_entry(&ctrl
->list
, struct cfctrl_request_info
, list
);
149 list_for_each_entry_safe(p
, tmp
, &ctrl
->list
, list
) {
150 if (cfctrl_req_eq(req
, p
)) {
152 pr_warn("Requests are not received in order\n");
154 atomic_set(&ctrl
->rsp_seq_no
,
165 struct cfctrl_rsp
*cfctrl_get_respfuncs(struct cflayer
*layer
)
167 struct cfctrl
*this = container_obj(layer
);
171 static void init_info(struct caif_payload_info
*info
, struct cfctrl
*cfctrl
)
174 info
->channel_id
= cfctrl
->serv
.layer
.id
;
175 info
->dev_info
= &cfctrl
->serv
.dev_info
;
178 void cfctrl_enum_req(struct cflayer
*layer
, u8 physlinkid
)
180 struct cfctrl
*cfctrl
= container_obj(layer
);
181 struct cfpkt
*pkt
= cfpkt_create(CFPKT_CTRL_PKT_LEN
);
182 struct cflayer
*dn
= cfctrl
->serv
.layer
.dn
;
184 pr_warn("Out of memory\n");
188 pr_debug("not able to send enum request\n");
191 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
);
197 dn
->transmit(dn
, pkt
);
200 int cfctrl_linkup_request(struct cflayer
*layer
,
201 struct cfctrl_link_param
*param
,
202 struct cflayer
*user_layer
)
204 struct cfctrl
*cfctrl
= container_obj(layer
);
208 struct cfctrl_request_info
*req
;
210 char utility_name
[16];
212 struct cflayer
*dn
= cfctrl
->serv
.layer
.dn
;
215 pr_debug("not able to send linkup request\n");
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");
226 pkt
= cfpkt_create(CFPKT_CTRL_PKT_LEN
);
228 pr_warn("Out of memory\n");
231 cfpkt_addbdy(pkt
, CFCTRL_CMD_LINK_SETUP
);
232 cfpkt_addbdy(pkt
, (param
->chtype
<< 4) | param
->linktype
);
233 cfpkt_addbdy(pkt
, (param
->priority
<< 3) | param
->phyid
);
234 cfpkt_addbdy(pkt
, param
->endpoint
& 0x03);
236 switch (param
->linktype
) {
239 case CFCTRL_SRV_VIDEO
:
240 cfpkt_addbdy(pkt
, (u8
) param
->u
.video
.connid
);
244 case CFCTRL_SRV_DATAGRAM
:
245 tmp32
= cpu_to_le32(param
->u
.datagram
.connid
);
246 cfpkt_add_body(pkt
, &tmp32
, 4);
249 /* Construct a frame, convert DatagramConnectionID to network
250 * format long and copy it out...
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);
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
);
273 pr_warn("Request setup of bad link type = %d\n",
277 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
279 pr_warn("Out of memory\n");
282 req
->client_layer
= user_layer
;
283 req
->cmd
= CFCTRL_CMD_LINK_SETUP
;
285 cfctrl_insert_req(cfctrl
, req
);
286 init_info(cfpkt_info(pkt
), cfctrl
);
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.
292 cfpkt_info(pkt
)->dev_info
->id
= param
->phyid
;
294 dn
->transmit(dn
, pkt
);
298 count
= cfctrl_cancel_req(&cfctrl
->serv
.layer
,
301 pr_err("Could not remove request (%d)", count
);
307 int cfctrl_linkdown_req(struct cflayer
*layer
, u8 channelid
,
308 struct cflayer
*client
)
311 struct cfctrl
*cfctrl
= container_obj(layer
);
312 struct cfpkt
*pkt
= cfpkt_create(CFPKT_CTRL_PKT_LEN
);
313 struct cflayer
*dn
= cfctrl
->serv
.layer
.dn
;
316 pr_warn("Out of memory\n");
321 pr_debug("not able to send link-down request\n");
325 cfpkt_addbdy(pkt
, CFCTRL_CMD_LINK_DESTROY
);
326 cfpkt_addbdy(pkt
, channelid
);
327 init_info(cfpkt_info(pkt
), cfctrl
);
329 dn
->transmit(dn
, pkt
);
331 cfctrl
->loop_linkused
[channelid
] = 0;
336 int cfctrl_cancel_req(struct cflayer
*layr
, struct cflayer
*adap_layer
)
338 struct cfctrl_request_info
*p
, *tmp
;
339 struct cfctrl
*ctrl
= container_obj(layr
);
341 spin_lock_bh(&ctrl
->info_list_lock
);
343 list_for_each_entry_safe(p
, tmp
, &ctrl
->list
, list
) {
344 if (p
->client_layer
== adap_layer
) {
351 spin_unlock_bh(&ctrl
->info_list_lock
);
355 static int cfctrl_recv(struct cflayer
*layer
, struct cfpkt
*pkt
)
364 struct cfctrl
*cfctrl
= container_obj(layer
);
365 struct cfctrl_request_info rsp
, *req
;
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
)
372 && CFCTRL_ERR_BIT
!= (CFCTRL_ERR_BIT
& cmdrsp
)) {
373 if (handle_loop(cfctrl
, cmd
, pkt
) != 0)
374 cmdrsp
|= CFCTRL_ERR_BIT
;
378 case CFCTRL_CMD_LINK_SETUP
:
380 enum cfctrl_srv serv
;
381 enum cfctrl_srv servtype
;
389 struct cfctrl_link_param linkparam
;
390 memset(&linkparam
, 0, sizeof(linkparam
));
392 cfpkt_extr_head(pkt
, &tmp
, 1);
394 serv
= tmp
& CFCTRL_SRV_MASK
;
395 linkparam
.linktype
= serv
;
398 linkparam
.chtype
= servtype
;
400 cfpkt_extr_head(pkt
, &tmp
, 1);
401 physlinkid
= tmp
& 0x07;
404 linkparam
.priority
= prio
;
405 linkparam
.phyid
= physlinkid
;
406 cfpkt_extr_head(pkt
, &endpoint
, 1);
407 linkparam
.endpoint
= endpoint
& 0x03;
412 if (CFCTRL_ERR_BIT
& cmdrsp
)
415 cfpkt_extr_head(pkt
, &linkid
, 1);
417 case CFCTRL_SRV_VIDEO
:
418 cfpkt_extr_head(pkt
, &tmp
, 1);
419 linkparam
.u
.video
.connid
= tmp
;
420 if (CFCTRL_ERR_BIT
& cmdrsp
)
423 cfpkt_extr_head(pkt
, &linkid
, 1);
426 case CFCTRL_SRV_DATAGRAM
:
427 cfpkt_extr_head(pkt
, &tmp32
, 4);
428 linkparam
.u
.datagram
.connid
=
430 if (CFCTRL_ERR_BIT
& cmdrsp
)
433 cfpkt_extr_head(pkt
, &linkid
, 1);
436 /* Construct a frame, convert
437 * DatagramConnectionID
438 * to network format long and copy it out...
440 cfpkt_extr_head(pkt
, &tmp32
, 4);
441 linkparam
.u
.rfm
.connid
=
443 cp
= (u8
*) linkparam
.u
.rfm
.volume
;
444 for (cfpkt_extr_head(pkt
, &tmp
, 1);
445 cfpkt_more(pkt
) && tmp
!= '\0';
446 cfpkt_extr_head(pkt
, &tmp
, 1))
450 if (CFCTRL_ERR_BIT
& cmdrsp
)
453 cfpkt_extr_head(pkt
, &linkid
, 1);
456 case CFCTRL_SRV_UTIL
:
457 /* Construct a frame, convert
458 * DatagramConnectionID
459 * to network format long and copy it out...
462 cfpkt_extr_head(pkt
, &tmp16
, 2);
463 linkparam
.u
.utility
.fifosize_kb
=
466 cfpkt_extr_head(pkt
, &tmp16
, 2);
467 linkparam
.u
.utility
.fifosize_bufs
=
470 cp
= (u8
*) linkparam
.u
.utility
.name
;
471 caif_assert(sizeof(linkparam
.u
.utility
.name
)
472 >= UTILITY_NAME_LENGTH
);
474 i
< UTILITY_NAME_LENGTH
475 && cfpkt_more(pkt
); i
++) {
476 cfpkt_extr_head(pkt
, &tmp
, 1);
480 cfpkt_extr_head(pkt
, &len
, 1);
481 linkparam
.u
.utility
.paramlen
= len
;
483 cp
= linkparam
.u
.utility
.params
;
484 while (cfpkt_more(pkt
) && len
--) {
485 cfpkt_extr_head(pkt
, &tmp
, 1);
488 if (CFCTRL_ERR_BIT
& cmdrsp
)
491 cfpkt_extr_head(pkt
, &linkid
, 1);
493 cfpkt_extr_head(pkt
, &len
, 1);
495 cfpkt_extr_head(pkt
, ¶m
, len
);
498 pr_warn("Request setup, invalid type (%d)\n",
504 rsp
.param
= linkparam
;
505 spin_lock_bh(&cfctrl
->info_list_lock
);
506 req
= cfctrl_remove_req(cfctrl
, &rsp
);
508 if (CFCTRL_ERR_BIT
== (CFCTRL_ERR_BIT
& cmdrsp
) ||
509 cfpkt_erroneous(pkt
)) {
510 pr_err("Invalid O/E bit or parse error "
511 "on CAIF control channel\n");
512 cfctrl
->res
.reject_rsp(cfctrl
->serv
.layer
.up
,
514 req
? req
->client_layer
517 cfctrl
->res
.linksetup_rsp(cfctrl
->serv
.
521 client_layer
: NULL
);
527 spin_unlock_bh(&cfctrl
->info_list_lock
);
530 case CFCTRL_CMD_LINK_DESTROY
:
531 cfpkt_extr_head(pkt
, &linkid
, 1);
532 cfctrl
->res
.linkdestroy_rsp(cfctrl
->serv
.layer
.up
, linkid
);
534 case CFCTRL_CMD_LINK_ERR
:
535 pr_err("Frame Error Indication received\n");
536 cfctrl
->res
.linkerror_ind();
538 case CFCTRL_CMD_ENUM
:
539 cfctrl
->res
.enum_rsp();
541 case CFCTRL_CMD_SLEEP
:
542 cfctrl
->res
.sleep_rsp();
544 case CFCTRL_CMD_WAKE
:
545 cfctrl
->res
.wake_rsp();
547 case CFCTRL_CMD_LINK_RECONF
:
548 cfctrl
->res
.restart_rsp();
550 case CFCTRL_CMD_RADIO_SET
:
551 cfctrl
->res
.radioset_rsp();
554 pr_err("Unrecognized Control Frame\n");
564 static void cfctrl_ctrlcmd(struct cflayer
*layr
, enum caif_ctrlcmd ctrl
,
567 struct cfctrl
*this = container_obj(layr
);
569 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND
:
570 case CAIF_CTRLCMD_FLOW_OFF_IND
:
571 spin_lock_bh(&this->info_list_lock
);
572 if (!list_empty(&this->list
))
573 pr_debug("Received flow off in control layer\n");
574 spin_unlock_bh(&this->info_list_lock
);
576 case _CAIF_CTRLCMD_PHYIF_DOWN_IND
: {
577 struct cfctrl_request_info
*p
, *tmp
;
579 /* Find all connect request and report failure */
580 spin_lock_bh(&this->info_list_lock
);
581 list_for_each_entry_safe(p
, tmp
, &this->list
, list
) {
582 if (p
->param
.phyid
== phyid
) {
584 p
->client_layer
->ctrlcmd(p
->client_layer
,
585 CAIF_CTRLCMD_INIT_FAIL_RSP
,
590 spin_unlock_bh(&this->info_list_lock
);
599 static int handle_loop(struct cfctrl
*ctrl
, int cmd
, struct cfpkt
*pkt
)
601 static int last_linkid
;
603 u8 linkid
, linktype
, tmp
;
605 case CFCTRL_CMD_LINK_SETUP
:
606 spin_lock_bh(&ctrl
->loop_linkid_lock
);
608 for (linkid
= last_linkid
+ 1; linkid
< 254; linkid
++)
609 if (!ctrl
->loop_linkused
[linkid
])
613 for (linkid
= last_linkid
- 1; linkid
> 1; linkid
--)
614 if (!ctrl
->loop_linkused
[linkid
])
616 spin_unlock_bh(&ctrl
->loop_linkid_lock
);
622 if (!ctrl
->loop_linkused
[linkid
])
623 ctrl
->loop_linkused
[linkid
] = 1;
625 last_linkid
= linkid
;
627 cfpkt_add_trail(pkt
, &linkid
, 1);
628 spin_unlock_bh(&ctrl
->loop_linkid_lock
);
629 cfpkt_peek_head(pkt
, &linktype
, 1);
630 if (linktype
== CFCTRL_SRV_UTIL
) {
632 cfpkt_add_trail(pkt
, &tmp
, 1);
633 cfpkt_add_trail(pkt
, &tmp
, 1);
637 case CFCTRL_CMD_LINK_DESTROY
:
638 spin_lock_bh(&ctrl
->loop_linkid_lock
);
639 cfpkt_peek_head(pkt
, &linkid
, 1);
640 ctrl
->loop_linkused
[linkid
] = 0;
641 spin_unlock_bh(&ctrl
->loop_linkid_lock
);