linux/drivers/isdn/mISDN/layer2.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *
   4 * Author       Karsten Keil <kkeil@novell.com>
   5 *
   6 * Copyright 2008  by Karsten Keil <kkeil@novell.com>
   7 */
   8
   9#include <linux/mISDNif.h>
  10#include <linux/slab.h>
  11#include "core.h"
  12#include "fsm.h"
  13#include "layer2.h"
  14
  15static u_int *debug;
  16
  17static
  18struct Fsm l2fsm = {NULL, 0, 0, NULL, NULL};
  19
  20static char *strL2State[] =
  21{
  22        "ST_L2_1",
  23        "ST_L2_2",
  24        "ST_L2_3",
  25        "ST_L2_4",
  26        "ST_L2_5",
  27        "ST_L2_6",
  28        "ST_L2_7",
  29        "ST_L2_8",
  30};
  31
  32enum {
  33        EV_L2_UI,
  34        EV_L2_SABME,
  35        EV_L2_DISC,
  36        EV_L2_DM,
  37        EV_L2_UA,
  38        EV_L2_FRMR,
  39        EV_L2_SUPER,
  40        EV_L2_I,
  41        EV_L2_DL_DATA,
  42        EV_L2_ACK_PULL,
  43        EV_L2_DL_UNITDATA,
  44        EV_L2_DL_ESTABLISH_REQ,
  45        EV_L2_DL_RELEASE_REQ,
  46        EV_L2_MDL_ASSIGN,
  47        EV_L2_MDL_REMOVE,
  48        EV_L2_MDL_ERROR,
  49        EV_L1_DEACTIVATE,
  50        EV_L2_T200,
  51        EV_L2_T203,
  52        EV_L2_T200I,
  53        EV_L2_T203I,
  54        EV_L2_SET_OWN_BUSY,
  55        EV_L2_CLEAR_OWN_BUSY,
  56        EV_L2_FRAME_ERROR,
  57};
  58
  59#define L2_EVENT_COUNT (EV_L2_FRAME_ERROR + 1)
  60
  61static char *strL2Event[] =
  62{
  63        "EV_L2_UI",
  64        "EV_L2_SABME",
  65        "EV_L2_DISC",
  66        "EV_L2_DM",
  67        "EV_L2_UA",
  68        "EV_L2_FRMR",
  69        "EV_L2_SUPER",
  70        "EV_L2_I",
  71        "EV_L2_DL_DATA",
  72        "EV_L2_ACK_PULL",
  73        "EV_L2_DL_UNITDATA",
  74        "EV_L2_DL_ESTABLISH_REQ",
  75        "EV_L2_DL_RELEASE_REQ",
  76        "EV_L2_MDL_ASSIGN",
  77        "EV_L2_MDL_REMOVE",
  78        "EV_L2_MDL_ERROR",
  79        "EV_L1_DEACTIVATE",
  80        "EV_L2_T200",
  81        "EV_L2_T203",
  82        "EV_L2_T200I",
  83        "EV_L2_T203I",
  84        "EV_L2_SET_OWN_BUSY",
  85        "EV_L2_CLEAR_OWN_BUSY",
  86        "EV_L2_FRAME_ERROR",
  87};
  88
  89static void
  90l2m_debug(struct FsmInst *fi, char *fmt, ...)
  91{
  92        struct layer2 *l2 = fi->userdata;
  93        struct va_format vaf;
  94        va_list va;
  95
  96        if (!(*debug & DEBUG_L2_FSM))
  97                return;
  98
  99        va_start(va, fmt);
 100
 101        vaf.fmt = fmt;
 102        vaf.va = &va;
 103
 104        printk(KERN_DEBUG "%s l2 (sapi %d tei %d): %pV\n",
 105               mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei, &vaf);
 106
 107        va_end(va);
 108}
 109
 110inline u_int
 111l2headersize(struct layer2 *l2, int ui)
 112{
 113        return ((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
 114                (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
 115}
 116
 117inline u_int
 118l2addrsize(struct layer2 *l2)
 119{
 120        return test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
 121}
 122
 123static u_int
 124l2_newid(struct layer2 *l2)
 125{
 126        u_int   id;
 127
 128        id = l2->next_id++;
 129        if (id == 0x7fff)
 130                l2->next_id = 1;
 131        id <<= 16;
 132        id |= l2->tei << 8;
 133        id |= l2->sapi;
 134        return id;
 135}
 136
 137static void
 138l2up(struct layer2 *l2, u_int prim, struct sk_buff *skb)
 139{
 140        int     err;
 141
 142        if (!l2->up)
 143                return;
 144        mISDN_HEAD_PRIM(skb) = prim;
 145        mISDN_HEAD_ID(skb) = (l2->ch.nr << 16) | l2->ch.addr;
 146        err = l2->up->send(l2->up, skb);
 147        if (err) {
 148                printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
 149                       mISDNDevName4ch(&l2->ch), err);
 150                dev_kfree_skb(skb);
 151        }
 152}
 153
 154static void
 155l2up_create(struct layer2 *l2, u_int prim, int len, void *arg)
 156{
 157        struct sk_buff  *skb;
 158        struct mISDNhead *hh;
 159        int             err;
 160
 161        if (!l2->up)
 162                return;
 163        skb = mI_alloc_skb(len, GFP_ATOMIC);
 164        if (!skb)
 165                return;
 166        hh = mISDN_HEAD_P(skb);
 167        hh->prim = prim;
 168        hh->id = (l2->ch.nr << 16) | l2->ch.addr;
 169        if (len)
 170                skb_put_data(skb, arg, len);
 171        err = l2->up->send(l2->up, skb);
 172        if (err) {
 173                printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
 174                       mISDNDevName4ch(&l2->ch), err);
 175                dev_kfree_skb(skb);
 176        }
 177}
 178
 179static int
 180l2down_skb(struct layer2 *l2, struct sk_buff *skb) {
 181        int ret;
 182
 183        ret = l2->ch.recv(l2->ch.peer, skb);
 184        if (ret && (*debug & DEBUG_L2_RECV))
 185                printk(KERN_DEBUG "l2down_skb: dev %s ret(%d)\n",
 186                       mISDNDevName4ch(&l2->ch), ret);
 187        return ret;
 188}
 189
 190static int
 191l2down_raw(struct layer2 *l2, struct sk_buff *skb)
 192{
 193        struct mISDNhead *hh = mISDN_HEAD_P(skb);
 194
 195        if (hh->prim == PH_DATA_REQ) {
 196                if (test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
 197                        skb_queue_tail(&l2->down_queue, skb);
 198                        return 0;
 199                }
 200                l2->down_id = mISDN_HEAD_ID(skb);
 201        }
 202        return l2down_skb(l2, skb);
 203}
 204
 205static int
 206l2down(struct layer2 *l2, u_int prim, u_int id, struct sk_buff *skb)
 207{
 208        struct mISDNhead *hh = mISDN_HEAD_P(skb);
 209
 210        hh->prim = prim;
 211        hh->id = id;
 212        return l2down_raw(l2, skb);
 213}
 214
 215static int
 216l2down_create(struct layer2 *l2, u_int prim, u_int id, int len, void *arg)
 217{
 218        struct sk_buff  *skb;
 219        int             err;
 220        struct mISDNhead *hh;
 221
 222        skb = mI_alloc_skb(len, GFP_ATOMIC);
 223        if (!skb)
 224                return -ENOMEM;
 225        hh = mISDN_HEAD_P(skb);
 226        hh->prim = prim;
 227        hh->id = id;
 228        if (len)
 229                skb_put_data(skb, arg, len);
 230        err = l2down_raw(l2, skb);
 231        if (err)
 232                dev_kfree_skb(skb);
 233        return err;
 234}
 235
 236static int
 237ph_data_confirm(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb) {
 238        struct sk_buff *nskb = skb;
 239        int ret = -EAGAIN;
 240
 241        if (test_bit(FLG_L1_NOTREADY, &l2->flag)) {
 242                if (hh->id == l2->down_id) {
 243                        nskb = skb_dequeue(&l2->down_queue);
 244                        if (nskb) {
 245                                l2->down_id = mISDN_HEAD_ID(nskb);
 246                                if (l2down_skb(l2, nskb)) {
 247                                        dev_kfree_skb(nskb);
 248                                        l2->down_id = MISDN_ID_NONE;
 249                                }
 250                        } else
 251                                l2->down_id = MISDN_ID_NONE;
 252                        if (ret) {
 253                                dev_kfree_skb(skb);
 254                                ret = 0;
 255                        }
 256                        if (l2->down_id == MISDN_ID_NONE) {
 257                                test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
 258                                mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
 259                        }
 260                }
 261        }
 262        if (!test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
 263                nskb = skb_dequeue(&l2->down_queue);
 264                if (nskb) {
 265                        l2->down_id = mISDN_HEAD_ID(nskb);
 266                        if (l2down_skb(l2, nskb)) {
 267                                dev_kfree_skb(nskb);
 268                                l2->down_id = MISDN_ID_NONE;
 269                                test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
 270                        }
 271                } else
 272                        test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
 273        }
 274        return ret;
 275}
 276
 277static void
 278l2_timeout(struct FsmInst *fi, int event, void *arg)
 279{
 280        struct layer2 *l2 = fi->userdata;
 281        struct sk_buff *skb;
 282        struct mISDNhead *hh;
 283
 284        skb = mI_alloc_skb(0, GFP_ATOMIC);
 285        if (!skb) {
 286                printk(KERN_WARNING "%s: L2(%d,%d) nr:%x timer %s no skb\n",
 287                       mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
 288                       l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
 289                return;
 290        }
 291        hh = mISDN_HEAD_P(skb);
 292        hh->prim = event == EV_L2_T200 ? DL_TIMER200_IND : DL_TIMER203_IND;
 293        hh->id = l2->ch.nr;
 294        if (*debug & DEBUG_TIMER)
 295                printk(KERN_DEBUG "%s: L2(%d,%d) nr:%x timer %s expired\n",
 296                       mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
 297                       l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
 298        if (l2->ch.st)
 299                l2->ch.st->own.recv(&l2->ch.st->own, skb);
 300}
 301
 302static int
 303l2mgr(struct layer2 *l2, u_int prim, void *arg) {
 304        long c = (long)arg;
 305
 306        printk(KERN_WARNING "l2mgr: dev %s addr:%x prim %x %c\n",
 307               mISDNDevName4ch(&l2->ch), l2->id, prim, (char)c);
 308        if (test_bit(FLG_LAPD, &l2->flag) &&
 309            !test_bit(FLG_FIXED_TEI, &l2->flag)) {
 310                switch (c) {
 311                case 'C':
 312                case 'D':
 313                case 'G':
 314                case 'H':
 315                        l2_tei(l2, prim, (u_long)arg);
 316                        break;
 317                }
 318        }
 319        return 0;
 320}
 321
 322static void
 323set_peer_busy(struct layer2 *l2) {
 324        test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
 325        if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
 326                test_and_set_bit(FLG_L2BLOCK, &l2->flag);
 327}
 328
 329static void
 330clear_peer_busy(struct layer2 *l2) {
 331        if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
 332                test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
 333}
 334
 335static void
 336InitWin(struct layer2 *l2)
 337{
 338        int i;
 339
 340        for (i = 0; i < MAX_WINDOW; i++)
 341                l2->windowar[i] = NULL;
 342}
 343
 344static int
 345freewin(struct layer2 *l2)
 346{
 347        int i, cnt = 0;
 348
 349        for (i = 0; i < MAX_WINDOW; i++) {
 350                if (l2->windowar[i]) {
 351                        cnt++;
 352                        dev_kfree_skb(l2->windowar[i]);
 353                        l2->windowar[i] = NULL;
 354                }
 355        }
 356        return cnt;
 357}
 358
 359static void
 360ReleaseWin(struct layer2 *l2)
 361{
 362        int cnt = freewin(l2);
 363
 364        if (cnt)
 365                printk(KERN_WARNING
 366                       "isdnl2 freed %d skbuffs in release\n", cnt);
 367}
 368
 369inline unsigned int
 370cansend(struct layer2 *l2)
 371{
 372        unsigned int p1;
 373
 374        if (test_bit(FLG_MOD128, &l2->flag))
 375                p1 = (l2->vs - l2->va) % 128;
 376        else
 377                p1 = (l2->vs - l2->va) % 8;
 378        return (p1 < l2->window) && !test_bit(FLG_PEER_BUSY, &l2->flag);
 379}
 380
 381inline void
 382clear_exception(struct layer2 *l2)
 383{
 384        test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
 385        test_and_clear_bit(FLG_REJEXC, &l2->flag);
 386        test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
 387        clear_peer_busy(l2);
 388}
 389
 390static int
 391sethdraddr(struct layer2 *l2, u_char *header, int rsp)
 392{
 393        u_char *ptr = header;
 394        int crbit = rsp;
 395
 396        if (test_bit(FLG_LAPD, &l2->flag)) {
 397                if (test_bit(FLG_LAPD_NET, &l2->flag))
 398                        crbit = !crbit;
 399                *ptr++ = (l2->sapi << 2) | (crbit ? 2 : 0);
 400                *ptr++ = (l2->tei << 1) | 1;
 401                return 2;
 402        } else {
 403                if (test_bit(FLG_ORIG, &l2->flag))
 404                        crbit = !crbit;
 405                if (crbit)
 406                        *ptr++ = l2->addr.B;
 407                else
 408                        *ptr++ = l2->addr.A;
 409                return 1;
 410        }
 411}
 412
 413static inline void
 414enqueue_super(struct layer2 *l2, struct sk_buff *skb)
 415{
 416        if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
 417                dev_kfree_skb(skb);
 418}
 419
 420static inline void
 421enqueue_ui(struct layer2 *l2, struct sk_buff *skb)
 422{
 423        if (l2->tm)
 424                l2_tei(l2, MDL_STATUS_UI_IND, 0);
 425        if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
 426                dev_kfree_skb(skb);
 427}
 428
 429inline int
 430IsUI(u_char *data)
 431{
 432        return (data[0] & 0xef) == UI;
 433}
 434
 435inline int
 436IsUA(u_char *data)
 437{
 438        return (data[0] & 0xef) == UA;
 439}
 440
 441inline int
 442IsDM(u_char *data)
 443{
 444        return (data[0] & 0xef) == DM;
 445}
 446
 447inline int
 448IsDISC(u_char *data)
 449{
 450        return (data[0] & 0xef) == DISC;
 451}
 452
 453inline int
 454IsRR(u_char *data, struct layer2 *l2)
 455{
 456        if (test_bit(FLG_MOD128, &l2->flag))
 457                return data[0] == RR;
 458        else
 459                return (data[0] & 0xf) == 1;
 460}
 461
 462inline int
 463IsSFrame(u_char *data, struct layer2 *l2)
 464{
 465        register u_char d = *data;
 466
 467        if (!test_bit(FLG_MOD128, &l2->flag))
 468                d &= 0xf;
 469        return ((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c);
 470}
 471
 472inline int
 473IsSABME(u_char *data, struct layer2 *l2)
 474{
 475        u_char d = data[0] & ~0x10;
 476
 477        return test_bit(FLG_MOD128, &l2->flag) ? d == SABME : d == SABM;
 478}
 479
 480inline int
 481IsREJ(u_char *data, struct layer2 *l2)
 482{
 483        return test_bit(FLG_MOD128, &l2->flag) ?
 484                data[0] == REJ : (data[0] & 0xf) == REJ;
 485}
 486
 487inline int
 488IsFRMR(u_char *data)
 489{
 490        return (data[0] & 0xef) == FRMR;
 491}
 492
 493inline int
 494IsRNR(u_char *data, struct layer2 *l2)
 495{
 496        return test_bit(FLG_MOD128, &l2->flag) ?
 497                data[0] == RNR : (data[0] & 0xf) == RNR;
 498}
 499
 500static int
 501iframe_error(struct layer2 *l2, struct sk_buff *skb)
 502{
 503        u_int   i;
 504        int     rsp = *skb->data & 0x2;
 505
 506        i = l2addrsize(l2) + (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1);
 507        if (test_bit(FLG_ORIG, &l2->flag))
 508                rsp = !rsp;
 509        if (rsp)
 510                return 'L';
 511        if (skb->len < i)
 512                return 'N';
 513        if ((skb->len - i) > l2->maxlen)
 514                return 'O';
 515        return 0;
 516}
 517
 518static int
 519super_error(struct layer2 *l2, struct sk_buff *skb)
 520{
 521        if (skb->len != l2addrsize(l2) +
 522            (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1))
 523                return 'N';
 524        return 0;
 525}
 526
 527static int
 528unnum_error(struct layer2 *l2, struct sk_buff *skb, int wantrsp)
 529{
 530        int rsp = (*skb->data & 0x2) >> 1;
 531        if (test_bit(FLG_ORIG, &l2->flag))
 532                rsp = !rsp;
 533        if (rsp != wantrsp)
 534                return 'L';
 535        if (skb->len != l2addrsize(l2) + 1)
 536                return 'N';
 537        return 0;
 538}
 539
 540static int
 541UI_error(struct layer2 *l2, struct sk_buff *skb)
 542{
 543        int rsp = *skb->data & 0x2;
 544        if (test_bit(FLG_ORIG, &l2->flag))
 545                rsp = !rsp;
 546        if (rsp)
 547                return 'L';
 548        if (skb->len > l2->maxlen + l2addrsize(l2) + 1)
 549                return 'O';
 550        return 0;
 551}
 552
 553static int
 554FRMR_error(struct layer2 *l2, struct sk_buff *skb)
 555{
 556        u_int   headers = l2addrsize(l2) + 1;
 557        u_char  *datap = skb->data + headers;
 558        int     rsp = *skb->data & 0x2;
 559
 560        if (test_bit(FLG_ORIG, &l2->flag))
 561                rsp = !rsp;
 562        if (!rsp)
 563                return 'L';
 564        if (test_bit(FLG_MOD128, &l2->flag)) {
 565                if (skb->len < headers + 5)
 566                        return 'N';
 567                else if (*debug & DEBUG_L2)
 568                        l2m_debug(&l2->l2m,
 569                                  "FRMR information %2x %2x %2x %2x %2x",
 570                                  datap[0], datap[1], datap[2], datap[3], datap[4]);
 571        } else {
 572                if (skb->len < headers + 3)
 573                        return 'N';
 574                else if (*debug & DEBUG_L2)
 575                        l2m_debug(&l2->l2m,
 576                                  "FRMR information %2x %2x %2x",
 577                                  datap[0], datap[1], datap[2]);
 578        }
 579        return 0;
 580}
 581
 582static unsigned int
 583legalnr(struct layer2 *l2, unsigned int nr)
 584{
 585        if (test_bit(FLG_MOD128, &l2->flag))
 586                return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
 587        else
 588                return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
 589}
 590
 591static void
 592setva(struct layer2 *l2, unsigned int nr)
 593{
 594        struct sk_buff  *skb;
 595
 596        while (l2->va != nr) {
 597                l2->va++;
 598                if (test_bit(FLG_MOD128, &l2->flag))
 599                        l2->va %= 128;
 600                else
 601                        l2->va %= 8;
 602                if (l2->windowar[l2->sow]) {
 603                        skb_trim(l2->windowar[l2->sow], 0);
 604                        skb_queue_tail(&l2->tmp_queue, l2->windowar[l2->sow]);
 605                        l2->windowar[l2->sow] = NULL;
 606                }
 607                l2->sow = (l2->sow + 1) % l2->window;
 608        }
 609        skb = skb_dequeue(&l2->tmp_queue);
 610        while (skb) {
 611                dev_kfree_skb(skb);
 612                skb = skb_dequeue(&l2->tmp_queue);
 613        }
 614}
 615
 616static void
 617send_uframe(struct layer2 *l2, struct sk_buff *skb, u_char cmd, u_char cr)
 618{
 619        u_char tmp[MAX_L2HEADER_LEN];
 620        int i;
 621
 622        i = sethdraddr(l2, tmp, cr);
 623        tmp[i++] = cmd;
 624        if (skb)
 625                skb_trim(skb, 0);
 626        else {
 627                skb = mI_alloc_skb(i, GFP_ATOMIC);
 628                if (!skb) {
 629                        printk(KERN_WARNING "%s: can't alloc skbuff in %s\n",
 630                               mISDNDevName4ch(&l2->ch), __func__);
 631                        return;
 632                }
 633        }
 634        skb_put_data(skb, tmp, i);
 635        enqueue_super(l2, skb);
 636}
 637
 638
 639inline u_char
 640get_PollFlag(struct layer2 *l2, struct sk_buff *skb)
 641{
 642        return skb->data[l2addrsize(l2)] & 0x10;
 643}
 644
 645inline u_char
 646get_PollFlagFree(struct layer2 *l2, struct sk_buff *skb)
 647{
 648        u_char PF;
 649
 650        PF = get_PollFlag(l2, skb);
 651        dev_kfree_skb(skb);
 652        return PF;
 653}
 654
 655inline void
 656start_t200(struct layer2 *l2, int i)
 657{
 658        mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
 659        test_and_set_bit(FLG_T200_RUN, &l2->flag);
 660}
 661
 662inline void
 663restart_t200(struct layer2 *l2, int i)
 664{
 665        mISDN_FsmRestartTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
 666        test_and_set_bit(FLG_T200_RUN, &l2->flag);
 667}
 668
 669inline void
 670stop_t200(struct layer2 *l2, int i)
 671{
 672        if (test_and_clear_bit(FLG_T200_RUN, &l2->flag))
 673                mISDN_FsmDelTimer(&l2->t200, i);
 674}
 675
 676inline void
 677st5_dl_release_l2l3(struct layer2 *l2)
 678{
 679        int pr;
 680
 681        if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
 682                pr = DL_RELEASE_CNF;
 683        else
 684                pr = DL_RELEASE_IND;
 685        l2up_create(l2, pr, 0, NULL);
 686}
 687
 688inline void
 689lapb_dl_release_l2l3(struct layer2 *l2, int f)
 690{
 691        if (test_bit(FLG_LAPB, &l2->flag))
 692                l2down_create(l2, PH_DEACTIVATE_REQ, l2_newid(l2), 0, NULL);
 693        l2up_create(l2, f, 0, NULL);
 694}
 695
 696static void
 697establishlink(struct FsmInst *fi)
 698{
 699        struct layer2 *l2 = fi->userdata;
 700        u_char cmd;
 701
 702        clear_exception(l2);
 703        l2->rc = 0;
 704        cmd = (test_bit(FLG_MOD128, &l2->flag) ? SABME : SABM) | 0x10;
 705        send_uframe(l2, NULL, cmd, CMD);
 706        mISDN_FsmDelTimer(&l2->t203, 1);
 707        restart_t200(l2, 1);
 708        test_and_clear_bit(FLG_PEND_REL, &l2->flag);
 709        freewin(l2);
 710        mISDN_FsmChangeState(fi, ST_L2_5);
 711}
 712
 713static void
 714l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
 715{
 716        struct sk_buff *skb = arg;
 717        struct layer2 *l2 = fi->userdata;
 718
 719        if (get_PollFlagFree(l2, skb))
 720                l2mgr(l2, MDL_ERROR_IND, (void *) 'C');
 721        else
 722                l2mgr(l2, MDL_ERROR_IND, (void *) 'D');
 723
 724}
 725
 726static void
 727l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
 728{
 729        struct sk_buff *skb = arg;
 730        struct layer2 *l2 = fi->userdata;
 731
 732        if (get_PollFlagFree(l2, skb))
 733                l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
 734        else {
 735                l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
 736                establishlink(fi);
 737                test_and_clear_bit(FLG_L3_INIT, &l2->flag);
 738        }
 739}
 740
 741static void
 742l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
 743{
 744        struct sk_buff *skb = arg;
 745        struct layer2 *l2 = fi->userdata;
 746
 747        if (get_PollFlagFree(l2, skb))
 748                l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
 749        else
 750                l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
 751        establishlink(fi);
 752        test_and_clear_bit(FLG_L3_INIT, &l2->flag);
 753}
 754
 755static void
 756l2_go_st3(struct FsmInst *fi, int event, void *arg)
 757{
 758        dev_kfree_skb((struct sk_buff *)arg);
 759        mISDN_FsmChangeState(fi, ST_L2_3);
 760}
 761
 762static void
 763l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
 764{
 765        struct layer2   *l2 = fi->userdata;
 766
 767        mISDN_FsmChangeState(fi, ST_L2_3);
 768        dev_kfree_skb((struct sk_buff *)arg);
 769        l2_tei(l2, MDL_ASSIGN_IND, 0);
 770}
 771
 772static void
 773l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
 774{
 775        struct layer2 *l2 = fi->userdata;
 776        struct sk_buff *skb = arg;
 777
 778        skb_queue_tail(&l2->ui_queue, skb);
 779        mISDN_FsmChangeState(fi, ST_L2_2);
 780        l2_tei(l2, MDL_ASSIGN_IND, 0);
 781}
 782
 783static void
 784l2_queue_ui(struct FsmInst *fi, int event, void *arg)
 785{
 786        struct layer2 *l2 = fi->userdata;
 787        struct sk_buff *skb = arg;
 788
 789        skb_queue_tail(&l2->ui_queue, skb);
 790}
 791
 792static void
 793tx_ui(struct layer2 *l2)
 794{
 795        struct sk_buff *skb;
 796        u_char header[MAX_L2HEADER_LEN];
 797        int i;
 798
 799        i = sethdraddr(l2, header, CMD);
 800        if (test_bit(FLG_LAPD_NET, &l2->flag))
 801                header[1] = 0xff; /* tei 127 */
 802        header[i++] = UI;
 803        while ((skb = skb_dequeue(&l2->ui_queue))) {
 804                memcpy(skb_push(skb, i), header, i);
 805                enqueue_ui(l2, skb);
 806        }
 807}
 808
 809static void
 810l2_send_ui(struct FsmInst *fi, int event, void *arg)
 811{
 812        struct layer2 *l2 = fi->userdata;
 813        struct sk_buff *skb = arg;
 814
 815        skb_queue_tail(&l2->ui_queue, skb);
 816        tx_ui(l2);
 817}
 818
 819static void
 820l2_got_ui(struct FsmInst *fi, int event, void *arg)
 821{
 822        struct layer2 *l2 = fi->userdata;
 823        struct sk_buff *skb = arg;
 824
 825        skb_pull(skb, l2headersize(l2, 1));
 826/*
 827 *              in states 1-3 for broadcast
 828 */
 829
 830        if (l2->tm)
 831                l2_tei(l2, MDL_STATUS_UI_IND, 0);
 832        l2up(l2, DL_UNITDATA_IND, skb);
 833}
 834
 835static void
 836l2_establish(struct FsmInst *fi, int event, void *arg)
 837{
 838        struct sk_buff *skb = arg;
 839        struct layer2 *l2 = fi->userdata;
 840
 841        establishlink(fi);
 842        test_and_set_bit(FLG_L3_INIT, &l2->flag);
 843        dev_kfree_skb(skb);
 844}
 845
 846static void
 847l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
 848{
 849        struct sk_buff *skb = arg;
 850        struct layer2 *l2 = fi->userdata;
 851
 852        skb_queue_purge(&l2->i_queue);
 853        test_and_set_bit(FLG_L3_INIT, &l2->flag);
 854        test_and_clear_bit(FLG_PEND_REL, &l2->flag);
 855        dev_kfree_skb(skb);
 856}
 857
 858static void
 859l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
 860{
 861        struct sk_buff *skb = arg;
 862        struct layer2 *l2 = fi->userdata;
 863
 864        skb_queue_purge(&l2->i_queue);
 865        establishlink(fi);
 866        test_and_set_bit(FLG_L3_INIT, &l2->flag);
 867        dev_kfree_skb(skb);
 868}
 869
 870static void
 871l2_release(struct FsmInst *fi, int event, void *arg)
 872{
 873        struct layer2 *l2 = fi->userdata;
 874        struct sk_buff *skb = arg;
 875
 876        skb_trim(skb, 0);
 877        l2up(l2, DL_RELEASE_CNF, skb);
 878}
 879
 880static void
 881l2_pend_rel(struct FsmInst *fi, int event, void *arg)
 882{
 883        struct sk_buff *skb = arg;
 884        struct layer2 *l2 = fi->userdata;
 885
 886        test_and_set_bit(FLG_PEND_REL, &l2->flag);
 887        dev_kfree_skb(skb);
 888}
 889
 890static void
 891l2_disconnect(struct FsmInst *fi, int event, void *arg)
 892{
 893        struct layer2 *l2 = fi->userdata;
 894        struct sk_buff *skb = arg;
 895
 896        skb_queue_purge(&l2->i_queue);
 897        freewin(l2);
 898        mISDN_FsmChangeState(fi, ST_L2_6);
 899        l2->rc = 0;
 900        send_uframe(l2, NULL, DISC | 0x10, CMD);
 901        mISDN_FsmDelTimer(&l2->t203, 1);
 902        restart_t200(l2, 2);
 903        if (skb)
 904                dev_kfree_skb(skb);
 905}
 906
 907static void
 908l2_start_multi(struct FsmInst *fi, int event, void *arg)
 909{
 910        struct layer2   *l2 = fi->userdata;
 911        struct sk_buff  *skb = arg;
 912
 913        l2->vs = 0;
 914        l2->va = 0;
 915        l2->vr = 0;
 916        l2->sow = 0;
 917        clear_exception(l2);
 918        send_uframe(l2, NULL, UA | get_PollFlag(l2, skb), RSP);
 919        mISDN_FsmChangeState(fi, ST_L2_7);
 920        mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
 921        skb_trim(skb, 0);
 922        l2up(l2, DL_ESTABLISH_IND, skb);
 923        if (l2->tm)
 924                l2_tei(l2, MDL_STATUS_UP_IND, 0);
 925}
 926
 927static void
 928l2_send_UA(struct FsmInst *fi, int event, void *arg)
 929{
 930        struct layer2 *l2 = fi->userdata;
 931        struct sk_buff *skb = arg;
 932
 933        send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
 934}
 935
 936static void
 937l2_send_DM(struct FsmInst *fi, int event, void *arg)
 938{
 939        struct layer2 *l2 = fi->userdata;
 940        struct sk_buff *skb = arg;
 941
 942        send_uframe(l2, skb, DM | get_PollFlag(l2, skb), RSP);
 943}
 944
 945static void
 946l2_restart_multi(struct FsmInst *fi, int event, void *arg)
 947{
 948        struct layer2   *l2 = fi->userdata;
 949        struct sk_buff  *skb = arg;
 950        int             est = 0;
 951
 952        send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
 953
 954        l2mgr(l2, MDL_ERROR_IND, (void *) 'F');
 955
 956        if (l2->vs != l2->va) {
 957                skb_queue_purge(&l2->i_queue);
 958                est = 1;
 959        }
 960
 961        clear_exception(l2);
 962        l2->vs = 0;
 963        l2->va = 0;
 964        l2->vr = 0;
 965        l2->sow = 0;
 966        mISDN_FsmChangeState(fi, ST_L2_7);
 967        stop_t200(l2, 3);
 968        mISDN_FsmRestartTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
 969
 970        if (est)
 971                l2up_create(l2, DL_ESTABLISH_IND, 0, NULL);
 972/*              mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
 973 *                  MGR_SHORTSTATUS | INDICATION, SSTATUS_L2_ESTABLISHED,
 974 *                  0, NULL, 0);
 975 */
 976        if (skb_queue_len(&l2->i_queue) && cansend(l2))
 977                mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
 978}
 979
 980static void
 981l2_stop_multi(struct FsmInst *fi, int event, void *arg)
 982{
 983        struct layer2   *l2 = fi->userdata;
 984        struct sk_buff  *skb = arg;
 985
 986        mISDN_FsmChangeState(fi, ST_L2_4);
 987        mISDN_FsmDelTimer(&l2->t203, 3);
 988        stop_t200(l2, 4);
 989
 990        send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
 991        skb_queue_purge(&l2->i_queue);
 992        freewin(l2);
 993        lapb_dl_release_l2l3(l2, DL_RELEASE_IND);
 994        if (l2->tm)
 995                l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
 996}
 997
 998static void
 999l2_connected(struct FsmInst *fi, int event, void *arg)
1000{
1001        struct layer2   *l2 = fi->userdata;
1002        struct sk_buff  *skb = arg;
1003        int pr = -1;
1004
1005        if (!get_PollFlag(l2, skb)) {
1006                l2_mdl_error_ua(fi, event, arg);
1007                return;
1008        }
1009        dev_kfree_skb(skb);
1010        if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
1011                l2_disconnect(fi, event, NULL);
1012        if (test_and_clear_bit(FLG_L3_INIT, &l2->flag)) {
1013                pr = DL_ESTABLISH_CNF;
1014        } else if (l2->vs != l2->va) {
1015                skb_queue_purge(&l2->i_queue);
1016                pr = DL_ESTABLISH_IND;
1017        }
1018        stop_t200(l2, 5);
1019        l2->vr = 0;
1020        l2->vs = 0;
1021        l2->va = 0;
1022        l2->sow = 0;
1023        mISDN_FsmChangeState(fi, ST_L2_7);
1024        mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 4);
1025        if (pr != -1)
1026                l2up_create(l2, pr, 0, NULL);
1027
1028        if (skb_queue_len(&l2->i_queue) && cansend(l2))
1029                mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1030
1031        if (l2->tm)
1032                l2_tei(l2, MDL_STATUS_UP_IND, 0);
1033}
1034
1035static void
1036l2_released(struct FsmInst *fi, int event, void *arg)
1037{
1038        struct layer2 *l2 = fi->userdata;
1039        struct sk_buff *skb = arg;
1040
1041        if (!get_PollFlag(l2, skb)) {
1042                l2_mdl_error_ua(fi, event, arg);
1043                return;
1044        }
1045        dev_kfree_skb(skb);
1046        stop_t200(l2, 6);
1047        lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1048        mISDN_FsmChangeState(fi, ST_L2_4);
1049        if (l2->tm)
1050                l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1051}
1052
1053static void
1054l2_reestablish(struct FsmInst *fi, int event, void *arg)
1055{
1056        struct layer2 *l2 = fi->userdata;
1057        struct sk_buff *skb = arg;
1058
1059        if (!get_PollFlagFree(l2, skb)) {
1060                establishlink(fi);
1061                test_and_set_bit(FLG_L3_INIT, &l2->flag);
1062        }
1063}
1064
1065static void
1066l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
1067{
1068        struct layer2 *l2 = fi->userdata;
1069        struct sk_buff *skb = arg;
1070
1071        if (get_PollFlagFree(l2, skb)) {
1072                stop_t200(l2, 7);
1073                if (!test_bit(FLG_L3_INIT, &l2->flag))
1074                        skb_queue_purge(&l2->i_queue);
1075                if (test_bit(FLG_LAPB, &l2->flag))
1076                        l2down_create(l2, PH_DEACTIVATE_REQ,
1077                                      l2_newid(l2), 0, NULL);
1078                st5_dl_release_l2l3(l2);
1079                mISDN_FsmChangeState(fi, ST_L2_4);
1080                if (l2->tm)
1081                        l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1082        }
1083}
1084
1085static void
1086l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
1087{
1088        struct layer2 *l2 = fi->userdata;
1089        struct sk_buff *skb = arg;
1090
1091        if (get_PollFlagFree(l2, skb)) {
1092                stop_t200(l2, 8);
1093                lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1094                mISDN_FsmChangeState(fi, ST_L2_4);
1095                if (l2->tm)
1096                        l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1097        }
1098}
1099
1100static void
1101enquiry_cr(struct layer2 *l2, u_char typ, u_char cr, u_char pf)
1102{
1103        struct sk_buff *skb;
1104        u_char tmp[MAX_L2HEADER_LEN];
1105        int i;
1106
1107        i = sethdraddr(l2, tmp, cr);
1108        if (test_bit(FLG_MOD128, &l2->flag)) {
1109                tmp[i++] = typ;
1110                tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
1111        } else
1112                tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
1113        skb = mI_alloc_skb(i, GFP_ATOMIC);
1114        if (!skb) {
1115                printk(KERN_WARNING "%s: isdnl2 can't alloc sbbuff in %s\n",
1116                       mISDNDevName4ch(&l2->ch), __func__);
1117                return;
1118        }
1119        skb_put_data(skb, tmp, i);
1120        enqueue_super(l2, skb);
1121}
1122
1123inline void
1124enquiry_response(struct layer2 *l2)
1125{
1126        if (test_bit(FLG_OWN_BUSY, &l2->flag))
1127                enquiry_cr(l2, RNR, RSP, 1);
1128        else
1129                enquiry_cr(l2, RR, RSP, 1);
1130        test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1131}
1132
1133inline void
1134transmit_enquiry(struct layer2 *l2)
1135{
1136        if (test_bit(FLG_OWN_BUSY, &l2->flag))
1137                enquiry_cr(l2, RNR, CMD, 1);
1138        else
1139                enquiry_cr(l2, RR, CMD, 1);
1140        test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1141        start_t200(l2, 9);
1142}
1143
1144
1145static void
1146nrerrorrecovery(struct FsmInst *fi)
1147{
1148        struct layer2 *l2 = fi->userdata;
1149
1150        l2mgr(l2, MDL_ERROR_IND, (void *) 'J');
1151        establishlink(fi);
1152        test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1153}
1154
1155static void
1156invoke_retransmission(struct layer2 *l2, unsigned int nr)
1157{
1158        u_int   p1;
1159
1160        if (l2->vs != nr) {
1161                while (l2->vs != nr) {
1162                        (l2->vs)--;
1163                        if (test_bit(FLG_MOD128, &l2->flag)) {
1164                                l2->vs %= 128;
1165                                p1 = (l2->vs - l2->va) % 128;
1166                        } else {
1167                                l2->vs %= 8;
1168                                p1 = (l2->vs - l2->va) % 8;
1169                        }
1170                        p1 = (p1 + l2->sow) % l2->window;
1171                        if (l2->windowar[p1])
1172                                skb_queue_head(&l2->i_queue, l2->windowar[p1]);
1173                        else
1174                                printk(KERN_WARNING
1175                                       "%s: windowar[%d] is NULL\n",
1176                                       mISDNDevName4ch(&l2->ch), p1);
1177                        l2->windowar[p1] = NULL;
1178                }
1179                mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
1180        }
1181}
1182
1183static void
1184l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
1185{
1186        struct layer2 *l2 = fi->userdata;
1187        struct sk_buff *skb = arg;
1188        int PollFlag, rsp, typ = RR;
1189        unsigned int nr;
1190
1191        rsp = *skb->data & 0x2;
1192        if (test_bit(FLG_ORIG, &l2->flag))
1193                rsp = !rsp;
1194
1195        skb_pull(skb, l2addrsize(l2));
1196        if (IsRNR(skb->data, l2)) {
1197                set_peer_busy(l2);
1198                typ = RNR;
1199        } else
1200                clear_peer_busy(l2);
1201        if (IsREJ(skb->data, l2))
1202                typ = REJ;
1203
1204        if (test_bit(FLG_MOD128, &l2->flag)) {
1205                PollFlag = (skb->data[1] & 0x1) == 0x1;
1206                nr = skb->data[1] >> 1;
1207        } else {
1208                PollFlag = (skb->data[0] & 0x10);
1209                nr = (skb->data[0] >> 5) & 0x7;
1210        }
1211        dev_kfree_skb(skb);
1212
1213        if (PollFlag) {
1214                if (rsp)
1215                        l2mgr(l2, MDL_ERROR_IND, (void *) 'A');
1216                else
1217                        enquiry_response(l2);
1218        }
1219        if (legalnr(l2, nr)) {
1220                if (typ == REJ) {
1221                        setva(l2, nr);
1222                        invoke_retransmission(l2, nr);
1223                        stop_t200(l2, 10);
1224                        if (mISDN_FsmAddTimer(&l2->t203, l2->T203,
1225                                              EV_L2_T203, NULL, 6))
1226                                l2m_debug(&l2->l2m, "Restart T203 ST7 REJ");
1227                } else if ((nr == l2->vs) && (typ == RR)) {
1228                        setva(l2, nr);
1229                        stop_t200(l2, 11);
1230                        mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1231                                              EV_L2_T203, NULL, 7);
1232                } else if ((l2->va != nr) || (typ == RNR)) {
1233                        setva(l2, nr);
1234                        if (typ != RR)
1235                                mISDN_FsmDelTimer(&l2->t203, 9);
1236                        restart_t200(l2, 12);
1237                }
1238                if (skb_queue_len(&l2->i_queue) && (typ == RR))
1239                        mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1240        } else
1241                nrerrorrecovery(fi);
1242}
1243
1244static void
1245l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
1246{
1247        struct layer2 *l2 = fi->userdata;
1248        struct sk_buff *skb = arg;
1249
1250        if (!test_bit(FLG_L3_INIT, &l2->flag))
1251                skb_queue_tail(&l2->i_queue, skb);
1252        else
1253                dev_kfree_skb(skb);
1254}
1255
1256static void
1257l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
1258{
1259        struct layer2 *l2 = fi->userdata;
1260        struct sk_buff *skb = arg;
1261
1262        skb_queue_tail(&l2->i_queue, skb);
1263        mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1264}
1265
1266static void
1267l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
1268{
1269        struct layer2 *l2 = fi->userdata;
1270        struct sk_buff *skb = arg;
1271
1272        skb_queue_tail(&l2->i_queue, skb);
1273}
1274
1275static void
1276l2_got_iframe(struct FsmInst *fi, int event, void *arg)
1277{
1278        struct layer2   *l2 = fi->userdata;
1279        struct sk_buff  *skb = arg;
1280        int             PollFlag, i;
1281        u_int           ns, nr;
1282
1283        i = l2addrsize(l2);
1284        if (test_bit(FLG_MOD128, &l2->flag)) {
1285                PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
1286                ns = skb->data[i] >> 1;
1287                nr = (skb->data[i + 1] >> 1) & 0x7f;
1288        } else {
1289                PollFlag = (skb->data[i] & 0x10);
1290                ns = (skb->data[i] >> 1) & 0x7;
1291                nr = (skb->data[i] >> 5) & 0x7;
1292        }
1293        if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
1294                dev_kfree_skb(skb);
1295                if (PollFlag)
1296                        enquiry_response(l2);
1297        } else {
1298                if (l2->vr == ns) {
1299                        l2->vr++;
1300                        if (test_bit(FLG_MOD128, &l2->flag))
1301                                l2->vr %= 128;
1302                        else
1303                                l2->vr %= 8;
1304                        test_and_clear_bit(FLG_REJEXC, &l2->flag);
1305                        if (PollFlag)
1306                                enquiry_response(l2);
1307                        else
1308                                test_and_set_bit(FLG_ACK_PEND, &l2->flag);
1309                        skb_pull(skb, l2headersize(l2, 0));
1310                        l2up(l2, DL_DATA_IND, skb);
1311                } else {
1312                        /* n(s)!=v(r) */
1313                        dev_kfree_skb(skb);
1314                        if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
1315                                if (PollFlag)
1316                                        enquiry_response(l2);
1317                        } else {
1318                                enquiry_cr(l2, REJ, RSP, PollFlag);
1319                                test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1320                        }
1321                }
1322        }
1323        if (legalnr(l2, nr)) {
1324                if (!test_bit(FLG_PEER_BUSY, &l2->flag) &&
1325                    (fi->state == ST_L2_7)) {
1326                        if (nr == l2->vs) {
1327                                stop_t200(l2, 13);
1328                                mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1329                                                      EV_L2_T203, NULL, 7);
1330                        } else if (nr != l2->va)
1331                                restart_t200(l2, 14);
1332                }
1333                setva(l2, nr);
1334        } else {
1335                nrerrorrecovery(fi);
1336                return;
1337        }
1338        if (skb_queue_len(&l2->i_queue) && (fi->state == ST_L2_7))
1339                mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1340        if (test_and_clear_bit(FLG_ACK_PEND, &l2->flag))
1341                enquiry_cr(l2, RR, RSP, 0);
1342}
1343
1344static void
1345l2_got_tei(struct FsmInst *fi, int event, void *arg)
1346{
1347        struct layer2   *l2 = fi->userdata;
1348        u_int           info;
1349
1350        l2->tei = (signed char)(long)arg;
1351        set_channel_address(&l2->ch, l2->sapi, l2->tei);
1352        info = DL_INFO_L2_CONNECT;
1353        l2up_create(l2, DL_INFORMATION_IND, sizeof(info), &info);
1354        if (fi->state == ST_L2_3) {
1355                establishlink(fi);
1356                test_and_set_bit(FLG_L3_INIT, &l2->flag);
1357        } else
1358                mISDN_FsmChangeState(fi, ST_L2_4);
1359        if (skb_queue_len(&l2->ui_queue))
1360                tx_ui(l2);
1361}
1362
1363static void
1364l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
1365{
1366        struct layer2 *l2 = fi->userdata;
1367
1368        if (test_bit(FLG_LAPD, &l2->flag) &&
1369            test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1370                mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1371        } else if (l2->rc == l2->N200) {
1372                mISDN_FsmChangeState(fi, ST_L2_4);
1373                test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1374                skb_queue_purge(&l2->i_queue);
1375                l2mgr(l2, MDL_ERROR_IND, (void *) 'G');
1376                if (test_bit(FLG_LAPB, &l2->flag))
1377                        l2down_create(l2, PH_DEACTIVATE_REQ,
1378                                      l2_newid(l2), 0, NULL);
1379                st5_dl_release_l2l3(l2);
1380                if (l2->tm)
1381                        l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1382        } else {
1383                l2->rc++;
1384                mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1385                send_uframe(l2, NULL, (test_bit(FLG_MOD128, &l2->flag) ?
1386                                       SABME : SABM) | 0x10, CMD);
1387        }
1388}
1389
1390static void
1391l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
1392{
1393        struct layer2 *l2 = fi->userdata;
1394
1395        if (test_bit(FLG_LAPD, &l2->flag) &&
1396            test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1397                mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1398        } else if (l2->rc == l2->N200) {
1399                mISDN_FsmChangeState(fi, ST_L2_4);
1400                test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1401                l2mgr(l2, MDL_ERROR_IND, (void *) 'H');
1402                lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1403                if (l2->tm)
1404                        l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1405        } else {
1406                l2->rc++;
1407                mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200,
1408                                  NULL, 9);
1409                send_uframe(l2, NULL, DISC | 0x10, CMD);
1410        }
1411}
1412
1413static void
1414l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
1415{
1416        struct layer2 *l2 = fi->userdata;
1417
1418        if (test_bit(FLG_LAPD, &l2->flag) &&
1419            test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1420                mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1421                return;
1422        }
1423        test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1424        l2->rc = 0;
1425        mISDN_FsmChangeState(fi, ST_L2_8);
1426        transmit_enquiry(l2);
1427        l2->rc++;
1428}
1429
1430static void
1431l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
1432{
1433        struct layer2 *l2 = fi->userdata;
1434
1435        if (test_bit(FLG_LAPD, &l2->flag) &&
1436            test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1437                mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1438                return;
1439        }
1440        test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1441        if (l2->rc == l2->N200) {
1442                l2mgr(l2, MDL_ERROR_IND, (void *) 'I');
1443                establishlink(fi);
1444                test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1445        } else {
1446                transmit_enquiry(l2);
1447                l2->rc++;
1448        }
1449}
1450
1451static void
1452l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
1453{
1454        struct layer2 *l2 = fi->userdata;
1455
1456        if (test_bit(FLG_LAPD, &l2->flag) &&
1457            test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1458                mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 9);
1459                return;
1460        }
1461        mISDN_FsmChangeState(fi, ST_L2_8);
1462        transmit_enquiry(l2);
1463        l2->rc = 0;
1464}
1465
1466static void
1467l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
1468{
1469        struct layer2   *l2 = fi->userdata;
1470        struct sk_buff  *skb, *nskb;
1471        u_char          header[MAX_L2HEADER_LEN];
1472        u_int           i, p1;
1473
1474        if (!cansend(l2))
1475                return;
1476
1477        skb = skb_dequeue(&l2->i_queue);
1478        if (!skb)
1479                return;
1480        i = sethdraddr(l2, header, CMD);
1481        if (test_bit(FLG_MOD128, &l2->flag)) {
1482                header[i++] = l2->vs << 1;
1483                header[i++] = l2->vr << 1;
1484        } else
1485                header[i++] = (l2->vr << 5) | (l2->vs << 1);
1486        nskb = skb_realloc_headroom(skb, i);
1487        if (!nskb) {
1488                printk(KERN_WARNING "%s: no headroom(%d) copy for IFrame\n",
1489                       mISDNDevName4ch(&l2->ch), i);
1490                skb_queue_head(&l2->i_queue, skb);
1491                return;
1492        }
1493        if (test_bit(FLG_MOD128, &l2->flag)) {
1494                p1 = (l2->vs - l2->va) % 128;
1495                l2->vs = (l2->vs + 1) % 128;
1496        } else {
1497                p1 = (l2->vs - l2->va) % 8;
1498                l2->vs = (l2->vs + 1) % 8;
1499        }
1500        p1 = (p1 + l2->sow) % l2->window;
1501        if (l2->windowar[p1]) {
1502                printk(KERN_WARNING "%s: l2 try overwrite ack queue entry %d\n",
1503                       mISDNDevName4ch(&l2->ch), p1);
1504                dev_kfree_skb(l2->windowar[p1]);
1505        }
1506        l2->windowar[p1] = skb;
1507        memcpy(skb_push(nskb, i), header, i);
1508        l2down(l2, PH_DATA_REQ, l2_newid(l2), nskb);
1509        test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1510        if (!test_and_set_bit(FLG_T200_RUN, &l2->flag)) {
1511                mISDN_FsmDelTimer(&l2->t203, 13);
1512                mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 11);
1513        }
1514}
1515
1516static void
1517l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
1518{
1519        struct layer2 *l2 = fi->userdata;
1520        struct sk_buff *skb = arg;
1521        int PollFlag, rsp, rnr = 0;
1522        unsigned int nr;
1523
1524        rsp = *skb->data & 0x2;
1525        if (test_bit(FLG_ORIG, &l2->flag))
1526                rsp = !rsp;
1527
1528        skb_pull(skb, l2addrsize(l2));
1529
1530        if (IsRNR(skb->data, l2)) {
1531                set_peer_busy(l2);
1532                rnr = 1;
1533        } else
1534                clear_peer_busy(l2);
1535
1536        if (test_bit(FLG_MOD128, &l2->flag)) {
1537                PollFlag = (skb->data[1] & 0x1) == 0x1;
1538                nr = skb->data[1] >> 1;
1539        } else {
1540                PollFlag = (skb->data[0] & 0x10);
1541                nr = (skb->data[0] >> 5) & 0x7;
1542        }
1543        dev_kfree_skb(skb);
1544        if (rsp && PollFlag) {
1545                if (legalnr(l2, nr)) {
1546                        if (rnr) {
1547                                restart_t200(l2, 15);
1548                        } else {
1549                                stop_t200(l2, 16);
1550                                mISDN_FsmAddTimer(&l2->t203, l2->T203,
1551                                                  EV_L2_T203, NULL, 5);
1552                                setva(l2, nr);
1553                        }
1554                        invoke_retransmission(l2, nr);
1555                        mISDN_FsmChangeState(fi, ST_L2_7);
1556                        if (skb_queue_len(&l2->i_queue) && cansend(l2))
1557                                mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1558                } else
1559                        nrerrorrecovery(fi);
1560        } else {
1561                if (!rsp && PollFlag)
1562                        enquiry_response(l2);
1563                if (legalnr(l2, nr))
1564                        setva(l2, nr);
1565                else
1566                        nrerrorrecovery(fi);
1567        }
1568}
1569
1570static void
1571l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
1572{
1573        struct layer2 *l2 = fi->userdata;
1574        struct sk_buff *skb = arg;
1575
1576        skb_pull(skb, l2addrsize(l2) + 1);
1577
1578        if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) || /* I or S */
1579            (IsUA(skb->data) && (fi->state == ST_L2_7))) {
1580                l2mgr(l2, MDL_ERROR_IND, (void *) 'K');
1581                establishlink(fi);
1582                test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1583        }
1584        dev_kfree_skb(skb);
1585}
1586
1587static void
1588l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
1589{
1590        struct layer2 *l2 = fi->userdata;
1591
1592        skb_queue_purge(&l2->ui_queue);
1593        l2->tei = GROUP_TEI;
1594        mISDN_FsmChangeState(fi, ST_L2_1);
1595}
1596
1597static void
1598l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
1599{
1600        struct layer2 *l2 = fi->userdata;
1601
1602        skb_queue_purge(&l2->ui_queue);
1603        l2->tei = GROUP_TEI;
1604        l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1605        mISDN_FsmChangeState(fi, ST_L2_1);
1606}
1607
1608static void
1609l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
1610{
1611        struct layer2 *l2 = fi->userdata;
1612
1613        skb_queue_purge(&l2->i_queue);
1614        skb_queue_purge(&l2->ui_queue);
1615        freewin(l2);
1616        l2->tei = GROUP_TEI;
1617        stop_t200(l2, 17);
1618        st5_dl_release_l2l3(l2);
1619        mISDN_FsmChangeState(fi, ST_L2_1);
1620}
1621
1622static void
1623l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
1624{
1625        struct layer2 *l2 = fi->userdata;
1626
1627        skb_queue_purge(&l2->ui_queue);
1628        l2->tei = GROUP_TEI;
1629        stop_t200(l2, 18);
1630        l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1631        mISDN_FsmChangeState(fi, ST_L2_1);
1632}
1633
1634static void
1635l2_tei_remove(struct FsmInst *fi, int event, void *arg)
1636{
1637        struct layer2 *l2 = fi->userdata;
1638
1639        skb_queue_purge(&l2->i_queue);
1640        skb_queue_purge(&l2->ui_queue);
1641        freewin(l2);
1642        l2->tei = GROUP_TEI;
1643        stop_t200(l2, 17);
1644        mISDN_FsmDelTimer(&l2->t203, 19);
1645        l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1646/*      mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
1647 *              MGR_SHORTSTATUS_IND, SSTATUS_L2_RELEASED,
1648 *              0, NULL, 0);
1649 */
1650        mISDN_FsmChangeState(fi, ST_L2_1);
1651}
1652
1653static void
1654l2_st14_persistent_da(struct FsmInst *fi, int event, void *arg)
1655{
1656        struct layer2 *l2 = fi->userdata;
1657        struct sk_buff *skb = arg;
1658
1659        skb_queue_purge(&l2->i_queue);
1660        skb_queue_purge(&l2->ui_queue);
1661        if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
1662                l2up(l2, DL_RELEASE_IND, skb);
1663        else
1664                dev_kfree_skb(skb);
1665}
1666
1667static void
1668l2_st5_persistent_da(struct FsmInst *fi, int event, void *arg)
1669{
1670        struct layer2 *l2 = fi->userdata;
1671        struct sk_buff *skb = arg;
1672
1673        skb_queue_purge(&l2->i_queue);
1674        skb_queue_purge(&l2->ui_queue);
1675        freewin(l2);
1676        stop_t200(l2, 19);
1677        st5_dl_release_l2l3(l2);
1678        mISDN_FsmChangeState(fi, ST_L2_4);
1679        if (l2->tm)
1680                l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1681        dev_kfree_skb(skb);
1682}
1683
1684static void
1685l2_st6_persistent_da(struct FsmInst *fi, int event, void *arg)
1686{
1687        struct layer2 *l2 = fi->userdata;
1688        struct sk_buff *skb = arg;
1689
1690        skb_queue_purge(&l2->ui_queue);
1691        stop_t200(l2, 20);
1692        l2up(l2, DL_RELEASE_CNF, skb);
1693        mISDN_FsmChangeState(fi, ST_L2_4);
1694        if (l2->tm)
1695                l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1696}
1697
1698static void
1699l2_persistent_da(struct FsmInst *fi, int event, void *arg)
1700{
1701        struct layer2 *l2 = fi->userdata;
1702        struct sk_buff *skb = arg;
1703
1704        skb_queue_purge(&l2->i_queue);
1705        skb_queue_purge(&l2->ui_queue);
1706        freewin(l2);
1707        stop_t200(l2, 19);
1708        mISDN_FsmDelTimer(&l2->t203, 19);
1709        l2up(l2, DL_RELEASE_IND, skb);
1710        mISDN_FsmChangeState(fi, ST_L2_4);
1711        if (l2->tm)
1712                l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1713}
1714
1715static void
1716l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
1717{
1718        struct layer2 *l2 = fi->userdata;
1719        struct sk_buff *skb = arg;
1720
1721        if (!test_and_set_bit(FLG_OWN_BUSY, &l2->flag)) {
1722                enquiry_cr(l2, RNR, RSP, 0);
1723                test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1724        }
1725        if (skb)
1726                dev_kfree_skb(skb);
1727}
1728
1729static void
1730l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
1731{
1732        struct layer2 *l2 = fi->userdata;
1733        struct sk_buff *skb = arg;
1734
1735        if (!test_and_clear_bit(FLG_OWN_BUSY, &l2->flag)) {
1736                enquiry_cr(l2, RR, RSP, 0);
1737                test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1738        }
1739        if (skb)
1740                dev_kfree_skb(skb);
1741}
1742
1743static void
1744l2_frame_error(struct FsmInst *fi, int event, void *arg)
1745{
1746        struct layer2 *l2 = fi->userdata;
1747
1748        l2mgr(l2, MDL_ERROR_IND, arg);
1749}
1750
1751static void
1752l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
1753{
1754        struct layer2 *l2 = fi->userdata;
1755
1756        l2mgr(l2, MDL_ERROR_IND, arg);
1757        establishlink(fi);
1758        test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1759}
1760
1761static struct FsmNode L2FnList[] =
1762{
1763        {ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
1764        {ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
1765        {ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
1766        {ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
1767        {ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1768        {ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1769        {ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
1770        {ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
1771        {ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1772        {ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1773        {ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
1774        {ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
1775        {ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
1776        {ST_L2_1, EV_L2_DL_UNITDATA, l2_queue_ui_assign},
1777        {ST_L2_2, EV_L2_DL_UNITDATA, l2_queue_ui},
1778        {ST_L2_3, EV_L2_DL_UNITDATA, l2_queue_ui},
1779        {ST_L2_4, EV_L2_DL_UNITDATA, l2_send_ui},
1780        {ST_L2_5, EV_L2_DL_UNITDATA, l2_send_ui},
1781        {ST_L2_6, EV_L2_DL_UNITDATA, l2_send_ui},
1782        {ST_L2_7, EV_L2_DL_UNITDATA, l2_send_ui},
1783        {ST_L2_8, EV_L2_DL_UNITDATA, l2_send_ui},
1784        {ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
1785        {ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
1786        {ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
1787        {ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
1788        {ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
1789        {ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
1790        {ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
1791        {ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
1792        {ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
1793        {ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
1794        {ST_L2_4, EV_L2_SABME, l2_start_multi},
1795        {ST_L2_5, EV_L2_SABME, l2_send_UA},
1796        {ST_L2_6, EV_L2_SABME, l2_send_DM},
1797        {ST_L2_7, EV_L2_SABME, l2_restart_multi},
1798        {ST_L2_8, EV_L2_SABME, l2_restart_multi},
1799        {ST_L2_4, EV_L2_DISC, l2_send_DM},
1800        {ST_L2_5, EV_L2_DISC, l2_send_DM},
1801        {ST_L2_6, EV_L2_DISC, l2_send_UA},
1802        {ST_L2_7, EV_L2_DISC, l2_stop_multi},
1803        {ST_L2_8, EV_L2_DISC, l2_stop_multi},
1804        {ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
1805        {ST_L2_5, EV_L2_UA, l2_connected},
1806        {ST_L2_6, EV_L2_UA, l2_released},
1807        {ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
1808        {ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
1809        {ST_L2_4, EV_L2_DM, l2_reestablish},
1810        {ST_L2_5, EV_L2_DM, l2_st5_dm_release},
1811        {ST_L2_6, EV_L2_DM, l2_st6_dm_release},
1812        {ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
1813        {ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
1814        {ST_L2_1, EV_L2_UI, l2_got_ui},
1815        {ST_L2_2, EV_L2_UI, l2_got_ui},
1816        {ST_L2_3, EV_L2_UI, l2_got_ui},
1817        {ST_L2_4, EV_L2_UI, l2_got_ui},
1818        {ST_L2_5, EV_L2_UI, l2_got_ui},
1819        {ST_L2_6, EV_L2_UI, l2_got_ui},
1820        {ST_L2_7, EV_L2_UI, l2_got_ui},
1821        {ST_L2_8, EV_L2_UI, l2_got_ui},
1822        {ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
1823        {ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
1824        {ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
1825        {ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
1826        {ST_L2_7, EV_L2_I, l2_got_iframe},
1827        {ST_L2_8, EV_L2_I, l2_got_iframe},
1828        {ST_L2_5, EV_L2_T200, l2_timeout},
1829        {ST_L2_6, EV_L2_T200, l2_timeout},
1830        {ST_L2_7, EV_L2_T200, l2_timeout},
1831        {ST_L2_8, EV_L2_T200, l2_timeout},
1832        {ST_L2_7, EV_L2_T203, l2_timeout},
1833        {ST_L2_5, EV_L2_T200I, l2_st5_tout_200},
1834        {ST_L2_6, EV_L2_T200I, l2_st6_tout_200},
1835        {ST_L2_7, EV_L2_T200I, l2_st7_tout_200},
1836        {ST_L2_8, EV_L2_T200I, l2_st8_tout_200},
1837        {ST_L2_7, EV_L2_T203I, l2_st7_tout_203},
1838        {ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
1839        {ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1840        {ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1841        {ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1842        {ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1843        {ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
1844        {ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
1845        {ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
1846        {ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1847        {ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1848        {ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistent_da},
1849        {ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
1850        {ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
1851        {ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistent_da},
1852        {ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistent_da},
1853        {ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistent_da},
1854        {ST_L2_7, EV_L1_DEACTIVATE, l2_persistent_da},
1855        {ST_L2_8, EV_L1_DEACTIVATE, l2_persistent_da},
1856};
1857
1858static int
1859ph_data_indication(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb)
1860{
1861        u_char  *datap = skb->data;
1862        int     ret = -EINVAL;
1863        int     psapi, ptei;
1864        u_int   l;
1865        int     c = 0;
1866
1867        l = l2addrsize(l2);
1868        if (skb->len <= l) {
1869                mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *) 'N');
1870                return ret;
1871        }
1872        if (test_bit(FLG_LAPD, &l2->flag)) { /* Maybe not needed */
1873                psapi = *datap++;
1874                ptei = *datap++;
1875                if ((psapi & 1) || !(ptei & 1)) {
1876                        printk(KERN_WARNING
1877                               "%s l2 D-channel frame wrong EA0/EA1\n",
1878                               mISDNDevName4ch(&l2->ch));
1879                        return ret;
1880                }
1881                psapi >>= 2;
1882                ptei >>= 1;
1883                if (psapi != l2->sapi) {
1884                        /* not our business */
1885                        if (*debug & DEBUG_L2)
1886                                printk(KERN_DEBUG "%s: sapi %d/%d mismatch\n",
1887                                       mISDNDevName4ch(&l2->ch), psapi,
1888                                       l2->sapi);
1889                        dev_kfree_skb(skb);
1890                        return 0;
1891                }
1892                if ((ptei != l2->tei) && (ptei != GROUP_TEI)) {
1893                        /* not our business */
1894                        if (*debug & DEBUG_L2)
1895                                printk(KERN_DEBUG "%s: tei %d/%d mismatch\n",
1896                                       mISDNDevName4ch(&l2->ch), ptei, l2->tei);
1897                        dev_kfree_skb(skb);
1898                        return 0;
1899                }
1900        } else
1901                datap += l;
1902        if (!(*datap & 1)) {    /* I-Frame */
1903                c = iframe_error(l2, skb);
1904                if (!c)
1905                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_I, skb);
1906        } else if (IsSFrame(datap, l2)) {       /* S-Frame */
1907                c = super_error(l2, skb);
1908                if (!c)
1909                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SUPER, skb);
1910        } else if (IsUI(datap)) {
1911                c = UI_error(l2, skb);
1912                if (!c)
1913                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UI, skb);
1914        } else if (IsSABME(datap, l2)) {
1915                c = unnum_error(l2, skb, CMD);
1916                if (!c)
1917                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SABME, skb);
1918        } else if (IsUA(datap)) {
1919                c = unnum_error(l2, skb, RSP);
1920                if (!c)
1921                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UA, skb);
1922        } else if (IsDISC(datap)) {
1923                c = unnum_error(l2, skb, CMD);
1924                if (!c)
1925                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DISC, skb);
1926        } else if (IsDM(datap)) {
1927                c = unnum_error(l2, skb, RSP);
1928                if (!c)
1929                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DM, skb);
1930        } else if (IsFRMR(datap)) {
1931                c = FRMR_error(l2, skb);
1932                if (!c)
1933                        ret = mISDN_FsmEvent(&l2->l2m, EV_L2_FRMR, skb);
1934        } else
1935                c = 'L';
1936        if (c) {
1937                printk(KERN_WARNING "%s:l2 D-channel frame error %c\n",
1938                       mISDNDevName4ch(&l2->ch), c);
1939                mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
1940        }
1941        return ret;
1942}
1943
1944static int
1945l2_send(struct mISDNchannel *ch, struct sk_buff *skb)
1946{
1947        struct layer2           *l2 = container_of(ch, struct layer2, ch);
1948        struct mISDNhead        *hh =  mISDN_HEAD_P(skb);
1949        int                     ret = -EINVAL;
1950
1951        if (*debug & DEBUG_L2_RECV)
1952                printk(KERN_DEBUG "%s: %s prim(%x) id(%x) sapi(%d) tei(%d)\n",
1953                       __func__, mISDNDevName4ch(&l2->ch), hh->prim, hh->id,
1954                       l2->sapi, l2->tei);
1955        if (hh->prim == DL_INTERN_MSG) {
1956                struct mISDNhead *chh = hh + 1; /* saved copy */
1957
1958                *hh = *chh;
1959                if (*debug & DEBUG_L2_RECV)
1960                        printk(KERN_DEBUG "%s: prim(%x) id(%x) internal msg\n",
1961                                mISDNDevName4ch(&l2->ch), hh->prim, hh->id);
1962        }
1963        switch (hh->prim) {
1964        case PH_DATA_IND:
1965                ret = ph_data_indication(l2, hh, skb);
1966                break;
1967        case PH_DATA_CNF:
1968                ret = ph_data_confirm(l2, hh, skb);
1969                break;
1970        case PH_ACTIVATE_IND:
1971                test_and_set_bit(FLG_L1_ACTIV, &l2->flag);
1972                l2up_create(l2, MPH_ACTIVATE_IND, 0, NULL);
1973                if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
1974                        ret = mISDN_FsmEvent(&l2->l2m,
1975                                             EV_L2_DL_ESTABLISH_REQ, skb);
1976                break;
1977        case PH_DEACTIVATE_IND:
1978                test_and_clear_bit(FLG_L1_ACTIV, &l2->flag);
1979                l2up_create(l2, MPH_DEACTIVATE_IND, 0, NULL);
1980                ret = mISDN_FsmEvent(&l2->l2m, EV_L1_DEACTIVATE, skb);
1981                break;
1982        case MPH_INFORMATION_IND:
1983                if (!l2->up)
1984                        break;
1985                ret = l2->up->send(l2->up, skb);
1986                break;
1987        case DL_DATA_REQ:
1988                ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_DATA, skb);
1989                break;
1990        case DL_UNITDATA_REQ:
1991                ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_UNITDATA, skb);
1992                break;
1993        case DL_ESTABLISH_REQ:
1994                if (test_bit(FLG_LAPB, &l2->flag))
1995                        test_and_set_bit(FLG_ORIG, &l2->flag);
1996                if (test_bit(FLG_L1_ACTIV, &l2->flag)) {
1997                        if (test_bit(FLG_LAPD, &l2->flag) ||
1998                            test_bit(FLG_ORIG, &l2->flag))
1999                                ret = mISDN_FsmEvent(&l2->l2m,
2000                                                     EV_L2_DL_ESTABLISH_REQ, skb);
2001                } else {
2002                        if (test_bit(FLG_LAPD, &l2->flag) ||
2003                            test_bit(FLG_ORIG, &l2->flag)) {
2004                                test_and_set_bit(FLG_ESTAB_PEND,
2005                                                 &l2->flag);
2006                        }
2007                        ret = l2down(l2, PH_ACTIVATE_REQ, l2_newid(l2),
2008                                     skb);
2009                }
2010                break;
2011        case DL_RELEASE_REQ:
2012                if (test_bit(FLG_LAPB, &l2->flag))
2013                        l2down_create(l2, PH_DEACTIVATE_REQ,
2014                                      l2_newid(l2), 0, NULL);
2015                ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_RELEASE_REQ,
2016                                     skb);
2017                break;
2018        case DL_TIMER200_IND:
2019                mISDN_FsmEvent(&l2->l2m, EV_L2_T200I, NULL);
2020                break;
2021        case DL_TIMER203_IND:
2022                mISDN_FsmEvent(&l2->l2m, EV_L2_T203I, NULL);
2023                break;
2024        default:
2025                if (*debug & DEBUG_L2)
2026                        l2m_debug(&l2->l2m, "l2 unknown pr %04x",
2027                                  hh->prim);
2028        }
2029        if (ret) {
2030                dev_kfree_skb(skb);
2031                ret = 0;
2032        }
2033        return ret;
2034}
2035
2036int
2037tei_l2(struct layer2 *l2, u_int cmd, u_long arg)
2038{
2039        int             ret = -EINVAL;
2040
2041        if (*debug & DEBUG_L2_TEI)
2042                printk(KERN_DEBUG "%s: cmd(%x) in %s\n",
2043                       mISDNDevName4ch(&l2->ch), cmd, __func__);
2044        switch (cmd) {
2045        case (MDL_ASSIGN_REQ):
2046                ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ASSIGN, (void *)arg);
2047                break;
2048        case (MDL_REMOVE_REQ):
2049                ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_REMOVE, NULL);
2050                break;
2051        case (MDL_ERROR_IND):
2052                ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
2053                break;
2054        case (MDL_ERROR_RSP):
2055                /* ETS 300-125 5.3.2.1 Test: TC13010 */
2056                printk(KERN_NOTICE "%s: MDL_ERROR|REQ (tei_l2)\n",
2057                       mISDNDevName4ch(&l2->ch));
2058                ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
2059                break;
2060        }
2061        return ret;
2062}
2063
2064static void
2065release_l2(struct layer2 *l2)
2066{
2067        mISDN_FsmDelTimer(&l2->t200, 21);
2068        mISDN_FsmDelTimer(&l2->t203, 16);
2069        skb_queue_purge(&l2->i_queue);
2070        skb_queue_purge(&l2->ui_queue);
2071        skb_queue_purge(&l2->down_queue);
2072        ReleaseWin(l2);
2073        if (test_bit(FLG_LAPD, &l2->flag)) {
2074                TEIrelease(l2);
2075                if (l2->ch.st)
2076                        l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D,
2077                                               CLOSE_CHANNEL, NULL);
2078        }
2079        kfree(l2);
2080}
2081
2082static int
2083l2_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
2084{
2085        struct layer2           *l2 = container_of(ch, struct layer2, ch);
2086        u_int                   info;
2087
2088        if (*debug & DEBUG_L2_CTRL)
2089                printk(KERN_DEBUG "%s: %s cmd(%x)\n",
2090                       mISDNDevName4ch(ch), __func__, cmd);
2091
2092        switch (cmd) {
2093        case OPEN_CHANNEL:
2094                if (test_bit(FLG_LAPD, &l2->flag)) {
2095                        set_channel_address(&l2->ch, l2->sapi, l2->tei);
2096                        info = DL_INFO_L2_CONNECT;
2097                        l2up_create(l2, DL_INFORMATION_IND,
2098                                    sizeof(info), &info);
2099                }
2100                break;
2101        case CLOSE_CHANNEL:
2102                if (l2->ch.peer)
2103                        l2->ch.peer->ctrl(l2->ch.peer, CLOSE_CHANNEL, NULL);
2104                release_l2(l2);
2105                break;
2106        }
2107        return 0;
2108}
2109
2110struct layer2 *
2111create_l2(struct mISDNchannel *ch, u_int protocol, u_long options, int tei,
2112          int sapi)
2113{
2114        struct layer2           *l2;
2115        struct channel_req      rq;
2116
2117        l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
2118        if (!l2) {
2119                printk(KERN_ERR "kzalloc layer2 failed\n");
2120                return NULL;
2121        }
2122        l2->next_id = 1;
2123        l2->down_id = MISDN_ID_NONE;
2124        l2->up = ch;
2125        l2->ch.st = ch->st;
2126        l2->ch.send = l2_send;
2127        l2->ch.ctrl = l2_ctrl;
2128        switch (protocol) {
2129        case ISDN_P_LAPD_NT:
2130                test_and_set_bit(FLG_LAPD, &l2->flag);
2131                test_and_set_bit(FLG_LAPD_NET, &l2->flag);
2132                test_and_set_bit(FLG_MOD128, &l2->flag);
2133                l2->sapi = sapi;
2134                l2->maxlen = MAX_DFRAME_LEN;
2135                if (test_bit(OPTION_L2_PMX, &options))
2136                        l2->window = 7;
2137                else
2138                        l2->window = 1;
2139                if (test_bit(OPTION_L2_PTP, &options))
2140                        test_and_set_bit(FLG_PTP, &l2->flag);
2141                if (test_bit(OPTION_L2_FIXEDTEI, &options))
2142                        test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2143                l2->tei = tei;
2144                l2->T200 = 1000;
2145                l2->N200 = 3;
2146                l2->T203 = 10000;
2147                if (test_bit(OPTION_L2_PMX, &options))
2148                        rq.protocol = ISDN_P_NT_E1;
2149                else
2150                        rq.protocol = ISDN_P_NT_S0;
2151                rq.adr.channel = 0;
2152                l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
2153                break;
2154        case ISDN_P_LAPD_TE:
2155                test_and_set_bit(FLG_LAPD, &l2->flag);
2156                test_and_set_bit(FLG_MOD128, &l2->flag);
2157                test_and_set_bit(FLG_ORIG, &l2->flag);
2158                l2->sapi = sapi;
2159                l2->maxlen = MAX_DFRAME_LEN;
2160                if (test_bit(OPTION_L2_PMX, &options))
2161                        l2->window = 7;
2162                else
2163                        l2->window = 1;
2164                if (test_bit(OPTION_L2_PTP, &options))
2165                        test_and_set_bit(FLG_PTP, &l2->flag);
2166                if (test_bit(OPTION_L2_FIXEDTEI, &options))
2167                        test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2168                l2->tei = tei;
2169                l2->T200 = 1000;
2170                l2->N200 = 3;
2171                l2->T203 = 10000;
2172                if (test_bit(OPTION_L2_PMX, &options))
2173                        rq.protocol = ISDN_P_TE_E1;
2174                else
2175                        rq.protocol = ISDN_P_TE_S0;
2176                rq.adr.channel = 0;
2177                l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
2178                break;
2179        case ISDN_P_B_X75SLP:
2180                test_and_set_bit(FLG_LAPB, &l2->flag);
2181                l2->window = 7;
2182                l2->maxlen = MAX_DATA_SIZE;
2183                l2->T200 = 1000;
2184                l2->N200 = 4;
2185                l2->T203 = 5000;
2186                l2->addr.A = 3;
2187                l2->addr.B = 1;
2188                break;
2189        default:
2190                printk(KERN_ERR "layer2 create failed prt %x\n",
2191                       protocol);
2192                kfree(l2);
2193                return NULL;
2194        }
2195        skb_queue_head_init(&l2->i_queue);
2196        skb_queue_head_init(&l2->ui_queue);
2197        skb_queue_head_init(&l2->down_queue);
2198        skb_queue_head_init(&l2->tmp_queue);
2199        InitWin(l2);
2200        l2->l2m.fsm = &l2fsm;
2201        if (test_bit(FLG_LAPB, &l2->flag) ||
2202            test_bit(FLG_FIXED_TEI, &l2->flag) ||
2203            test_bit(FLG_LAPD_NET, &l2->flag))
2204                l2->l2m.state = ST_L2_4;
2205        else
2206                l2->l2m.state = ST_L2_1;
2207        l2->l2m.debug = *debug;
2208        l2->l2m.userdata = l2;
2209        l2->l2m.userint = 0;
2210        l2->l2m.printdebug = l2m_debug;
2211
2212        mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
2213        mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
2214        return l2;
2215}
2216
2217static int
2218x75create(struct channel_req *crq)
2219{
2220        struct layer2   *l2;
2221
2222        if (crq->protocol != ISDN_P_B_X75SLP)
2223                return -EPROTONOSUPPORT;
2224        l2 = create_l2(crq->ch, crq->protocol, 0, 0, 0);
2225        if (!l2)
2226                return -ENOMEM;
2227        crq->ch = &l2->ch;
2228        crq->protocol = ISDN_P_B_HDLC;
2229        return 0;
2230}
2231
2232static struct Bprotocol X75SLP = {
2233        .Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
2234        .name = "X75SLP",
2235        .create = x75create
2236};
2237
2238int
2239Isdnl2_Init(u_int *deb)
2240{
2241        int res;
2242        debug = deb;
2243        mISDN_register_Bprotocol(&X75SLP);
2244        l2fsm.state_count = L2_STATE_COUNT;
2245        l2fsm.event_count = L2_EVENT_COUNT;
2246        l2fsm.strEvent = strL2Event;
2247        l2fsm.strState = strL2State;
2248        res = mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
2249        if (res)
2250                goto error;
2251        res = TEIInit(deb);
2252        if (res)
2253                goto error_fsm;
2254        return 0;
2255
2256error_fsm:
2257        mISDN_FsmFree(&l2fsm);
2258error:
2259        mISDN_unregister_Bprotocol(&X75SLP);
2260        return res;
2261}
2262
2263void
2264Isdnl2_cleanup(void)
2265{
2266        mISDN_unregister_Bprotocol(&X75SLP);
2267        TEIFree();
2268        mISDN_FsmFree(&l2fsm);
2269}
2270