cfctrl.c 15.9 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) ST-Ericsson AB 2010
 * Author:	Sjur Brendeland/sjur.brandeland@stericsson.com
 * License terms: GNU General Public License (GPL) version 2
 */

J
Joe Perches 已提交
7 8
#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <linux/stddef.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <net/caif/caif_layer.h>
#include <net/caif/cfpkt.h>
#include <net/caif/cfctrl.h>

#define container_obj(layr) container_of(layr, struct cfctrl, serv.layer)
#define UTILITY_NAME_LENGTH 16
#define CFPKT_CTRL_PKT_LEN 20


#ifdef CAIF_NO_LOOP
static int handle_loop(struct cfctrl *ctrl,
			      int cmd, struct cfpkt *pkt){
24
	return -1;
25 26 27 28 29 30 31 32 33 34 35 36
}
#else
static int handle_loop(struct cfctrl *ctrl,
		int cmd, struct cfpkt *pkt);
#endif
static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt);
static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
			   int phyid);


struct cflayer *cfctrl_create(void)
{
37
	struct dev_info dev_info;
38 39 40
	struct cfctrl *this =
		kmalloc(sizeof(struct cfctrl), GFP_ATOMIC);
	if (!this) {
J
Joe Perches 已提交
41
		pr_warn("Out of memory\n");
42 43 44
		return NULL;
	}
	caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
45 46
	memset(&dev_info, 0, sizeof(dev_info));
	dev_info.id = 0xff;
47
	memset(this, 0, sizeof(*this));
48
	cfsrvl_init(&this->serv, 0, &dev_info, false);
49 50 51 52 53 54
	atomic_set(&this->req_seq_no, 1);
	atomic_set(&this->rsp_seq_no, 1);
	this->serv.layer.receive = cfctrl_recv;
	sprintf(this->serv.layer.name, "ctrl");
	this->serv.layer.ctrlcmd = cfctrl_ctrlcmd;
	spin_lock_init(&this->loop_linkid_lock);
55 56
	spin_lock_init(&this->info_list_lock);
	INIT_LIST_HEAD(&this->list);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	this->loop_linkid = 1;
	return &this->serv.layer;
}

static bool param_eq(struct cfctrl_link_param *p1, struct cfctrl_link_param *p2)
{
	bool eq =
	    p1->linktype == p2->linktype &&
	    p1->priority == p2->priority &&
	    p1->phyid == p2->phyid &&
	    p1->endpoint == p2->endpoint && p1->chtype == p2->chtype;

	if (!eq)
		return false;

	switch (p1->linktype) {
	case CFCTRL_SRV_VEI:
		return true;
	case CFCTRL_SRV_DATAGRAM:
		return p1->u.datagram.connid == p2->u.datagram.connid;
	case CFCTRL_SRV_RFM:
		return
		    p1->u.rfm.connid == p2->u.rfm.connid &&
		    strcmp(p1->u.rfm.volume, p2->u.rfm.volume) == 0;
	case CFCTRL_SRV_UTIL:
		return
		    p1->u.utility.fifosize_kb == p2->u.utility.fifosize_kb
		    && p1->u.utility.fifosize_bufs ==
		    p2->u.utility.fifosize_bufs
		    && strcmp(p1->u.utility.name, p2->u.utility.name) == 0
		    && p1->u.utility.paramlen == p2->u.utility.paramlen
		    && memcmp(p1->u.utility.params, p2->u.utility.params,
			      p1->u.utility.paramlen) == 0;

	case CFCTRL_SRV_VIDEO:
		return p1->u.video.connid == p2->u.video.connid;
	case CFCTRL_SRV_DBG:
		return true;
	case CFCTRL_SRV_DECM:
		return false;
	default:
		return false;
	}
	return false;
}

bool cfctrl_req_eq(struct cfctrl_request_info *r1,
		   struct cfctrl_request_info *r2)
{
	if (r1->cmd != r2->cmd)
		return false;
	if (r1->cmd == CFCTRL_CMD_LINK_SETUP)
		return param_eq(&r1->param, &r2->param);
	else
		return r1->channel_id == r2->channel_id;
}

/* Insert request at the end */
void cfctrl_insert_req(struct cfctrl *ctrl,
			      struct cfctrl_request_info *req)
{
	spin_lock(&ctrl->info_list_lock);
	atomic_inc(&ctrl->req_seq_no);
	req->sequence_no = atomic_read(&ctrl->req_seq_no);
121
	list_add_tail(&req->list, &ctrl->list);
122 123 124 125 126 127 128
	spin_unlock(&ctrl->info_list_lock);
}

/* Compare and remove request */
struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl,
					      struct cfctrl_request_info *req)
{
129
	struct cfctrl_request_info *p, *tmp, *first;
130 131

	spin_lock(&ctrl->info_list_lock);
132
	first = list_first_entry(&ctrl->list, struct cfctrl_request_info, list);
133

134 135 136
	list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
		if (cfctrl_req_eq(req, p)) {
			if (p != first)
J
Joe Perches 已提交
137
				pr_warn("Requests are not received in order\n");
138

139
			atomic_set(&ctrl->rsp_seq_no,
140 141 142
					 p->sequence_no);
			list_del(&p->list);
			goto out;
143 144
		}
	}
145 146
	p = NULL;
out:
147
	spin_unlock(&ctrl->info_list_lock);
148
	return p;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
}

struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer)
{
	struct cfctrl *this = container_obj(layer);
	return &this->res;
}

void cfctrl_set_dnlayer(struct cflayer *this, struct cflayer *dn)
{
	this->dn = dn;
}

void cfctrl_set_uplayer(struct cflayer *this, struct cflayer *up)
{
	this->up = up;
}

static void init_info(struct caif_payload_info *info, struct cfctrl *cfctrl)
{
	info->hdr_len = 0;
	info->channel_id = cfctrl->serv.layer.id;
	info->dev_info = &cfctrl->serv.dev_info;
}

void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid)
{
	struct cfctrl *cfctrl = container_obj(layer);
	int ret;
	struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
	if (!pkt) {
J
Joe Perches 已提交
180
		pr_warn("Out of memory\n");
181 182 183 184 185 186 187 188 189 190 191
		return;
	}
	caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
	init_info(cfpkt_info(pkt), cfctrl);
	cfpkt_info(pkt)->dev_info->id = physlinkid;
	cfctrl->serv.dev_info.id = physlinkid;
	cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM);
	cfpkt_addbdy(pkt, physlinkid);
	ret =
	    cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
	if (ret < 0) {
J
Joe Perches 已提交
192
		pr_err("Could not transmit enum message\n");
193 194 195 196
		cfpkt_destroy(pkt);
	}
}

197
int cfctrl_linkup_request(struct cflayer *layer,
198 199 200 201 202 203 204 205 206 207 208 209
			   struct cfctrl_link_param *param,
			   struct cflayer *user_layer)
{
	struct cfctrl *cfctrl = container_obj(layer);
	u32 tmp32;
	u16 tmp16;
	u8 tmp8;
	struct cfctrl_request_info *req;
	int ret;
	char utility_name[16];
	struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
	if (!pkt) {
J
Joe Perches 已提交
210
		pr_warn("Out of memory\n");
211
		return -ENOMEM;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	}
	cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
	cfpkt_addbdy(pkt, (param->chtype << 4) + param->linktype);
	cfpkt_addbdy(pkt, (param->priority << 3) + param->phyid);
	cfpkt_addbdy(pkt, param->endpoint & 0x03);

	switch (param->linktype) {
	case CFCTRL_SRV_VEI:
		break;
	case CFCTRL_SRV_VIDEO:
		cfpkt_addbdy(pkt, (u8) param->u.video.connid);
		break;
	case CFCTRL_SRV_DBG:
		break;
	case CFCTRL_SRV_DATAGRAM:
		tmp32 = cpu_to_le32(param->u.datagram.connid);
		cfpkt_add_body(pkt, &tmp32, 4);
		break;
	case CFCTRL_SRV_RFM:
		/* Construct a frame, convert DatagramConnectionID to network
		 * format long and copy it out...
		 */
		tmp32 = cpu_to_le32(param->u.rfm.connid);
		cfpkt_add_body(pkt, &tmp32, 4);
		/* Add volume name, including zero termination... */
		cfpkt_add_body(pkt, param->u.rfm.volume,
			       strlen(param->u.rfm.volume) + 1);
		break;
	case CFCTRL_SRV_UTIL:
		tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
		cfpkt_add_body(pkt, &tmp16, 2);
		tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
		cfpkt_add_body(pkt, &tmp16, 2);
		memset(utility_name, 0, sizeof(utility_name));
		strncpy(utility_name, param->u.utility.name,
			UTILITY_NAME_LENGTH - 1);
		cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
		tmp8 = param->u.utility.paramlen;
		cfpkt_add_body(pkt, &tmp8, 1);
		cfpkt_add_body(pkt, param->u.utility.params,
			       param->u.utility.paramlen);
		break;
	default:
J
Joe Perches 已提交
255 256
		pr_warn("Request setup of bad link type = %d\n",
			param->linktype);
257
		return -EINVAL;
258
	}
J
Julia Lawall 已提交
259
	req = kzalloc(sizeof(*req), GFP_KERNEL);
260
	if (!req) {
J
Joe Perches 已提交
261
		pr_warn("Out of memory\n");
262
		return -ENOMEM;
263 264 265 266 267 268
	}
	req->client_layer = user_layer;
	req->cmd = CFCTRL_CMD_LINK_SETUP;
	req->param = *param;
	cfctrl_insert_req(cfctrl, req);
	init_info(cfpkt_info(pkt), cfctrl);
269 270 271 272 273
	/*
	 * NOTE:Always send linkup and linkdown request on the same
	 *	device as the payload. Otherwise old queued up payload
	 *	might arrive with the newly allocated channel ID.
	 */
274 275 276 277
	cfpkt_info(pkt)->dev_info->id = param->phyid;
	ret =
	    cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
	if (ret < 0) {
J
Joe Perches 已提交
278
		pr_err("Could not transmit linksetup request\n");
279
		cfpkt_destroy(pkt);
280
		return -ENODEV;
281
	}
282
	return 0;
283 284 285 286 287 288 289 290 291
}

int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
				struct cflayer *client)
{
	int ret;
	struct cfctrl *cfctrl = container_obj(layer);
	struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
	if (!pkt) {
J
Joe Perches 已提交
292
		pr_warn("Out of memory\n");
293 294 295 296 297 298 299 300
		return -ENOMEM;
	}
	cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
	cfpkt_addbdy(pkt, channelid);
	init_info(cfpkt_info(pkt), cfctrl);
	ret =
	    cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
	if (ret < 0) {
J
Joe Perches 已提交
301
		pr_err("Could not transmit link-down request\n");
302 303 304 305 306 307 308 309 310 311 312
		cfpkt_destroy(pkt);
	}
	return ret;
}

void cfctrl_sleep_req(struct cflayer *layer)
{
	int ret;
	struct cfctrl *cfctrl = container_obj(layer);
	struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
	if (!pkt) {
J
Joe Perches 已提交
313
		pr_warn("Out of memory\n");
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		return;
	}
	cfpkt_addbdy(pkt, CFCTRL_CMD_SLEEP);
	init_info(cfpkt_info(pkt), cfctrl);
	ret =
	    cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
	if (ret < 0)
		cfpkt_destroy(pkt);
}

void cfctrl_wake_req(struct cflayer *layer)
{
	int ret;
	struct cfctrl *cfctrl = container_obj(layer);
	struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
	if (!pkt) {
J
Joe Perches 已提交
330
		pr_warn("Out of memory\n");
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		return;
	}
	cfpkt_addbdy(pkt, CFCTRL_CMD_WAKE);
	init_info(cfpkt_info(pkt), cfctrl);
	ret =
	    cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
	if (ret < 0)
		cfpkt_destroy(pkt);
}

void cfctrl_getstartreason_req(struct cflayer *layer)
{
	int ret;
	struct cfctrl *cfctrl = container_obj(layer);
	struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
	if (!pkt) {
J
Joe Perches 已提交
347
		pr_warn("Out of memory\n");
348 349 350 351 352 353 354 355 356 357 358
		return;
	}
	cfpkt_addbdy(pkt, CFCTRL_CMD_START_REASON);
	init_info(cfpkt_info(pkt), cfctrl);
	ret =
	    cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
	if (ret < 0)
		cfpkt_destroy(pkt);
}


359 360
void cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
{
361
	struct cfctrl_request_info *p, *tmp;
362 363
	struct cfctrl *ctrl = container_obj(layr);
	spin_lock(&ctrl->info_list_lock);
364 365 366

	list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
		if (p->client_layer == adap_layer) {
367
			pr_debug("cancel req :%d\n", p->sequence_no);
368 369
			list_del(&p->list);
			kfree(p);
370 371 372 373 374 375
		}
	}

	spin_unlock(&ctrl->info_list_lock);
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
{
	u8 cmdrsp;
	u8 cmd;
	int ret = -1;
	u16 tmp16;
	u8 len;
	u8 param[255];
	u8 linkid;
	struct cfctrl *cfctrl = container_obj(layer);
	struct cfctrl_request_info rsp, *req;


	cfpkt_extr_head(pkt, &cmdrsp, 1);
	cmd = cmdrsp & CFCTRL_CMD_MASK;
	if (cmd != CFCTRL_CMD_LINK_ERR
	    && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)) {
393
		if (handle_loop(cfctrl, cmd, pkt) != 0)
394
			cmdrsp |= CFCTRL_ERR_BIT;
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	}

	switch (cmd) {
	case CFCTRL_CMD_LINK_SETUP:
		{
			enum cfctrl_srv serv;
			enum cfctrl_srv servtype;
			u8 endpoint;
			u8 physlinkid;
			u8 prio;
			u8 tmp;
			u32 tmp32;
			u8 *cp;
			int i;
			struct cfctrl_link_param linkparam;
			memset(&linkparam, 0, sizeof(linkparam));

			cfpkt_extr_head(pkt, &tmp, 1);

			serv = tmp & CFCTRL_SRV_MASK;
			linkparam.linktype = serv;

			servtype = tmp >> 4;
			linkparam.chtype = servtype;

			cfpkt_extr_head(pkt, &tmp, 1);
			physlinkid = tmp & 0x07;
			prio = tmp >> 3;

			linkparam.priority = prio;
			linkparam.phyid = physlinkid;
			cfpkt_extr_head(pkt, &endpoint, 1);
			linkparam.endpoint = endpoint & 0x03;

			switch (serv) {
			case CFCTRL_SRV_VEI:
			case CFCTRL_SRV_DBG:
432 433
				if (CFCTRL_ERR_BIT & cmdrsp)
					break;
434 435 436 437 438 439
				/* Link ID */
				cfpkt_extr_head(pkt, &linkid, 1);
				break;
			case CFCTRL_SRV_VIDEO:
				cfpkt_extr_head(pkt, &tmp, 1);
				linkparam.u.video.connid = tmp;
440 441
				if (CFCTRL_ERR_BIT & cmdrsp)
					break;
442 443 444 445 446 447 448 449
				/* Link ID */
				cfpkt_extr_head(pkt, &linkid, 1);
				break;

			case CFCTRL_SRV_DATAGRAM:
				cfpkt_extr_head(pkt, &tmp32, 4);
				linkparam.u.datagram.connid =
				    le32_to_cpu(tmp32);
450 451
				if (CFCTRL_ERR_BIT & cmdrsp)
					break;
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
				/* Link ID */
				cfpkt_extr_head(pkt, &linkid, 1);
				break;
			case CFCTRL_SRV_RFM:
				/* Construct a frame, convert
				 * DatagramConnectionID
				 * to network format long and copy it out...
				 */
				cfpkt_extr_head(pkt, &tmp32, 4);
				linkparam.u.rfm.connid =
				  le32_to_cpu(tmp32);
				cp = (u8 *) linkparam.u.rfm.volume;
				for (cfpkt_extr_head(pkt, &tmp, 1);
				     cfpkt_more(pkt) && tmp != '\0';
				     cfpkt_extr_head(pkt, &tmp, 1))
					*cp++ = tmp;
				*cp = '\0';

470 471
				if (CFCTRL_ERR_BIT & cmdrsp)
					break;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
				/* Link ID */
				cfpkt_extr_head(pkt, &linkid, 1);

				break;
			case CFCTRL_SRV_UTIL:
				/* Construct a frame, convert
				 * DatagramConnectionID
				 * to network format long and copy it out...
				 */
				/* Fifosize KB */
				cfpkt_extr_head(pkt, &tmp16, 2);
				linkparam.u.utility.fifosize_kb =
				    le16_to_cpu(tmp16);
				/* Fifosize bufs */
				cfpkt_extr_head(pkt, &tmp16, 2);
				linkparam.u.utility.fifosize_bufs =
				    le16_to_cpu(tmp16);
				/* name */
				cp = (u8 *) linkparam.u.utility.name;
				caif_assert(sizeof(linkparam.u.utility.name)
					     >= UTILITY_NAME_LENGTH);
				for (i = 0;
				     i < UTILITY_NAME_LENGTH
				     && cfpkt_more(pkt); i++) {
					cfpkt_extr_head(pkt, &tmp, 1);
					*cp++ = tmp;
				}
				/* Length */
				cfpkt_extr_head(pkt, &len, 1);
				linkparam.u.utility.paramlen = len;
				/* Param Data */
				cp = linkparam.u.utility.params;
				while (cfpkt_more(pkt) && len--) {
					cfpkt_extr_head(pkt, &tmp, 1);
					*cp++ = tmp;
				}
508 509
				if (CFCTRL_ERR_BIT & cmdrsp)
					break;
510 511 512 513 514 515 516 517
				/* Link ID */
				cfpkt_extr_head(pkt, &linkid, 1);
				/* Length */
				cfpkt_extr_head(pkt, &len, 1);
				/* Param Data */
				cfpkt_extr_head(pkt, &param, len);
				break;
			default:
J
Joe Perches 已提交
518 519
				pr_warn("Request setup - invalid link type (%d)\n",
					serv);
520 521 522 523 524 525 526 527 528
				goto error;
			}

			rsp.cmd = cmd;
			rsp.param = linkparam;
			req = cfctrl_remove_req(cfctrl, &rsp);

			if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
				cfpkt_erroneous(pkt)) {
J
Joe Perches 已提交
529
				pr_err("Invalid O/E bit or parse error on CAIF control channel\n");
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
				cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
						       0,
						       req ? req->client_layer
						       : NULL);
			} else {
				cfctrl->res.linksetup_rsp(cfctrl->serv.
							  layer.up, linkid,
							  serv, physlinkid,
							  req ? req->
							  client_layer : NULL);
			}

			if (req != NULL)
				kfree(req);
		}
		break;
	case CFCTRL_CMD_LINK_DESTROY:
		cfpkt_extr_head(pkt, &linkid, 1);
548
		cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
549 550
		break;
	case CFCTRL_CMD_LINK_ERR:
J
Joe Perches 已提交
551
		pr_err("Frame Error Indication received\n");
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
		cfctrl->res.linkerror_ind();
		break;
	case CFCTRL_CMD_ENUM:
		cfctrl->res.enum_rsp();
		break;
	case CFCTRL_CMD_SLEEP:
		cfctrl->res.sleep_rsp();
		break;
	case CFCTRL_CMD_WAKE:
		cfctrl->res.wake_rsp();
		break;
	case CFCTRL_CMD_LINK_RECONF:
		cfctrl->res.restart_rsp();
		break;
	case CFCTRL_CMD_RADIO_SET:
		cfctrl->res.radioset_rsp();
		break;
	default:
J
Joe Perches 已提交
570
		pr_err("Unrecognized Control Frame\n");
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
		goto error;
		break;
	}
	ret = 0;
error:
	cfpkt_destroy(pkt);
	return ret;
}

static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
			int phyid)
{
	struct cfctrl *this = container_obj(layr);
	switch (ctrl) {
	case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
	case CAIF_CTRLCMD_FLOW_OFF_IND:
		spin_lock(&this->info_list_lock);
588
		if (!list_empty(&this->list)) {
J
Joe Perches 已提交
589
			pr_debug("Received flow off in control layer\n");
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
		}
		spin_unlock(&this->info_list_lock);
		break;
	default:
		break;
	}
}

#ifndef CAIF_NO_LOOP
static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
{
	static int last_linkid;
	u8 linkid, linktype, tmp;
	switch (cmd) {
	case CFCTRL_CMD_LINK_SETUP:
		spin_lock(&ctrl->loop_linkid_lock);
		for (linkid = last_linkid + 1; linkid < 255; linkid++)
			if (!ctrl->loop_linkused[linkid])
				goto found;
		for (linkid = last_linkid - 1; linkid > 0; linkid--)
			if (!ctrl->loop_linkused[linkid])
				goto found;
		spin_unlock(&ctrl->loop_linkid_lock);
J
Joe Perches 已提交
613
		pr_err("Out of link-ids\n");
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
		return -EINVAL;
found:
		if (!ctrl->loop_linkused[linkid])
			ctrl->loop_linkused[linkid] = 1;

		last_linkid = linkid;

		cfpkt_add_trail(pkt, &linkid, 1);
		spin_unlock(&ctrl->loop_linkid_lock);
		cfpkt_peek_head(pkt, &linktype, 1);
		if (linktype ==  CFCTRL_SRV_UTIL) {
			tmp = 0x01;
			cfpkt_add_trail(pkt, &tmp, 1);
			cfpkt_add_trail(pkt, &tmp, 1);
		}
		break;

	case CFCTRL_CMD_LINK_DESTROY:
		spin_lock(&ctrl->loop_linkid_lock);
		cfpkt_peek_head(pkt, &linkid, 1);
		ctrl->loop_linkused[linkid] = 0;
		spin_unlock(&ctrl->loop_linkid_lock);
		break;
	default:
		break;
	}
640
	return 0;
641 642
}
#endif