layer2.c 51.9 KB
Newer Older
Karsten Keil's avatar
Karsten Keil committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *
 * Author	Karsten Keil <kkeil@novell.com>
 *
 * Copyright 2008  by Karsten Keil <kkeil@novell.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

18
#include <linux/mISDNif.h>
19
#include <linux/slab.h>
20
#include "core.h"
Karsten Keil's avatar
Karsten Keil committed
21 22 23
#include "fsm.h"
#include "layer2.h"

24
static u_int *debug;
Karsten Keil's avatar
Karsten Keil committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

static
struct Fsm l2fsm = {NULL, 0, 0, NULL, NULL};

static char *strL2State[] =
{
	"ST_L2_1",
	"ST_L2_2",
	"ST_L2_3",
	"ST_L2_4",
	"ST_L2_5",
	"ST_L2_6",
	"ST_L2_7",
	"ST_L2_8",
};

enum {
	EV_L2_UI,
	EV_L2_SABME,
	EV_L2_DISC,
	EV_L2_DM,
	EV_L2_UA,
	EV_L2_FRMR,
	EV_L2_SUPER,
	EV_L2_I,
	EV_L2_DL_DATA,
	EV_L2_ACK_PULL,
	EV_L2_DL_UNITDATA,
	EV_L2_DL_ESTABLISH_REQ,
	EV_L2_DL_RELEASE_REQ,
	EV_L2_MDL_ASSIGN,
	EV_L2_MDL_REMOVE,
	EV_L2_MDL_ERROR,
	EV_L1_DEACTIVATE,
	EV_L2_T200,
	EV_L2_T203,
61 62
	EV_L2_T200I,
	EV_L2_T203I,
Karsten Keil's avatar
Karsten Keil committed
63 64 65 66 67
	EV_L2_SET_OWN_BUSY,
	EV_L2_CLEAR_OWN_BUSY,
	EV_L2_FRAME_ERROR,
};

68
#define L2_EVENT_COUNT (EV_L2_FRAME_ERROR + 1)
Karsten Keil's avatar
Karsten Keil committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

static char *strL2Event[] =
{
	"EV_L2_UI",
	"EV_L2_SABME",
	"EV_L2_DISC",
	"EV_L2_DM",
	"EV_L2_UA",
	"EV_L2_FRMR",
	"EV_L2_SUPER",
	"EV_L2_I",
	"EV_L2_DL_DATA",
	"EV_L2_ACK_PULL",
	"EV_L2_DL_UNITDATA",
	"EV_L2_DL_ESTABLISH_REQ",
	"EV_L2_DL_RELEASE_REQ",
	"EV_L2_MDL_ASSIGN",
	"EV_L2_MDL_REMOVE",
	"EV_L2_MDL_ERROR",
	"EV_L1_DEACTIVATE",
	"EV_L2_T200",
	"EV_L2_T203",
91 92
	"EV_L2_T200I",
	"EV_L2_T203I",
Karsten Keil's avatar
Karsten Keil committed
93 94 95 96 97 98 99 100 101
	"EV_L2_SET_OWN_BUSY",
	"EV_L2_CLEAR_OWN_BUSY",
	"EV_L2_FRAME_ERROR",
};

static void
l2m_debug(struct FsmInst *fi, char *fmt, ...)
{
	struct layer2 *l2 = fi->userdata;
102
	struct va_format vaf;
Karsten Keil's avatar
Karsten Keil committed
103 104 105 106
	va_list va;

	if (!(*debug & DEBUG_L2_FSM))
		return;
107

Karsten Keil's avatar
Karsten Keil committed
108
	va_start(va, fmt);
109 110 111 112

	vaf.fmt = fmt;
	vaf.va = &va;

113 114
	printk(KERN_DEBUG "%s l2 (sapi %d tei %d): %pV\n",
	       mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei, &vaf);
115

Karsten Keil's avatar
Karsten Keil committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	va_end(va);
}

inline u_int
l2headersize(struct layer2 *l2, int ui)
{
	return ((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
		(test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
}

inline u_int
l2addrsize(struct layer2 *l2)
{
	return test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
}

static u_int
l2_newid(struct layer2 *l2)
{
	u_int	id;

	id = l2->next_id++;
	if (id == 0x7fff)
		l2->next_id = 1;
	id <<= 16;
	id |= l2->tei << 8;
	id |= l2->sapi;
	return id;
}

static void
l2up(struct layer2 *l2, u_int prim, struct sk_buff *skb)
{
	int	err;

	if (!l2->up)
		return;
	mISDN_HEAD_PRIM(skb) = prim;
	mISDN_HEAD_ID(skb) = (l2->ch.nr << 16) | l2->ch.addr;
	err = l2->up->send(l2->up, skb);
	if (err) {
157 158
		printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
		       mISDNDevName4ch(&l2->ch), err);
Karsten Keil's avatar
Karsten Keil committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
		dev_kfree_skb(skb);
	}
}

static void
l2up_create(struct layer2 *l2, u_int prim, int len, void *arg)
{
	struct sk_buff	*skb;
	struct mISDNhead *hh;
	int		err;

	if (!l2->up)
		return;
	skb = mI_alloc_skb(len, GFP_ATOMIC);
	if (!skb)
		return;
	hh = mISDN_HEAD_P(skb);
	hh->prim = prim;
	hh->id = (l2->ch.nr << 16) | l2->ch.addr;
	if (len)
		memcpy(skb_put(skb, len), arg, len);
	err = l2->up->send(l2->up, skb);
	if (err) {
182 183
		printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
		       mISDNDevName4ch(&l2->ch), err);
Karsten Keil's avatar
Karsten Keil committed
184 185 186 187 188 189 190 191 192 193
		dev_kfree_skb(skb);
	}
}

static int
l2down_skb(struct layer2 *l2, struct sk_buff *skb) {
	int ret;

	ret = l2->ch.recv(l2->ch.peer, skb);
	if (ret && (*debug & DEBUG_L2_RECV))
194 195
		printk(KERN_DEBUG "l2down_skb: dev %s ret(%d)\n",
		       mISDNDevName4ch(&l2->ch), ret);
Karsten Keil's avatar
Karsten Keil committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	return ret;
}

static int
l2down_raw(struct layer2 *l2, struct sk_buff *skb)
{
	struct mISDNhead *hh = mISDN_HEAD_P(skb);

	if (hh->prim == PH_DATA_REQ) {
		if (test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
			skb_queue_tail(&l2->down_queue, skb);
			return 0;
		}
		l2->down_id = mISDN_HEAD_ID(skb);
	}
	return l2down_skb(l2, skb);
}

static int
l2down(struct layer2 *l2, u_int prim, u_int id, struct sk_buff *skb)
{
	struct mISDNhead *hh = mISDN_HEAD_P(skb);

	hh->prim = prim;
	hh->id = id;
	return l2down_raw(l2, skb);
}

static int
l2down_create(struct layer2 *l2, u_int prim, u_int id, int len, void *arg)
{
	struct sk_buff	*skb;
	int		err;
	struct mISDNhead *hh;

	skb = mI_alloc_skb(len, GFP_ATOMIC);
	if (!skb)
		return -ENOMEM;
	hh = mISDN_HEAD_P(skb);
	hh->prim = prim;
	hh->id = id;
	if (len)
		memcpy(skb_put(skb, len), arg, len);
	err = l2down_raw(l2, skb);
	if (err)
		dev_kfree_skb(skb);
	return err;
}

static int
ph_data_confirm(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb) {
	struct sk_buff *nskb = skb;
	int ret = -EAGAIN;

	if (test_bit(FLG_L1_NOTREADY, &l2->flag)) {
		if (hh->id == l2->down_id) {
			nskb = skb_dequeue(&l2->down_queue);
			if (nskb) {
				l2->down_id = mISDN_HEAD_ID(nskb);
				if (l2down_skb(l2, nskb)) {
					dev_kfree_skb(nskb);
					l2->down_id = MISDN_ID_NONE;
				}
			} else
				l2->down_id = MISDN_ID_NONE;
			if (ret) {
				dev_kfree_skb(skb);
				ret = 0;
			}
			if (l2->down_id == MISDN_ID_NONE) {
				test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
				mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
			}
		}
	}
	if (!test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
		nskb = skb_dequeue(&l2->down_queue);
		if (nskb) {
			l2->down_id = mISDN_HEAD_ID(nskb);
			if (l2down_skb(l2, nskb)) {
				dev_kfree_skb(nskb);
				l2->down_id = MISDN_ID_NONE;
				test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
			}
		} else
			test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
	}
	return ret;
}

286 287 288 289 290 291 292 293 294
static void
l2_timeout(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb;
	struct mISDNhead *hh;

	skb = mI_alloc_skb(0, GFP_ATOMIC);
	if (!skb) {
295 296 297
		printk(KERN_WARNING "%s: L2(%d,%d) nr:%x timer %s no skb\n",
		       mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
		       l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
298 299 300 301 302 303
		return;
	}
	hh = mISDN_HEAD_P(skb);
	hh->prim = event == EV_L2_T200 ? DL_TIMER200_IND : DL_TIMER203_IND;
	hh->id = l2->ch.nr;
	if (*debug & DEBUG_TIMER)
304 305 306
		printk(KERN_DEBUG "%s: L2(%d,%d) nr:%x timer %s expired\n",
		       mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
		       l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
307 308 309 310
	if (l2->ch.st)
		l2->ch.st->own.recv(&l2->ch.st->own, skb);
}

Karsten Keil's avatar
Karsten Keil committed
311 312 313 314
static int
l2mgr(struct layer2 *l2, u_int prim, void *arg) {
	long c = (long)arg;

315 316
	printk(KERN_WARNING "l2mgr: dev %s addr:%x prim %x %c\n",
	       mISDNDevName4ch(&l2->ch), l2->id, prim, (char)c);
Karsten Keil's avatar
Karsten Keil committed
317
	if (test_bit(FLG_LAPD, &l2->flag) &&
318
	    !test_bit(FLG_FIXED_TEI, &l2->flag)) {
Karsten Keil's avatar
Karsten Keil committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		switch (c) {
		case 'C':
		case 'D':
		case 'G':
		case 'H':
			l2_tei(l2, prim, (u_long)arg);
			break;
		}
	}
	return 0;
}

static void
set_peer_busy(struct layer2 *l2) {
	test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
	if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
		test_and_set_bit(FLG_L2BLOCK, &l2->flag);
}

static void
clear_peer_busy(struct layer2 *l2) {
	if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
		test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
}

static void
InitWin(struct layer2 *l2)
{
	int i;

	for (i = 0; i < MAX_WINDOW; i++)
		l2->windowar[i] = NULL;
}

static int
freewin(struct layer2 *l2)
{
	int i, cnt = 0;

	for (i = 0; i < MAX_WINDOW; i++) {
		if (l2->windowar[i]) {
			cnt++;
			dev_kfree_skb(l2->windowar[i]);
			l2->windowar[i] = NULL;
		}
	}
	return cnt;
}

static void
ReleaseWin(struct layer2 *l2)
{
	int cnt = freewin(l2);

	if (cnt)
		printk(KERN_WARNING
375
		       "isdnl2 freed %d skbuffs in release\n", cnt);
Karsten Keil's avatar
Karsten Keil committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 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
}

inline unsigned int
cansend(struct layer2 *l2)
{
	unsigned int p1;

	if (test_bit(FLG_MOD128, &l2->flag))
		p1 = (l2->vs - l2->va) % 128;
	else
		p1 = (l2->vs - l2->va) % 8;
	return (p1 < l2->window) && !test_bit(FLG_PEER_BUSY, &l2->flag);
}

inline void
clear_exception(struct layer2 *l2)
{
	test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
	test_and_clear_bit(FLG_REJEXC, &l2->flag);
	test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
	clear_peer_busy(l2);
}

static int
sethdraddr(struct layer2 *l2, u_char *header, int rsp)
{
	u_char *ptr = header;
	int crbit = rsp;

	if (test_bit(FLG_LAPD, &l2->flag)) {
		if (test_bit(FLG_LAPD_NET, &l2->flag))
			crbit = !crbit;
		*ptr++ = (l2->sapi << 2) | (crbit ? 2 : 0);
		*ptr++ = (l2->tei << 1) | 1;
		return 2;
	} else {
		if (test_bit(FLG_ORIG, &l2->flag))
			crbit = !crbit;
		if (crbit)
			*ptr++ = l2->addr.B;
		else
			*ptr++ = l2->addr.A;
		return 1;
	}
}

static inline void
enqueue_super(struct layer2 *l2, struct sk_buff *skb)
{
	if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
		dev_kfree_skb(skb);
}

static inline void
enqueue_ui(struct layer2 *l2, struct sk_buff *skb)
{
	if (l2->tm)
		l2_tei(l2, MDL_STATUS_UI_IND, 0);
	if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
		dev_kfree_skb(skb);
}

inline int
IsUI(u_char *data)
{
	return (data[0] & 0xef) == UI;
}

inline int
IsUA(u_char *data)
{
	return (data[0] & 0xef) == UA;
}

inline int
IsDM(u_char *data)
{
	return (data[0] & 0xef) == DM;
}

inline int
IsDISC(u_char *data)
{
	return (data[0] & 0xef) == DISC;
}

inline int
IsRR(u_char *data, struct layer2 *l2)
{
	if (test_bit(FLG_MOD128, &l2->flag))
		return data[0] == RR;
	else
		return (data[0] & 0xf) == 1;
}

inline int
IsSFrame(u_char *data, struct layer2 *l2)
{
	register u_char d = *data;

	if (!test_bit(FLG_MOD128, &l2->flag))
		d &= 0xf;
	return ((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c);
}

inline int
IsSABME(u_char *data, struct layer2 *l2)
{
	u_char d = data[0] & ~0x10;

	return test_bit(FLG_MOD128, &l2->flag) ? d == SABME : d == SABM;
}

inline int
IsREJ(u_char *data, struct layer2 *l2)
{
	return test_bit(FLG_MOD128, &l2->flag) ?
		data[0] == REJ : (data[0] & 0xf) == REJ;
}

inline int
IsFRMR(u_char *data)
{
	return (data[0] & 0xef) == FRMR;
}

inline int
IsRNR(u_char *data, struct layer2 *l2)
{
	return test_bit(FLG_MOD128, &l2->flag) ?
506
		data[0] == RNR : (data[0] & 0xf) == RNR;
Karsten Keil's avatar
Karsten Keil committed
507 508
}

509
static int
Karsten Keil's avatar
Karsten Keil committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
iframe_error(struct layer2 *l2, struct sk_buff *skb)
{
	u_int	i;
	int	rsp = *skb->data & 0x2;

	i = l2addrsize(l2) + (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1);
	if (test_bit(FLG_ORIG, &l2->flag))
		rsp = !rsp;
	if (rsp)
		return 'L';
	if (skb->len < i)
		return 'N';
	if ((skb->len - i) > l2->maxlen)
		return 'O';
	return 0;
}

527
static int
Karsten Keil's avatar
Karsten Keil committed
528 529 530 531 532 533 534 535
super_error(struct layer2 *l2, struct sk_buff *skb)
{
	if (skb->len != l2addrsize(l2) +
	    (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1))
		return 'N';
	return 0;
}

536
static int
Karsten Keil's avatar
Karsten Keil committed
537 538 539 540 541 542 543 544 545 546 547 548
unnum_error(struct layer2 *l2, struct sk_buff *skb, int wantrsp)
{
	int rsp = (*skb->data & 0x2) >> 1;
	if (test_bit(FLG_ORIG, &l2->flag))
		rsp = !rsp;
	if (rsp != wantrsp)
		return 'L';
	if (skb->len != l2addrsize(l2) + 1)
		return 'N';
	return 0;
}

549
static int
Karsten Keil's avatar
Karsten Keil committed
550 551 552 553 554 555 556 557 558 559 560 561
UI_error(struct layer2 *l2, struct sk_buff *skb)
{
	int rsp = *skb->data & 0x2;
	if (test_bit(FLG_ORIG, &l2->flag))
		rsp = !rsp;
	if (rsp)
		return 'L';
	if (skb->len > l2->maxlen + l2addrsize(l2) + 1)
		return 'O';
	return 0;
}

562
static int
Karsten Keil's avatar
Karsten Keil committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
FRMR_error(struct layer2 *l2, struct sk_buff *skb)
{
	u_int	headers = l2addrsize(l2) + 1;
	u_char	*datap = skb->data + headers;
	int	rsp = *skb->data & 0x2;

	if (test_bit(FLG_ORIG, &l2->flag))
		rsp = !rsp;
	if (!rsp)
		return 'L';
	if (test_bit(FLG_MOD128, &l2->flag)) {
		if (skb->len < headers + 5)
			return 'N';
		else if (*debug & DEBUG_L2)
			l2m_debug(&l2->l2m,
578 579
				  "FRMR information %2x %2x %2x %2x %2x",
				  datap[0], datap[1], datap[2], datap[3], datap[4]);
Karsten Keil's avatar
Karsten Keil committed
580 581 582 583 584
	} else {
		if (skb->len < headers + 3)
			return 'N';
		else if (*debug & DEBUG_L2)
			l2m_debug(&l2->l2m,
585 586
				  "FRMR information %2x %2x %2x",
				  datap[0], datap[1], datap[2]);
Karsten Keil's avatar
Karsten Keil committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	}
	return 0;
}

static unsigned int
legalnr(struct layer2 *l2, unsigned int nr)
{
	if (test_bit(FLG_MOD128, &l2->flag))
		return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
	else
		return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
}

static void
setva(struct layer2 *l2, unsigned int nr)
{
	struct sk_buff	*skb;

	while (l2->va != nr) {
		l2->va++;
		if (test_bit(FLG_MOD128, &l2->flag))
			l2->va %= 128;
		else
			l2->va %= 8;
		if (l2->windowar[l2->sow]) {
			skb_trim(l2->windowar[l2->sow], 0);
			skb_queue_tail(&l2->tmp_queue, l2->windowar[l2->sow]);
			l2->windowar[l2->sow] = NULL;
		}
		l2->sow = (l2->sow + 1) % l2->window;
	}
	skb = skb_dequeue(&l2->tmp_queue);
	while (skb) {
		dev_kfree_skb(skb);
		skb = skb_dequeue(&l2->tmp_queue);
	}
}

static void
send_uframe(struct layer2 *l2, struct sk_buff *skb, u_char cmd, u_char cr)
{
	u_char tmp[MAX_L2HEADER_LEN];
	int i;

	i = sethdraddr(l2, tmp, cr);
	tmp[i++] = cmd;
	if (skb)
		skb_trim(skb, 0);
	else {
		skb = mI_alloc_skb(i, GFP_ATOMIC);
		if (!skb) {
638 639
			printk(KERN_WARNING "%s: can't alloc skbuff in %s\n",
			       mISDNDevName4ch(&l2->ch), __func__);
Karsten Keil's avatar
Karsten Keil committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
			return;
		}
	}
	memcpy(skb_put(skb, i), tmp, i);
	enqueue_super(l2, skb);
}


inline u_char
get_PollFlag(struct layer2 *l2, struct sk_buff *skb)
{
	return skb->data[l2addrsize(l2)] & 0x10;
}

inline u_char
get_PollFlagFree(struct layer2 *l2, struct sk_buff *skb)
{
	u_char PF;

	PF = get_PollFlag(l2, skb);
	dev_kfree_skb(skb);
	return PF;
}

inline void
start_t200(struct layer2 *l2, int i)
{
	mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
	test_and_set_bit(FLG_T200_RUN, &l2->flag);
}

inline void
restart_t200(struct layer2 *l2, int i)
{
	mISDN_FsmRestartTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
	test_and_set_bit(FLG_T200_RUN, &l2->flag);
}

inline void
stop_t200(struct layer2 *l2, int i)
{
	if (test_and_clear_bit(FLG_T200_RUN, &l2->flag))
		mISDN_FsmDelTimer(&l2->t200, i);
}

inline void
st5_dl_release_l2l3(struct layer2 *l2)
{
	int pr;

	if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
		pr = DL_RELEASE_CNF;
	else
		pr = DL_RELEASE_IND;
	l2up_create(l2, pr, 0, NULL);
}

inline void
lapb_dl_release_l2l3(struct layer2 *l2, int f)
{
	if (test_bit(FLG_LAPB, &l2->flag))
		l2down_create(l2, PH_DEACTIVATE_REQ, l2_newid(l2), 0, NULL);
	l2up_create(l2, f, 0, NULL);
}

static void
establishlink(struct FsmInst *fi)
{
	struct layer2 *l2 = fi->userdata;
	u_char cmd;

	clear_exception(l2);
	l2->rc = 0;
	cmd = (test_bit(FLG_MOD128, &l2->flag) ? SABME : SABM) | 0x10;
	send_uframe(l2, NULL, cmd, CMD);
	mISDN_FsmDelTimer(&l2->t203, 1);
	restart_t200(l2, 1);
	test_and_clear_bit(FLG_PEND_REL, &l2->flag);
	freewin(l2);
	mISDN_FsmChangeState(fi, ST_L2_5);
}

static void
l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
{
	struct sk_buff *skb = arg;
	struct layer2 *l2 = fi->userdata;

	if (get_PollFlagFree(l2, skb))
		l2mgr(l2, MDL_ERROR_IND, (void *) 'C');
	else
		l2mgr(l2, MDL_ERROR_IND, (void *) 'D');

}

static void
l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
{
	struct sk_buff *skb = arg;
	struct layer2 *l2 = fi->userdata;

	if (get_PollFlagFree(l2, skb))
		l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
	else {
		l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
		establishlink(fi);
		test_and_clear_bit(FLG_L3_INIT, &l2->flag);
	}
}

static void
l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
{
	struct sk_buff *skb = arg;
	struct layer2 *l2 = fi->userdata;

	if (get_PollFlagFree(l2, skb))
		l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
	else
		l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
	establishlink(fi);
	test_and_clear_bit(FLG_L3_INIT, &l2->flag);
}

static void
l2_go_st3(struct FsmInst *fi, int event, void *arg)
{
	dev_kfree_skb((struct sk_buff *)arg);
	mISDN_FsmChangeState(fi, ST_L2_3);
}

static void
l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;

	mISDN_FsmChangeState(fi, ST_L2_3);
	dev_kfree_skb((struct sk_buff *)arg);
	l2_tei(l2, MDL_ASSIGN_IND, 0);
}

static void
l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_tail(&l2->ui_queue, skb);
	mISDN_FsmChangeState(fi, ST_L2_2);
	l2_tei(l2, MDL_ASSIGN_IND, 0);
}

static void
l2_queue_ui(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_tail(&l2->ui_queue, skb);
}

static void
tx_ui(struct layer2 *l2)
{
	struct sk_buff *skb;
	u_char header[MAX_L2HEADER_LEN];
	int i;

	i = sethdraddr(l2, header, CMD);
	if (test_bit(FLG_LAPD_NET, &l2->flag))
		header[1] = 0xff; /* tei 127 */
	header[i++] = UI;
	while ((skb = skb_dequeue(&l2->ui_queue))) {
		memcpy(skb_push(skb, i), header, i);
		enqueue_ui(l2, skb);
	}
}

static void
l2_send_ui(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_tail(&l2->ui_queue, skb);
	tx_ui(l2);
}

static void
l2_got_ui(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_pull(skb, l2headersize(l2, 1));
/*
 *		in states 1-3 for broadcast
 */

	if (l2->tm)
		l2_tei(l2, MDL_STATUS_UI_IND, 0);
	l2up(l2, DL_UNITDATA_IND, skb);
}

static void
l2_establish(struct FsmInst *fi, int event, void *arg)
{
	struct sk_buff *skb = arg;
	struct layer2 *l2 = fi->userdata;

	establishlink(fi);
	test_and_set_bit(FLG_L3_INIT, &l2->flag);
	dev_kfree_skb(skb);
}

static void
l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
{
	struct sk_buff *skb = arg;
	struct layer2 *l2 = fi->userdata;

	skb_queue_purge(&l2->i_queue);
	test_and_set_bit(FLG_L3_INIT, &l2->flag);
	test_and_clear_bit(FLG_PEND_REL, &l2->flag);
	dev_kfree_skb(skb);
}

static void
l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
{
	struct sk_buff *skb = arg;
	struct layer2 *l2 = fi->userdata;

	skb_queue_purge(&l2->i_queue);
	establishlink(fi);
	test_and_set_bit(FLG_L3_INIT, &l2->flag);
	dev_kfree_skb(skb);
}

static void
l2_release(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_trim(skb, 0);
	l2up(l2, DL_RELEASE_CNF, skb);
}

static void
l2_pend_rel(struct FsmInst *fi, int event, void *arg)
{
	struct sk_buff *skb = arg;
	struct layer2 *l2 = fi->userdata;

	test_and_set_bit(FLG_PEND_REL, &l2->flag);
	dev_kfree_skb(skb);
}

static void
l2_disconnect(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_purge(&l2->i_queue);
	freewin(l2);
	mISDN_FsmChangeState(fi, ST_L2_6);
	l2->rc = 0;
	send_uframe(l2, NULL, DISC | 0x10, CMD);
	mISDN_FsmDelTimer(&l2->t203, 1);
	restart_t200(l2, 2);
	if (skb)
		dev_kfree_skb(skb);
}

static void
l2_start_multi(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;
	struct sk_buff	*skb = arg;

	l2->vs = 0;
	l2->va = 0;
	l2->vr = 0;
	l2->sow = 0;
	clear_exception(l2);
	send_uframe(l2, NULL, UA | get_PollFlag(l2, skb), RSP);
	mISDN_FsmChangeState(fi, ST_L2_7);
	mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
	skb_trim(skb, 0);
	l2up(l2, DL_ESTABLISH_IND, skb);
	if (l2->tm)
		l2_tei(l2, MDL_STATUS_UP_IND, 0);
}

static void
l2_send_UA(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
}

static void
l2_send_DM(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	send_uframe(l2, skb, DM | get_PollFlag(l2, skb), RSP);
}

static void
l2_restart_multi(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;
	struct sk_buff	*skb = arg;
	int		est = 0;

	send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);

	l2mgr(l2, MDL_ERROR_IND, (void *) 'F');

	if (l2->vs != l2->va) {
		skb_queue_purge(&l2->i_queue);
		est = 1;
	}

	clear_exception(l2);
	l2->vs = 0;
	l2->va = 0;
	l2->vr = 0;
	l2->sow = 0;
	mISDN_FsmChangeState(fi, ST_L2_7);
	stop_t200(l2, 3);
	mISDN_FsmRestartTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);

	if (est)
		l2up_create(l2, DL_ESTABLISH_IND, 0, NULL);
/*		mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
 *		    MGR_SHORTSTATUS | INDICATION, SSTATUS_L2_ESTABLISHED,
 *		    0, NULL, 0);
 */
	if (skb_queue_len(&l2->i_queue) && cansend(l2))
		mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
}

static void
l2_stop_multi(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;
	struct sk_buff	*skb = arg;

	mISDN_FsmChangeState(fi, ST_L2_4);
	mISDN_FsmDelTimer(&l2->t203, 3);
	stop_t200(l2, 4);

	send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
	skb_queue_purge(&l2->i_queue);
	freewin(l2);
	lapb_dl_release_l2l3(l2, DL_RELEASE_IND);
	if (l2->tm)
		l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
}

static void
l2_connected(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;
	struct sk_buff	*skb = arg;
	int pr = -1;

	if (!get_PollFlag(l2, skb)) {
		l2_mdl_error_ua(fi, event, arg);
		return;
	}
	dev_kfree_skb(skb);
	if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
		l2_disconnect(fi, event, NULL);
	if (test_and_clear_bit(FLG_L3_INIT, &l2->flag)) {
		pr = DL_ESTABLISH_CNF;
	} else if (l2->vs != l2->va) {
		skb_queue_purge(&l2->i_queue);
		pr = DL_ESTABLISH_IND;
	}
	stop_t200(l2, 5);
	l2->vr = 0;
	l2->vs = 0;
	l2->va = 0;
	l2->sow = 0;
	mISDN_FsmChangeState(fi, ST_L2_7);
	mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 4);
	if (pr != -1)
		l2up_create(l2, pr, 0, NULL);

	if (skb_queue_len(&l2->i_queue) && cansend(l2))
		mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);

	if (l2->tm)
		l2_tei(l2, MDL_STATUS_UP_IND, 0);
}

static void
l2_released(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	if (!get_PollFlag(l2, skb)) {
		l2_mdl_error_ua(fi, event, arg);
		return;
	}
	dev_kfree_skb(skb);
	stop_t200(l2, 6);
	lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
	mISDN_FsmChangeState(fi, ST_L2_4);
	if (l2->tm)
		l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
}

static void
l2_reestablish(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	if (!get_PollFlagFree(l2, skb)) {
		establishlink(fi);
		test_and_set_bit(FLG_L3_INIT, &l2->flag);
	}
}

static void
l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	if (get_PollFlagFree(l2, skb)) {
		stop_t200(l2, 7);
		if (!test_bit(FLG_L3_INIT, &l2->flag))
			skb_queue_purge(&l2->i_queue);
		if (test_bit(FLG_LAPB, &l2->flag))
			l2down_create(l2, PH_DEACTIVATE_REQ,
1086
				      l2_newid(l2), 0, NULL);
Karsten Keil's avatar
Karsten Keil committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
		st5_dl_release_l2l3(l2);
		mISDN_FsmChangeState(fi, ST_L2_4);
		if (l2->tm)
			l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
	}
}

static void
l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	if (get_PollFlagFree(l2, skb)) {
		stop_t200(l2, 8);
		lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
		mISDN_FsmChangeState(fi, ST_L2_4);
		if (l2->tm)
			l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
	}
}

1109
static void
Karsten Keil's avatar
Karsten Keil committed
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
enquiry_cr(struct layer2 *l2, u_char typ, u_char cr, u_char pf)
{
	struct sk_buff *skb;
	u_char tmp[MAX_L2HEADER_LEN];
	int i;

	i = sethdraddr(l2, tmp, cr);
	if (test_bit(FLG_MOD128, &l2->flag)) {
		tmp[i++] = typ;
		tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
	} else
		tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
	skb = mI_alloc_skb(i, GFP_ATOMIC);
	if (!skb) {
1124 1125
		printk(KERN_WARNING "%s: isdnl2 can't alloc sbbuff in %s\n",
		       mISDNDevName4ch(&l2->ch), __func__);
Karsten Keil's avatar
Karsten Keil committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
		return;
	}
	memcpy(skb_put(skb, i), tmp, i);
	enqueue_super(l2, skb);
}

inline void
enquiry_response(struct layer2 *l2)
{
	if (test_bit(FLG_OWN_BUSY, &l2->flag))
		enquiry_cr(l2, RNR, RSP, 1);
	else
		enquiry_cr(l2, RR, RSP, 1);
	test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
}

inline void
transmit_enquiry(struct layer2 *l2)
{
	if (test_bit(FLG_OWN_BUSY, &l2->flag))
		enquiry_cr(l2, RNR, CMD, 1);
	else
		enquiry_cr(l2, RR, CMD, 1);
	test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
	start_t200(l2, 9);
}


static void
nrerrorrecovery(struct FsmInst *fi)
{
	struct layer2 *l2 = fi->userdata;

	l2mgr(l2, MDL_ERROR_IND, (void *) 'J');
	establishlink(fi);
	test_and_clear_bit(FLG_L3_INIT, &l2->flag);
}

static void
invoke_retransmission(struct layer2 *l2, unsigned int nr)
{
	u_int	p1;

	if (l2->vs != nr) {
		while (l2->vs != nr) {
			(l2->vs)--;
			if (test_bit(FLG_MOD128, &l2->flag)) {
				l2->vs %= 128;
				p1 = (l2->vs - l2->va) % 128;
			} else {
				l2->vs %= 8;
				p1 = (l2->vs - l2->va) % 8;
			}
			p1 = (p1 + l2->sow) % l2->window;
			if (l2->windowar[p1])
				skb_queue_head(&l2->i_queue, l2->windowar[p1]);
			else
				printk(KERN_WARNING
1184
				       "%s: windowar[%d] is NULL\n",
1185
				       mISDNDevName4ch(&l2->ch), p1);
Karsten Keil's avatar
Karsten Keil committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
			l2->windowar[p1] = NULL;
		}
		mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
	}
}

static void
l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;
	int PollFlag, rsp, typ = RR;
	unsigned int nr;

	rsp = *skb->data & 0x2;
	if (test_bit(FLG_ORIG, &l2->flag))
		rsp = !rsp;

	skb_pull(skb, l2addrsize(l2));
	if (IsRNR(skb->data, l2)) {
		set_peer_busy(l2);
		typ = RNR;
	} else
		clear_peer_busy(l2);
	if (IsREJ(skb->data, l2))
		typ = REJ;

	if (test_bit(FLG_MOD128, &l2->flag)) {
		PollFlag = (skb->data[1] & 0x1) == 0x1;
		nr = skb->data[1] >> 1;
	} else {
		PollFlag = (skb->data[0] & 0x10);
		nr = (skb->data[0] >> 5) & 0x7;
	}
	dev_kfree_skb(skb);

	if (PollFlag) {
		if (rsp)
			l2mgr(l2, MDL_ERROR_IND, (void *) 'A');
		else
			enquiry_response(l2);
	}
	if (legalnr(l2, nr)) {
		if (typ == REJ) {
			setva(l2, nr);
			invoke_retransmission(l2, nr);
			stop_t200(l2, 10);
			if (mISDN_FsmAddTimer(&l2->t203, l2->T203,
1234
					      EV_L2_T203, NULL, 6))
Karsten Keil's avatar
Karsten Keil committed
1235 1236 1237 1238 1239
				l2m_debug(&l2->l2m, "Restart T203 ST7 REJ");
		} else if ((nr == l2->vs) && (typ == RR)) {
			setva(l2, nr);
			stop_t200(l2, 11);
			mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1240
					      EV_L2_T203, NULL, 7);
Karsten Keil's avatar
Karsten Keil committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
		} else if ((l2->va != nr) || (typ == RNR)) {
			setva(l2, nr);
			if (typ != RR)
				mISDN_FsmDelTimer(&l2->t203, 9);
			restart_t200(l2, 12);
		}
		if (skb_queue_len(&l2->i_queue) && (typ == RR))
			mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
	} else
		nrerrorrecovery(fi);
}

static void
l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	if (!test_bit(FLG_L3_INIT, &l2->flag))
		skb_queue_tail(&l2->i_queue, skb);
	else
		dev_kfree_skb(skb);
}

static void
l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_tail(&l2->i_queue, skb);
	mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
}

static void
l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_tail(&l2->i_queue, skb);
}

static void
l2_got_iframe(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;
	struct sk_buff	*skb = arg;
	int		PollFlag, i;
	u_int		ns, nr;

	i = l2addrsize(l2);
	if (test_bit(FLG_MOD128, &l2->flag)) {
		PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
		ns = skb->data[i] >> 1;
		nr = (skb->data[i + 1] >> 1) & 0x7f;
	} else {
		PollFlag = (skb->data[i] & 0x10);
		ns = (skb->data[i] >> 1) & 0x7;
		nr = (skb->data[i] >> 5) & 0x7;
	}
	if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
		dev_kfree_skb(skb);
		if (PollFlag)
			enquiry_response(l2);
	} else {
		if (l2->vr == ns) {
			l2->vr++;
			if (test_bit(FLG_MOD128, &l2->flag))
				l2->vr %= 128;
			else
				l2->vr %= 8;
			test_and_clear_bit(FLG_REJEXC, &l2->flag);
			if (PollFlag)
				enquiry_response(l2);
			else
				test_and_set_bit(FLG_ACK_PEND, &l2->flag);
			skb_pull(skb, l2headersize(l2, 0));
			l2up(l2, DL_DATA_IND, skb);
		} else {
			/* n(s)!=v(r) */
			dev_kfree_skb(skb);
			if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
				if (PollFlag)
					enquiry_response(l2);
			} else {
				enquiry_cr(l2, REJ, RSP, PollFlag);
				test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
			}
		}
	}
	if (legalnr(l2, nr)) {
		if (!test_bit(FLG_PEER_BUSY, &l2->flag) &&
		    (fi->state == ST_L2_7)) {
			if (nr == l2->vs) {
				stop_t200(l2, 13);
				mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1338
						      EV_L2_T203, NULL, 7);
Karsten Keil's avatar
Karsten Keil committed
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
			} else if (nr != l2->va)
				restart_t200(l2, 14);
		}
		setva(l2, nr);
	} else {
		nrerrorrecovery(fi);
		return;
	}
	if (skb_queue_len(&l2->i_queue) && (fi->state == ST_L2_7))
		mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
	if (test_and_clear_bit(FLG_ACK_PEND, &l2->flag))
		enquiry_cr(l2, RR, RSP, 0);
}

static void
l2_got_tei(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;
	u_int		info;

	l2->tei = (signed char)(long)arg;
	set_channel_address(&l2->ch, l2->sapi, l2->tei);
	info = DL_INFO_L2_CONNECT;
	l2up_create(l2, DL_INFORMATION_IND, sizeof(info), &info);
	if (fi->state == ST_L2_3) {
		establishlink(fi);
		test_and_set_bit(FLG_L3_INIT, &l2->flag);
	} else
		mISDN_FsmChangeState(fi, ST_L2_4);
	if (skb_queue_len(&l2->ui_queue))
		tx_ui(l2);
}

static void
l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	if (test_bit(FLG_LAPD, &l2->flag) &&
1378
	    test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
Karsten Keil's avatar
Karsten Keil committed
1379 1380 1381 1382 1383 1384 1385 1386
		mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
	} else if (l2->rc == l2->N200) {
		mISDN_FsmChangeState(fi, ST_L2_4);
		test_and_clear_bit(FLG_T200_RUN, &l2->flag);
		skb_queue_purge(&l2->i_queue);
		l2mgr(l2, MDL_ERROR_IND, (void *) 'G');
		if (test_bit(FLG_LAPB, &l2->flag))
			l2down_create(l2, PH_DEACTIVATE_REQ,
1387
				      l2_newid(l2), 0, NULL);
Karsten Keil's avatar
Karsten Keil committed
1388 1389 1390 1391 1392 1393 1394
		st5_dl_release_l2l3(l2);
		if (l2->tm)
			l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
	} else {
		l2->rc++;
		mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
		send_uframe(l2, NULL, (test_bit(FLG_MOD128, &l2->flag) ?
1395
				       SABME : SABM) | 0x10, CMD);
Karsten Keil's avatar
Karsten Keil committed
1396 1397 1398 1399 1400 1401 1402 1403 1404
	}
}

static void
l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	if (test_bit(FLG_LAPD, &l2->flag) &&
1405
	    test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
Karsten Keil's avatar
Karsten Keil committed
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
		mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
	} else if (l2->rc == l2->N200) {
		mISDN_FsmChangeState(fi, ST_L2_4);
		test_and_clear_bit(FLG_T200_RUN, &l2->flag);
		l2mgr(l2, MDL_ERROR_IND, (void *) 'H');
		lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
		if (l2->tm)
			l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
	} else {
		l2->rc++;
		mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200,
1417
				  NULL, 9);
Karsten Keil's avatar
Karsten Keil committed
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
		send_uframe(l2, NULL, DISC | 0x10, CMD);
	}
}

static void
l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	if (test_bit(FLG_LAPD, &l2->flag) &&
1428
	    test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
Karsten Keil's avatar
Karsten Keil committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
		mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
		return;
	}
	test_and_clear_bit(FLG_T200_RUN, &l2->flag);
	l2->rc = 0;
	mISDN_FsmChangeState(fi, ST_L2_8);
	transmit_enquiry(l2);
	l2->rc++;
}

static void
l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	if (test_bit(FLG_LAPD, &l2->flag) &&
1445
	    test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
Karsten Keil's avatar
Karsten Keil committed
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
		mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
		return;
	}
	test_and_clear_bit(FLG_T200_RUN, &l2->flag);
	if (l2->rc == l2->N200) {
		l2mgr(l2, MDL_ERROR_IND, (void *) 'I');
		establishlink(fi);
		test_and_clear_bit(FLG_L3_INIT, &l2->flag);
	} else {
		transmit_enquiry(l2);
		l2->rc++;
	}
}

static void
l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	if (test_bit(FLG_LAPD, &l2->flag) &&
1466
	    test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
Karsten Keil's avatar
Karsten Keil committed
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
		mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 9);
		return;
	}
	mISDN_FsmChangeState(fi, ST_L2_8);
	transmit_enquiry(l2);
	l2->rc = 0;
}

static void
l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
{
	struct layer2	*l2 = fi->userdata;
	struct sk_buff	*skb, *nskb, *oskb;
	u_char		header[MAX_L2HEADER_LEN];
	u_int		i, p1;

	if (!cansend(l2))
		return;

	skb = skb_dequeue(&l2->i_queue);
	if (!skb)
		return;

	if (test_bit(FLG_MOD128, &l2->flag))
		p1 = (l2->vs - l2->va) % 128;
	else
		p1 = (l2->vs - l2->va) % 8;
	p1 = (p1 + l2->sow) % l2->window;
	if (l2->windowar[p1]) {
1496 1497
		printk(KERN_WARNING "%s: l2 try overwrite ack queue entry %d\n",
		       mISDNDevName4ch(&l2->ch), p1);
Karsten Keil's avatar
Karsten Keil committed
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
		dev_kfree_skb(l2->windowar[p1]);
	}
	l2->windowar[p1] = skb;
	i = sethdraddr(l2, header, CMD);
	if (test_bit(FLG_MOD128, &l2->flag)) {
		header[i++] = l2->vs << 1;
		header[i++] = l2->vr << 1;
		l2->vs = (l2->vs + 1) % 128;
	} else {
		header[i++] = (l2->vr << 5) | (l2->vs << 1);
		l2->vs = (l2->vs + 1) % 8;
	}

	nskb = skb_clone(skb, GFP_ATOMIC);
	p1 = skb_headroom(nskb);
	if (p1 >= i)
		memcpy(skb_push(nskb, i), header, i);
	else {
		printk(KERN_WARNING
1517 1518
		       "%s: L2 pull_iqueue skb header(%d/%d) too short\n",
		       mISDNDevName4ch(&l2->ch), i, p1);
Karsten Keil's avatar
Karsten Keil committed
1519 1520 1521 1522
		oskb = nskb;
		nskb = mI_alloc_skb(oskb->len + i, GFP_ATOMIC);
		if (!nskb) {
			dev_kfree_skb(oskb);
1523 1524
			printk(KERN_WARNING "%s: no skb mem in %s\n",
			       mISDNDevName4ch(&l2->ch), __func__);
Karsten Keil's avatar
Karsten Keil committed
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
			return;
		}
		memcpy(skb_put(nskb, i), header, i);
		memcpy(skb_put(nskb, oskb->len), oskb->data, oskb->len);
		dev_kfree_skb(oskb);
	}
	l2down(l2, PH_DATA_REQ, l2_newid(l2), nskb);
	test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
	if (!test_and_set_bit(FLG_T200_RUN, &l2->flag)) {
		mISDN_FsmDelTimer(&l2->t203, 13);
		mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 11);
	}
}

static void
l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;
	int PollFlag, rsp, rnr = 0;
	unsigned int nr;

	rsp = *skb->data & 0x2;
	if (test_bit(FLG_ORIG, &l2->flag))
		rsp = !rsp;

	skb_pull(skb, l2addrsize(l2));

	if (IsRNR(skb->data, l2)) {
		set_peer_busy(l2);
		rnr = 1;
	} else
		clear_peer_busy(l2);

	if (test_bit(FLG_MOD128, &l2->flag)) {
		PollFlag = (skb->data[1] & 0x1) == 0x1;
		nr = skb->data[1] >> 1;
	} else {
		PollFlag = (skb->data[0] & 0x10);
		nr = (skb->data[0] >> 5) & 0x7;
	}
	dev_kfree_skb(skb);
	if (rsp && PollFlag) {
		if (legalnr(l2, nr)) {
			if (rnr) {
				restart_t200(l2, 15);
			} else {
				stop_t200(l2, 16);
				mISDN_FsmAddTimer(&l2->t203, l2->T203,
1574
						  EV_L2_T203, NULL, 5);
Karsten Keil's avatar
Karsten Keil committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
				setva(l2, nr);
			}
			invoke_retransmission(l2, nr);
			mISDN_FsmChangeState(fi, ST_L2_7);
			if (skb_queue_len(&l2->i_queue) && cansend(l2))
				mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
		} else
			nrerrorrecovery(fi);
	} else {
		if (!rsp && PollFlag)
			enquiry_response(l2);
		if (legalnr(l2, nr))
			setva(l2, nr);
		else
			nrerrorrecovery(fi);
	}
}

static void
l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_pull(skb, l2addrsize(l2) + 1);

	if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) || /* I or S */
	    (IsUA(skb->data) && (fi->state == ST_L2_7))) {
		l2mgr(l2, MDL_ERROR_IND, (void *) 'K');
		establishlink(fi);
		test_and_clear_bit(FLG_L3_INIT, &l2->flag);
	}
	dev_kfree_skb(skb);
}

static void
l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	skb_queue_purge(&l2->ui_queue);
	l2->tei = GROUP_TEI;
	mISDN_FsmChangeState(fi, ST_L2_1);
}

static void
l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	skb_queue_purge(&l2->ui_queue);
	l2->tei = GROUP_TEI;
	l2up_create(l2, DL_RELEASE_IND, 0, NULL);
	mISDN_FsmChangeState(fi, ST_L2_1);
}

static void
l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	skb_queue_purge(&l2->i_queue);
	skb_queue_purge(&l2->ui_queue);
	freewin(l2);
	l2->tei = GROUP_TEI;
	stop_t200(l2, 17);
	st5_dl_release_l2l3(l2);
	mISDN_FsmChangeState(fi, ST_L2_1);
}

static void
l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	skb_queue_purge(&l2->ui_queue);
	l2->tei = GROUP_TEI;
	stop_t200(l2, 18);
	l2up_create(l2, DL_RELEASE_IND, 0, NULL);
	mISDN_FsmChangeState(fi, ST_L2_1);
}

static void
l2_tei_remove(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	skb_queue_purge(&l2->i_queue);
	skb_queue_purge(&l2->ui_queue);
	freewin(l2);
	l2->tei = GROUP_TEI;
	stop_t200(l2, 17);
	mISDN_FsmDelTimer(&l2->t203, 19);
	l2up_create(l2, DL_RELEASE_IND, 0, NULL);
/*	mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
 *		MGR_SHORTSTATUS_IND, SSTATUS_L2_RELEASED,
 *		0, NULL, 0);
 */
	mISDN_FsmChangeState(fi, ST_L2_1);
}

static void
1677
l2_st14_persistent_da(struct FsmInst *fi, int event, void *arg)
Karsten Keil's avatar
Karsten Keil committed
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_purge(&l2->i_queue);
	skb_queue_purge(&l2->ui_queue);
	if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
		l2up(l2, DL_RELEASE_IND, skb);
	else
		dev_kfree_skb(skb);
}

static void
1691
l2_st5_persistent_da(struct FsmInst *fi, int event, void *arg)
Karsten Keil's avatar
Karsten Keil committed
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_purge(&l2->i_queue);
	skb_queue_purge(&l2->ui_queue);
	freewin(l2);
	stop_t200(l2, 19);
	st5_dl_release_l2l3(l2);
	mISDN_FsmChangeState(fi, ST_L2_4);
	if (l2->tm)
		l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
	dev_kfree_skb(skb);
}

static void
1708
l2_st6_persistent_da(struct FsmInst *fi, int event, void *arg)
Karsten Keil's avatar
Karsten Keil committed
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_purge(&l2->ui_queue);
	stop_t200(l2, 20);
	l2up(l2, DL_RELEASE_CNF, skb);
	mISDN_FsmChangeState(fi, ST_L2_4);
	if (l2->tm)
		l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
}

static void
1722
l2_persistent_da(struct FsmInst *fi, int event, void *arg)
Karsten Keil's avatar
Karsten Keil committed
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	skb_queue_purge(&l2->i_queue);
	skb_queue_purge(&l2->ui_queue);
	freewin(l2);
	stop_t200(l2, 19);
	mISDN_FsmDelTimer(&l2->t203, 19);
	l2up(l2, DL_RELEASE_IND, skb);
	mISDN_FsmChangeState(fi, ST_L2_4);
	if (l2->tm)
		l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
}

static void
l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	if (!test_and_set_bit(FLG_OWN_BUSY, &l2->flag)) {
		enquiry_cr(l2, RNR, RSP, 0);
		test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
	}
	if (skb)
		dev_kfree_skb(skb);
}

static void
l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;
	struct sk_buff *skb = arg;

	if (!test_and_clear_bit(FLG_OWN_BUSY, &l2->flag)) {
		enquiry_cr(l2, RR, RSP, 0);
		test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
	}
	if (skb)
		dev_kfree_skb(skb);
}

static void
l2_frame_error(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	l2mgr(l2, MDL_ERROR_IND, arg);
}

static void
l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
{
	struct layer2 *l2 = fi->userdata;

	l2mgr(l2, MDL_ERROR_IND, arg);
	establishlink(fi);
	test_and_clear_bit(FLG_L3_INIT, &l2->flag);
}

static struct FsmNode L2FnList[] =
{
	{ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
	{ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
	{ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
	{ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
	{ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
	{ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
	{ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
	{ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
	{ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
	{ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
	{ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
	{ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
	{ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
	{ST_L2_1, EV_L2_DL_UNITDATA, l2_queue_ui_assign},
	{ST_L2_2, EV_L2_DL_UNITDATA, l2_queue_ui},
	{ST_L2_3, EV_L2_DL_UNITDATA, l2_queue_ui},
	{ST_L2_4, EV_L2_DL_UNITDATA, l2_send_ui},
	{ST_L2_5, EV_L2_DL_UNITDATA, l2_send_ui},
	{ST_L2_6, EV_L2_DL_UNITDATA, l2_send_ui},
	{ST_L2_7, EV_L2_DL_UNITDATA, l2_send_ui},
	{ST_L2_8, EV_L2_DL_UNITDATA, l2_send_ui},
	{ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
	{ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
	{ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
	{ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
	{ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
	{ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
	{ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
	{ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
	{ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
	{ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
	{ST_L2_4, EV_L2_SABME, l2_start_multi},
	{ST_L2_5, EV_L2_SABME, l2_send_UA},
	{ST_L2_6, EV_L2_SABME, l2_send_DM},
	{ST_L2_7, EV_L2_SABME, l2_restart_multi},
	{ST_L2_8, EV_L2_SABME, l2_restart_multi},
	{ST_L2_4, EV_L2_DISC, l2_send_DM},
	{ST_L2_5, EV_L2_DISC, l2_send_DM},
	{ST_L2_6, EV_L2_DISC, l2_send_UA},
	{ST_L2_7, EV_L2_DISC, l2_stop_multi},
	{ST_L2_8, EV_L2_DISC, l2_stop_multi},
	{ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
	{ST_L2_5, EV_L2_UA, l2_connected},
	{ST_L2_6, EV_L2_UA, l2_released},
	{ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
	{ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
	{ST_L2_4, EV_L2_DM, l2_reestablish},
	{ST_L2_5, EV_L2_DM, l2_st5_dm_release},
	{ST_L2_6, EV_L2_DM, l2_st6_dm_release},
	{ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
	{ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
	{ST_L2_1, EV_L2_UI, l2_got_ui},
	{ST_L2_2, EV_L2_UI, l2_got_ui},
	{ST_L2_3, EV_L2_UI, l2_got_ui},
	{ST_L2_4, EV_L2_UI, l2_got_ui},
	{ST_L2_5, EV_L2_UI, l2_got_ui},
	{ST_L2_6, EV_L2_UI, l2_got_ui},
	{ST_L2_7, EV_L2_UI, l2_got_ui},
	{ST_L2_8, EV_L2_UI, l2_got_ui},
	{ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
	{ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
	{ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
	{ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
	{ST_L2_7, EV_L2_I, l2_got_iframe},
	{ST_L2_8, EV_L2_I, l2_got_iframe},
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
	{ST_L2_5, EV_L2_T200, l2_timeout},
	{ST_L2_6, EV_L2_T200, l2_timeout},
	{ST_L2_7, EV_L2_T200, l2_timeout},
	{ST_L2_8, EV_L2_T200, l2_timeout},
	{ST_L2_7, EV_L2_T203, l2_timeout},
	{ST_L2_5, EV_L2_T200I, l2_st5_tout_200},
	{ST_L2_6, EV_L2_T200I, l2_st6_tout_200},
	{ST_L2_7, EV_L2_T200I, l2_st7_tout_200},
	{ST_L2_8, EV_L2_T200I, l2_st8_tout_200},
	{ST_L2_7, EV_L2_T203I, l2_st7_tout_203},
Karsten Keil's avatar
Karsten Keil committed
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
	{ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
	{ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
	{ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
	{ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
	{ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
	{ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
	{ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
	{ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
	{ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
	{ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1871
	{ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistent_da},
Karsten Keil's avatar
Karsten Keil committed
1872 1873
	{ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
	{ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
1874 1875 1876 1877 1878
	{ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistent_da},
	{ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistent_da},
	{ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistent_da},
	{ST_L2_7, EV_L1_DEACTIVATE, l2_persistent_da},
	{ST_L2_8, EV_L1_DEACTIVATE, l2_persistent_da},
Karsten Keil's avatar
Karsten Keil committed
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
};

static int
ph_data_indication(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb)
{
	u_char	*datap = skb->data;
	int	ret = -EINVAL;
	int	psapi, ptei;
	u_int	l;
	int	c = 0;

	l = l2addrsize(l2);
	if (skb->len <= l) {
		mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *) 'N');
		return ret;
	}
	if (test_bit(FLG_LAPD, &l2->flag)) { /* Maybe not needed */
		psapi = *datap++;
		ptei = *datap++;
		if ((psapi & 1) || !(ptei & 1)) {
			printk(KERN_WARNING
1900 1901
			       "%s l2 D-channel frame wrong EA0/EA1\n",
			       mISDNDevName4ch(&l2->ch));
Karsten Keil's avatar
Karsten Keil committed
1902 1903 1904 1905 1906
			return ret;
		}
		psapi >>= 2;
		ptei >>= 1;
		if (psapi != l2->sapi) {
Lucas De Marchi's avatar
Lucas De Marchi committed
1907
			/* not our business */
1908 1909
			if (*debug & DEBUG_L2)
				printk(KERN_DEBUG "%s: sapi %d/%d mismatch\n",
1910 1911
				       mISDNDevName4ch(&l2->ch), psapi,
				       l2->sapi);
Karsten Keil's avatar
Karsten Keil committed
1912 1913 1914 1915
			dev_kfree_skb(skb);
			return 0;
		}
		if ((ptei != l2->tei) && (ptei != GROUP_TEI)) {
Lucas De Marchi's avatar
Lucas De Marchi committed
1916
			/* not our business */
1917 1918
			if (*debug & DEBUG_L2)
				printk(KERN_DEBUG "%s: tei %d/%d mismatch\n",
1919
				       mISDNDevName4ch(&l2->ch), ptei, l2->tei);
Karsten Keil's avatar
Karsten Keil committed
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
			dev_kfree_skb(skb);
			return 0;
		}
	} else
		datap += l;
	if (!(*datap & 1)) {	/* I-Frame */
		c = iframe_error(l2, skb);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_I, skb);
	} else if (IsSFrame(datap, l2)) {	/* S-Frame */
		c = super_error(l2, skb);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SUPER, skb);
	} else if (IsUI(datap)) {
		c = UI_error(l2, skb);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UI, skb);
	} else if (IsSABME(datap, l2)) {
		c = unnum_error(l2, skb, CMD);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SABME, skb);
	} else if (IsUA(datap)) {
		c = unnum_error(l2, skb, RSP);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UA, skb);
	} else if (IsDISC(datap)) {
		c = unnum_error(l2, skb, CMD);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DISC, skb);
	} else if (IsDM(datap)) {
		c = unnum_error(l2, skb, RSP);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DM, skb);
	} else if (IsFRMR(datap)) {
		c = FRMR_error(l2, skb);
		if (!c)
			ret = mISDN_FsmEvent(&l2->l2m, EV_L2_FRMR, skb);
	} else
		c = 'L';
	if (c) {
1960 1961
		printk(KERN_WARNING "%s:l2 D-channel frame error %c\n",
		       mISDNDevName4ch(&l2->ch), c);
Karsten Keil's avatar
Karsten Keil committed
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
		mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
	}
	return ret;
}

static int
l2_send(struct mISDNchannel *ch, struct sk_buff *skb)
{
	struct layer2		*l2 = container_of(ch, struct layer2, ch);
	struct mISDNhead	*hh =  mISDN_HEAD_P(skb);
1972
	int			ret = -EINVAL;
Karsten Keil's avatar
Karsten Keil committed
1973 1974

	if (*debug & DEBUG_L2_RECV)
1975 1976 1977
		printk(KERN_DEBUG "%s: %s prim(%x) id(%x) sapi(%d) tei(%d)\n",
		       __func__, mISDNDevName4ch(&l2->ch), hh->prim, hh->id,
		       l2->sapi, l2->tei);
1978 1979 1980 1981 1982 1983
	if (hh->prim == DL_INTERN_MSG) {
		struct mISDNhead *chh = hh + 1; /* saved copy */

		*hh = *chh;
		if (*debug & DEBUG_L2_RECV)
			printk(KERN_DEBUG "%s: prim(%x) id(%x) internal msg\n",
1984
				mISDNDevName4ch(&l2->ch), hh->prim, hh->id);
1985
	}
Karsten Keil's avatar
Karsten Keil committed
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
	switch (hh->prim) {
	case PH_DATA_IND:
		ret = ph_data_indication(l2, hh, skb);
		break;
	case PH_DATA_CNF:
		ret = ph_data_confirm(l2, hh, skb);
		break;
	case PH_ACTIVATE_IND:
		test_and_set_bit(FLG_L1_ACTIV, &l2->flag);
		l2up_create(l2, MPH_ACTIVATE_IND, 0, NULL);
		if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
			ret = mISDN_FsmEvent(&l2->l2m,
1998
					     EV_L2_DL_ESTABLISH_REQ, skb);
Karsten Keil's avatar
Karsten Keil committed
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
		break;
	case PH_DEACTIVATE_IND:
		test_and_clear_bit(FLG_L1_ACTIV, &l2->flag);
		l2up_create(l2, MPH_DEACTIVATE_IND, 0, NULL);
		ret = mISDN_FsmEvent(&l2->l2m, EV_L1_DEACTIVATE, skb);
		break;
	case MPH_INFORMATION_IND:
		if (!l2->up)
			break;
		ret = l2->up->send(l2->up, skb);
		break;
	case DL_DATA_REQ:
		ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_DATA, skb);
		break;
	case DL_UNITDATA_REQ:
		ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_UNITDATA, skb);
		break;
	case DL_ESTABLISH_REQ:
		if (test_bit(FLG_LAPB, &l2->flag))
			test_and_set_bit(FLG_ORIG, &l2->flag);
		if (test_bit(FLG_L1_ACTIV, &l2->flag)) {
			if (test_bit(FLG_LAPD, &l2->flag) ||
2021
			    test_bit(FLG_ORIG, &l2->flag))
Karsten Keil's avatar
Karsten Keil committed
2022
				ret = mISDN_FsmEvent(&l2->l2m,
2023
						     EV_L2_DL_ESTABLISH_REQ, skb);
Karsten Keil's avatar
Karsten Keil committed
2024 2025
		} else {
			if (test_bit(FLG_LAPD, &l2->flag) ||
2026
			    test_bit(FLG_ORIG, &l2->flag)) {
Karsten Keil's avatar
Karsten Keil committed
2027
				test_and_set_bit(FLG_ESTAB_PEND,
2028
						 &l2->flag);
Karsten Keil's avatar
Karsten Keil committed
2029 2030
			}
			ret = l2down(l2, PH_ACTIVATE_REQ, l2_newid(l2),
2031
				     skb);
Karsten Keil's avatar
Karsten Keil committed
2032 2033 2034 2035 2036
		}
		break;
	case DL_RELEASE_REQ:
		if (test_bit(FLG_LAPB, &l2->flag))
			l2down_create(l2, PH_DEACTIVATE_REQ,
2037
				      l2_newid(l2), 0, NULL);
Karsten Keil's avatar
Karsten Keil committed
2038
		ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_RELEASE_REQ,
2039
				     skb);
Karsten Keil's avatar
Karsten Keil committed
2040
		break;
2041 2042 2043 2044 2045 2046
	case DL_TIMER200_IND:
		mISDN_FsmEvent(&l2->l2m, EV_L2_T200I, NULL);
		break;
	case DL_TIMER203_IND:
		mISDN_FsmEvent(&l2->l2m, EV_L2_T203I, NULL);
		break;
Karsten Keil's avatar
Karsten Keil committed
2047 2048 2049
	default:
		if (*debug & DEBUG_L2)
			l2m_debug(&l2->l2m, "l2 unknown pr %04x",
2050
				  hh->prim);
Karsten Keil's avatar
Karsten Keil committed
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
	}
	if (ret) {
		dev_kfree_skb(skb);
		ret = 0;
	}
	return ret;
}

int
tei_l2(struct layer2 *l2, u_int cmd, u_long arg)
{
	int		ret = -EINVAL;

	if (*debug & DEBUG_L2_TEI)
2065 2066
		printk(KERN_DEBUG "%s: cmd(%x) in %s\n",
		       mISDNDevName4ch(&l2->ch), cmd, __func__);
Karsten Keil's avatar
Karsten Keil committed
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
	switch (cmd) {
	case (MDL_ASSIGN_REQ):
		ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ASSIGN, (void *)arg);
		break;
	case (MDL_REMOVE_REQ):
		ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_REMOVE, NULL);
		break;
	case (MDL_ERROR_IND):
		ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
		break;
	case (MDL_ERROR_RSP):
		/* ETS 300-125 5.3.2.1 Test: TC13010 */
2079 2080
		printk(KERN_NOTICE "%s: MDL_ERROR|REQ (tei_l2)\n",
		       mISDNDevName4ch(&l2->ch));
Karsten Keil's avatar
Karsten Keil committed
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
		ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
		break;
	}
	return ret;
}

static void
release_l2(struct layer2 *l2)
{
	mISDN_FsmDelTimer(&l2->t200, 21);
	mISDN_FsmDelTimer(&l2->t203, 16);
	skb_queue_purge(&l2->i_queue);
	skb_queue_purge(&l2->ui_queue);
	skb_queue_purge(&l2->down_queue);
	ReleaseWin(l2);
	if (test_bit(FLG_LAPD, &l2->flag)) {
2097
		TEIrelease(l2);
Karsten Keil's avatar
Karsten Keil committed
2098 2099
		if (l2->ch.st)
			l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D,
2100
					       CLOSE_CHANNEL, NULL);
Karsten Keil's avatar
Karsten Keil committed
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
	}
	kfree(l2);
}

static int
l2_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
{
	struct layer2		*l2 = container_of(ch, struct layer2, ch);
	u_int			info;

	if (*debug & DEBUG_L2_CTRL)
2112 2113
		printk(KERN_DEBUG "%s: %s cmd(%x)\n",
		       mISDNDevName4ch(ch), __func__, cmd);
Karsten Keil's avatar
Karsten Keil committed
2114 2115 2116 2117 2118 2119 2120

	switch (cmd) {
	case OPEN_CHANNEL:
		if (test_bit(FLG_LAPD, &l2->flag)) {
			set_channel_address(&l2->ch, l2->sapi, l2->tei);
			info = DL_INFO_L2_CONNECT;
			l2up_create(l2, DL_INFORMATION_IND,
2121
				    sizeof(info), &info);
Karsten Keil's avatar
Karsten Keil committed
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
		}
		break;
	case CLOSE_CHANNEL:
		if (l2->ch.peer)
			l2->ch.peer->ctrl(l2->ch.peer, CLOSE_CHANNEL, NULL);
		release_l2(l2);
		break;
	}
	return 0;
}

struct layer2 *
2134
create_l2(struct mISDNchannel *ch, u_int protocol, u_long options, int tei,
2135
	  int sapi)
Karsten Keil's avatar
Karsten Keil committed
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
{
	struct layer2		*l2;
	struct channel_req	rq;

	l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
	if (!l2) {
		printk(KERN_ERR "kzalloc layer2 failed\n");
		return NULL;
	}
	l2->next_id = 1;
	l2->down_id = MISDN_ID_NONE;
	l2->up = ch;
	l2->ch.st = ch->st;
	l2->ch.send = l2_send;
	l2->ch.ctrl = l2_ctrl;
	switch (protocol) {
	case ISDN_P_LAPD_NT:
		test_and_set_bit(FLG_LAPD, &l2->flag);
		test_and_set_bit(FLG_LAPD_NET, &l2->flag);
		test_and_set_bit(FLG_MOD128, &l2->flag);
2156
		l2->sapi = sapi;
Karsten Keil's avatar
Karsten Keil committed
2157 2158 2159 2160 2161 2162 2163 2164 2165
		l2->maxlen = MAX_DFRAME_LEN;
		if (test_bit(OPTION_L2_PMX, &options))
			l2->window = 7;
		else
			l2->window = 1;
		if (test_bit(OPTION_L2_PTP, &options))
			test_and_set_bit(FLG_PTP, &l2->flag);
		if (test_bit(OPTION_L2_FIXEDTEI, &options))
			test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2166
		l2->tei = tei;
Karsten Keil's avatar
Karsten Keil committed
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
		l2->T200 = 1000;
		l2->N200 = 3;
		l2->T203 = 10000;
		if (test_bit(OPTION_L2_PMX, &options))
			rq.protocol = ISDN_P_NT_E1;
		else
			rq.protocol = ISDN_P_NT_S0;
		rq.adr.channel = 0;
		l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
		break;
	case ISDN_P_LAPD_TE:
		test_and_set_bit(FLG_LAPD, &l2->flag);
		test_and_set_bit(FLG_MOD128, &l2->flag);
		test_and_set_bit(FLG_ORIG, &l2->flag);
2181
		l2->sapi = sapi;
Karsten Keil's avatar
Karsten Keil committed
2182 2183 2184 2185 2186 2187 2188 2189 2190
		l2->maxlen = MAX_DFRAME_LEN;
		if (test_bit(OPTION_L2_PMX, &options))
			l2->window = 7;
		else
			l2->window = 1;
		if (test_bit(OPTION_L2_PTP, &options))
			test_and_set_bit(FLG_PTP, &l2->flag);
		if (test_bit(OPTION_L2_FIXEDTEI, &options))
			test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2191
		l2->tei = tei;
Karsten Keil's avatar
Karsten Keil committed
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
		l2->T200 = 1000;
		l2->N200 = 3;
		l2->T203 = 10000;
		if (test_bit(OPTION_L2_PMX, &options))
			rq.protocol = ISDN_P_TE_E1;
		else
			rq.protocol = ISDN_P_TE_S0;
		rq.adr.channel = 0;
		l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
		break;
	case ISDN_P_B_X75SLP:
		test_and_set_bit(FLG_LAPB, &l2->flag);
		l2->window = 7;
		l2->maxlen = MAX_DATA_SIZE;
		l2->T200 = 1000;
		l2->N200 = 4;
		l2->T203 = 5000;
		l2->addr.A = 3;
		l2->addr.B = 1;
		break;
	default:
		printk(KERN_ERR "layer2 create failed prt %x\n",
2214
		       protocol);
Karsten Keil's avatar
Karsten Keil committed
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
		kfree(l2);
		return NULL;
	}
	skb_queue_head_init(&l2->i_queue);
	skb_queue_head_init(&l2->ui_queue);
	skb_queue_head_init(&l2->down_queue);
	skb_queue_head_init(&l2->tmp_queue);
	InitWin(l2);
	l2->l2m.fsm = &l2fsm;
	if (test_bit(FLG_LAPB, &l2->flag) ||
2225 2226
	    test_bit(FLG_PTP, &l2->flag) ||
	    test_bit(FLG_LAPD_NET, &l2->flag))
Karsten Keil's avatar
Karsten Keil committed
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
		l2->l2m.state = ST_L2_4;
	else
		l2->l2m.state = ST_L2_1;
	l2->l2m.debug = *debug;
	l2->l2m.userdata = l2;
	l2->l2m.userint = 0;
	l2->l2m.printdebug = l2m_debug;

	mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
	mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
	return l2;
}

static int
x75create(struct channel_req *crq)
{
	struct layer2	*l2;

	if (crq->protocol != ISDN_P_B_X75SLP)
		return -EPROTONOSUPPORT;
2247
	l2 = create_l2(crq->ch, crq->protocol, 0, 0, 0);
Karsten Keil's avatar
Karsten Keil committed
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
	if (!l2)
		return -ENOMEM;
	crq->ch = &l2->ch;
	crq->protocol = ISDN_P_B_HDLC;
	return 0;
}

static struct Bprotocol X75SLP = {
	.Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
	.name = "X75SLP",
	.create = x75create
};

int
Isdnl2_Init(u_int *deb)
{
	debug = deb;
	mISDN_register_Bprotocol(&X75SLP);
	l2fsm.state_count = L2_STATE_COUNT;
	l2fsm.event_count = L2_EVENT_COUNT;
	l2fsm.strEvent = strL2Event;
	l2fsm.strState = strL2State;
	mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
	TEIInit(deb);
	return 0;
}

void
Isdnl2_cleanup(void)
{
	mISDN_unregister_Bprotocol(&X75SLP);
	TEIFree();
	mISDN_FsmFree(&l2fsm);
}