blob: a1e85f0321081cfd305f3428dc6bdb155aebc40a [file] [log] [blame]
Sjur Braendelandb482cd22010-03-30 13:56:23 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
sjur.brandeland@stericsson.com26ee65e2013-04-22 23:57:01 +00003 * Author: Sjur Brendeland
Sjur Braendelandb482cd22010-03-30 13:56:23 +00004 * 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,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +000023 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,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +000028 int cmd, struct cfpkt *pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +000029#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,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +000075 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{
Kim Lilliestierna XXf315fd32012-06-25 07:49:35 +0000178 struct cfpkt *pkt;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000179 struct cfctrl *cfctrl = container_obj(layer);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000180 struct cflayer *dn = cfctrl->serv.layer.dn;
Kim Lilliestierna XXf315fd32012-06-25 07:49:35 +0000181
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000182 if (!dn) {
183 pr_debug("not able to send enum request\n");
184 return;
185 }
Kim Lilliestierna XXf315fd32012-06-25 07:49:35 +0000186 pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
187 if (!pkt)
188 return;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000189 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
190 init_info(cfpkt_info(pkt), cfctrl);
191 cfpkt_info(pkt)->dev_info->id = physlinkid;
192 cfctrl->serv.dev_info.id = physlinkid;
193 cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM);
194 cfpkt_addbdy(pkt, physlinkid);
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000195 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000196 dn->transmit(dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000197}
198
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000199int cfctrl_linkup_request(struct cflayer *layer,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000200 struct cfctrl_link_param *param,
201 struct cflayer *user_layer)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000202{
203 struct cfctrl *cfctrl = container_obj(layer);
204 u32 tmp32;
205 u16 tmp16;
206 u8 tmp8;
207 struct cfctrl_request_info *req;
208 int ret;
209 char utility_name[16];
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000210 struct cfpkt *pkt;
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000211 struct cflayer *dn = cfctrl->serv.layer.dn;
212
213 if (!dn) {
214 pr_debug("not able to send linkup request\n");
215 return -ENODEV;
216 }
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000217
218 if (cfctrl_cancel_req(layer, user_layer) > 0) {
219 /* Slight Paranoia, check if already connecting */
220 pr_err("Duplicate connect request for same client\n");
221 WARN_ON(1);
222 return -EALREADY;
223 }
224
225 pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
Joe Perches7ac2ed02011-08-25 13:22:24 +0000226 if (!pkt)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000227 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000228 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000229 cfpkt_addbdy(pkt, (param->chtype << 4) | param->linktype);
230 cfpkt_addbdy(pkt, (param->priority << 3) | param->phyid);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000231 cfpkt_addbdy(pkt, param->endpoint & 0x03);
232
233 switch (param->linktype) {
234 case CFCTRL_SRV_VEI:
235 break;
236 case CFCTRL_SRV_VIDEO:
237 cfpkt_addbdy(pkt, (u8) param->u.video.connid);
238 break;
239 case CFCTRL_SRV_DBG:
240 break;
241 case CFCTRL_SRV_DATAGRAM:
242 tmp32 = cpu_to_le32(param->u.datagram.connid);
243 cfpkt_add_body(pkt, &tmp32, 4);
244 break;
245 case CFCTRL_SRV_RFM:
246 /* Construct a frame, convert DatagramConnectionID to network
247 * format long and copy it out...
248 */
249 tmp32 = cpu_to_le32(param->u.rfm.connid);
250 cfpkt_add_body(pkt, &tmp32, 4);
251 /* Add volume name, including zero termination... */
252 cfpkt_add_body(pkt, param->u.rfm.volume,
253 strlen(param->u.rfm.volume) + 1);
254 break;
255 case CFCTRL_SRV_UTIL:
256 tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
257 cfpkt_add_body(pkt, &tmp16, 2);
258 tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
259 cfpkt_add_body(pkt, &tmp16, 2);
260 memset(utility_name, 0, sizeof(utility_name));
Xiongfeng Wang3dc2fa472018-01-08 19:43:00 +0800261 strlcpy(utility_name, param->u.utility.name,
262 UTILITY_NAME_LENGTH);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000263 cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
264 tmp8 = param->u.utility.paramlen;
265 cfpkt_add_body(pkt, &tmp8, 1);
266 cfpkt_add_body(pkt, param->u.utility.params,
267 param->u.utility.paramlen);
268 break;
269 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000270 pr_warn("Request setup of bad link type = %d\n",
271 param->linktype);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000272 return -EINVAL;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000273 }
Julia Lawall49afa552010-05-13 10:03:32 +0000274 req = kzalloc(sizeof(*req), GFP_KERNEL);
Joe Perches7ac2ed02011-08-25 13:22:24 +0000275 if (!req)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000276 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000277 req->client_layer = user_layer;
278 req->cmd = CFCTRL_CMD_LINK_SETUP;
279 req->param = *param;
280 cfctrl_insert_req(cfctrl, req);
281 init_info(cfpkt_info(pkt), cfctrl);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000282 /*
283 * NOTE:Always send linkup and linkdown request on the same
284 * device as the payload. Otherwise old queued up payload
285 * might arrive with the newly allocated channel ID.
286 */
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000287 cfpkt_info(pkt)->dev_info->id = param->phyid;
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000288 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000289 ret =
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000290 dn->transmit(dn, pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000291 if (ret < 0) {
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000292 int count;
293
294 count = cfctrl_cancel_req(&cfctrl->serv.layer,
295 user_layer);
Dave Jones0c1db732013-09-05 00:11:19 -0400296 if (count != 1) {
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000297 pr_err("Could not remove request (%d)", count);
298 return -ENODEV;
Dave Jones0c1db732013-09-05 00:11:19 -0400299 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000300 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000301 return 0;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000302}
303
304int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000305 struct cflayer *client)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000306{
307 int ret;
Kim Lilliestierna XXf315fd32012-06-25 07:49:35 +0000308 struct cfpkt *pkt;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000309 struct cfctrl *cfctrl = container_obj(layer);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000310 struct cflayer *dn = cfctrl->serv.layer.dn;
311
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000312 if (!dn) {
313 pr_debug("not able to send link-down request\n");
314 return -ENODEV;
315 }
Kim Lilliestierna XXf315fd32012-06-25 07:49:35 +0000316 pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
317 if (!pkt)
318 return -ENOMEM;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000319 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
320 cfpkt_addbdy(pkt, channelid);
321 init_info(cfpkt_info(pkt), cfctrl);
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000322 cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000323 ret =
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000324 dn->transmit(dn, pkt);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000325#ifndef CAIF_NO_LOOP
326 cfctrl->loop_linkused[channelid] = 0;
327#endif
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000328 return ret;
329}
330
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000331int cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000332{
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000333 struct cfctrl_request_info *p, *tmp;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000334 struct cfctrl *ctrl = container_obj(layr);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000335 int found = 0;
336 spin_lock_bh(&ctrl->info_list_lock);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000337
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000338 list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
339 if (p->client_layer == adap_layer) {
Sjur Braendeland7aecf492010-05-21 02:16:08 +0000340 list_del(&p->list);
341 kfree(p);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000342 found++;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000343 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000344 }
345
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000346 spin_unlock_bh(&ctrl->info_list_lock);
347 return found;
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000348}
349
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000350static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
351{
352 u8 cmdrsp;
353 u8 cmd;
354 int ret = -1;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000355 u8 len;
356 u8 param[255];
Arnd Bergmannce628962018-01-16 17:34:00 +0100357 u8 linkid = 0;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000358 struct cfctrl *cfctrl = container_obj(layer);
359 struct cfctrl_request_info rsp, *req;
360
361
Arnd Bergmannce628962018-01-16 17:34:00 +0100362 cmdrsp = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000363 cmd = cmdrsp & CFCTRL_CMD_MASK;
364 if (cmd != CFCTRL_CMD_LINK_ERR
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000365 && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)
366 && CFCTRL_ERR_BIT != (CFCTRL_ERR_BIT & cmdrsp)) {
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000367 if (handle_loop(cfctrl, cmd, pkt) != 0)
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000368 cmdrsp |= CFCTRL_ERR_BIT;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000369 }
370
371 switch (cmd) {
372 case CFCTRL_CMD_LINK_SETUP:
373 {
374 enum cfctrl_srv serv;
375 enum cfctrl_srv servtype;
376 u8 endpoint;
377 u8 physlinkid;
378 u8 prio;
379 u8 tmp;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000380 u8 *cp;
381 int i;
382 struct cfctrl_link_param linkparam;
383 memset(&linkparam, 0, sizeof(linkparam));
384
Arnd Bergmannce628962018-01-16 17:34:00 +0100385 tmp = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000386
387 serv = tmp & CFCTRL_SRV_MASK;
388 linkparam.linktype = serv;
389
390 servtype = tmp >> 4;
391 linkparam.chtype = servtype;
392
Arnd Bergmannce628962018-01-16 17:34:00 +0100393 tmp = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000394 physlinkid = tmp & 0x07;
395 prio = tmp >> 3;
396
397 linkparam.priority = prio;
398 linkparam.phyid = physlinkid;
Arnd Bergmannce628962018-01-16 17:34:00 +0100399 endpoint = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000400 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 */
Arnd Bergmannce628962018-01-16 17:34:00 +0100408 linkid = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000409 break;
410 case CFCTRL_SRV_VIDEO:
Arnd Bergmannce628962018-01-16 17:34:00 +0100411 tmp = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000412 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 */
Arnd Bergmannce628962018-01-16 17:34:00 +0100416 linkid = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000417 break;
418
419 case CFCTRL_SRV_DATAGRAM:
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000420 linkparam.u.datagram.connid =
Arnd Bergmannce628962018-01-16 17:34:00 +0100421 cfpkt_extr_head_u32(pkt);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000422 if (CFCTRL_ERR_BIT & cmdrsp)
423 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000424 /* Link ID */
Arnd Bergmannce628962018-01-16 17:34:00 +0100425 linkid = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000426 break;
427 case CFCTRL_SRV_RFM:
428 /* Construct a frame, convert
429 * DatagramConnectionID
430 * to network format long and copy it out...
431 */
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000432 linkparam.u.rfm.connid =
Arnd Bergmannce628962018-01-16 17:34:00 +0100433 cfpkt_extr_head_u32(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000434 cp = (u8 *) linkparam.u.rfm.volume;
Arnd Bergmannce628962018-01-16 17:34:00 +0100435 for (tmp = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000436 cfpkt_more(pkt) && tmp != '\0';
Arnd Bergmannce628962018-01-16 17:34:00 +0100437 tmp = cfpkt_extr_head_u8(pkt))
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000438 *cp++ = tmp;
439 *cp = '\0';
440
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000441 if (CFCTRL_ERR_BIT & cmdrsp)
442 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000443 /* Link ID */
Arnd Bergmannce628962018-01-16 17:34:00 +0100444 linkid = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000445
446 break;
447 case CFCTRL_SRV_UTIL:
448 /* Construct a frame, convert
449 * DatagramConnectionID
450 * to network format long and copy it out...
451 */
452 /* Fifosize KB */
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000453 linkparam.u.utility.fifosize_kb =
Arnd Bergmannce628962018-01-16 17:34:00 +0100454 cfpkt_extr_head_u16(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000455 /* Fifosize bufs */
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000456 linkparam.u.utility.fifosize_bufs =
Arnd Bergmannce628962018-01-16 17:34:00 +0100457 cfpkt_extr_head_u16(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000458 /* name */
459 cp = (u8 *) linkparam.u.utility.name;
460 caif_assert(sizeof(linkparam.u.utility.name)
461 >= UTILITY_NAME_LENGTH);
462 for (i = 0;
463 i < UTILITY_NAME_LENGTH
464 && cfpkt_more(pkt); i++) {
Arnd Bergmannce628962018-01-16 17:34:00 +0100465 tmp = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000466 *cp++ = tmp;
467 }
468 /* Length */
Arnd Bergmannce628962018-01-16 17:34:00 +0100469 len = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000470 linkparam.u.utility.paramlen = len;
471 /* Param Data */
472 cp = linkparam.u.utility.params;
473 while (cfpkt_more(pkt) && len--) {
Arnd Bergmannce628962018-01-16 17:34:00 +0100474 tmp = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000475 *cp++ = tmp;
476 }
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000477 if (CFCTRL_ERR_BIT & cmdrsp)
478 break;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000479 /* Link ID */
Arnd Bergmannce628962018-01-16 17:34:00 +0100480 linkid = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000481 /* Length */
Arnd Bergmannce628962018-01-16 17:34:00 +0100482 len = cfpkt_extr_head_u8(pkt);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000483 /* Param Data */
484 cfpkt_extr_head(pkt, &param, len);
485 break;
486 default:
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000487 pr_warn("Request setup, invalid type (%d)\n",
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000488 serv);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000489 goto error;
490 }
491
492 rsp.cmd = cmd;
493 rsp.param = linkparam;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000494 spin_lock_bh(&cfctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000495 req = cfctrl_remove_req(cfctrl, &rsp);
496
497 if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
498 cfpkt_erroneous(pkt)) {
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000499 pr_err("Invalid O/E bit or parse error "
500 "on CAIF control channel\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000501 cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
502 0,
503 req ? req->client_layer
504 : NULL);
505 } else {
506 cfctrl->res.linksetup_rsp(cfctrl->serv.
507 layer.up, linkid,
508 serv, physlinkid,
509 req ? req->
510 client_layer : NULL);
511 }
512
Sachin Kamat973b1b92012-11-20 00:53:58 +0000513 kfree(req);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000514
515 spin_unlock_bh(&cfctrl->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000516 }
517 break;
518 case CFCTRL_CMD_LINK_DESTROY:
Arnd Bergmannce628962018-01-16 17:34:00 +0100519 linkid = cfpkt_extr_head_u8(pkt);
Sjur Braendeland8d545c82010-04-28 08:54:37 +0000520 cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000521 break;
522 case CFCTRL_CMD_LINK_ERR:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000523 pr_err("Frame Error Indication received\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000524 cfctrl->res.linkerror_ind();
525 break;
526 case CFCTRL_CMD_ENUM:
527 cfctrl->res.enum_rsp();
528 break;
529 case CFCTRL_CMD_SLEEP:
530 cfctrl->res.sleep_rsp();
531 break;
532 case CFCTRL_CMD_WAKE:
533 cfctrl->res.wake_rsp();
534 break;
535 case CFCTRL_CMD_LINK_RECONF:
536 cfctrl->res.restart_rsp();
537 break;
538 case CFCTRL_CMD_RADIO_SET:
539 cfctrl->res.radioset_rsp();
540 break;
541 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000542 pr_err("Unrecognized Control Frame\n");
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000543 goto error;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000544 }
545 ret = 0;
546error:
547 cfpkt_destroy(pkt);
548 return ret;
549}
550
551static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000552 int phyid)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000553{
554 struct cfctrl *this = container_obj(layr);
555 switch (ctrl) {
556 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
557 case CAIF_CTRLCMD_FLOW_OFF_IND:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000558 spin_lock_bh(&this->info_list_lock);
sjur.brandeland@stericsson.com0e5a1172011-05-22 11:18:50 +0000559 if (!list_empty(&this->list))
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000560 pr_debug("Received flow off in control layer\n");
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000561 spin_unlock_bh(&this->info_list_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000562 break;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000563 case _CAIF_CTRLCMD_PHYIF_DOWN_IND: {
564 struct cfctrl_request_info *p, *tmp;
565
566 /* Find all connect request and report failure */
567 spin_lock_bh(&this->info_list_lock);
568 list_for_each_entry_safe(p, tmp, &this->list, list) {
569 if (p->param.phyid == phyid) {
570 list_del(&p->list);
571 p->client_layer->ctrlcmd(p->client_layer,
572 CAIF_CTRLCMD_INIT_FAIL_RSP,
573 phyid);
574 kfree(p);
575 }
576 }
577 spin_unlock_bh(&this->info_list_lock);
578 break;
579 }
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000580 default:
581 break;
582 }
583}
584
585#ifndef CAIF_NO_LOOP
586static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
587{
588 static int last_linkid;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000589 static int dec;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000590 u8 linkid, linktype, tmp;
591 switch (cmd) {
592 case CFCTRL_CMD_LINK_SETUP:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000593 spin_lock_bh(&ctrl->loop_linkid_lock);
594 if (!dec) {
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000595 for (linkid = last_linkid + 1; linkid < 254; linkid++)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000596 if (!ctrl->loop_linkused[linkid])
597 goto found;
598 }
599 dec = 1;
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000600 for (linkid = last_linkid - 1; linkid > 1; linkid--)
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000601 if (!ctrl->loop_linkused[linkid])
602 goto found;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000603 spin_unlock_bh(&ctrl->loop_linkid_lock);
sjur.brandeland@stericsson.com96796ea2011-05-22 11:18:52 +0000604 return -1;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000605found:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000606 if (linkid < 10)
607 dec = 0;
608
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000609 if (!ctrl->loop_linkused[linkid])
610 ctrl->loop_linkused[linkid] = 1;
611
612 last_linkid = linkid;
613
614 cfpkt_add_trail(pkt, &linkid, 1);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000615 spin_unlock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000616 cfpkt_peek_head(pkt, &linktype, 1);
617 if (linktype == CFCTRL_SRV_UTIL) {
618 tmp = 0x01;
619 cfpkt_add_trail(pkt, &tmp, 1);
620 cfpkt_add_trail(pkt, &tmp, 1);
621 }
622 break;
623
624 case CFCTRL_CMD_LINK_DESTROY:
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000625 spin_lock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000626 cfpkt_peek_head(pkt, &linkid, 1);
627 ctrl->loop_linkused[linkid] = 0;
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000628 spin_unlock_bh(&ctrl->loop_linkid_lock);
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000629 break;
630 default:
631 break;
632 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000633 return 0;
Sjur Braendelandb482cd22010-03-30 13:56:23 +0000634}
635#endif