Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Author   Karsten Keil <kkeil@novell.com>
0005  *
0006  * Copyright 2008  by Karsten Keil <kkeil@novell.com>
0007  */
0008 
0009 #include <linux/mISDNif.h>
0010 #include <linux/slab.h>
0011 #include "core.h"
0012 #include "fsm.h"
0013 #include "layer2.h"
0014 
0015 static u_int *debug;
0016 
0017 static
0018 struct Fsm l2fsm = {NULL, 0, 0, NULL, NULL};
0019 
0020 static char *strL2State[] =
0021 {
0022     "ST_L2_1",
0023     "ST_L2_2",
0024     "ST_L2_3",
0025     "ST_L2_4",
0026     "ST_L2_5",
0027     "ST_L2_6",
0028     "ST_L2_7",
0029     "ST_L2_8",
0030 };
0031 
0032 enum {
0033     EV_L2_UI,
0034     EV_L2_SABME,
0035     EV_L2_DISC,
0036     EV_L2_DM,
0037     EV_L2_UA,
0038     EV_L2_FRMR,
0039     EV_L2_SUPER,
0040     EV_L2_I,
0041     EV_L2_DL_DATA,
0042     EV_L2_ACK_PULL,
0043     EV_L2_DL_UNITDATA,
0044     EV_L2_DL_ESTABLISH_REQ,
0045     EV_L2_DL_RELEASE_REQ,
0046     EV_L2_MDL_ASSIGN,
0047     EV_L2_MDL_REMOVE,
0048     EV_L2_MDL_ERROR,
0049     EV_L1_DEACTIVATE,
0050     EV_L2_T200,
0051     EV_L2_T203,
0052     EV_L2_T200I,
0053     EV_L2_T203I,
0054     EV_L2_SET_OWN_BUSY,
0055     EV_L2_CLEAR_OWN_BUSY,
0056     EV_L2_FRAME_ERROR,
0057 };
0058 
0059 #define L2_EVENT_COUNT (EV_L2_FRAME_ERROR + 1)
0060 
0061 static char *strL2Event[] =
0062 {
0063     "EV_L2_UI",
0064     "EV_L2_SABME",
0065     "EV_L2_DISC",
0066     "EV_L2_DM",
0067     "EV_L2_UA",
0068     "EV_L2_FRMR",
0069     "EV_L2_SUPER",
0070     "EV_L2_I",
0071     "EV_L2_DL_DATA",
0072     "EV_L2_ACK_PULL",
0073     "EV_L2_DL_UNITDATA",
0074     "EV_L2_DL_ESTABLISH_REQ",
0075     "EV_L2_DL_RELEASE_REQ",
0076     "EV_L2_MDL_ASSIGN",
0077     "EV_L2_MDL_REMOVE",
0078     "EV_L2_MDL_ERROR",
0079     "EV_L1_DEACTIVATE",
0080     "EV_L2_T200",
0081     "EV_L2_T203",
0082     "EV_L2_T200I",
0083     "EV_L2_T203I",
0084     "EV_L2_SET_OWN_BUSY",
0085     "EV_L2_CLEAR_OWN_BUSY",
0086     "EV_L2_FRAME_ERROR",
0087 };
0088 
0089 static void
0090 l2m_debug(struct FsmInst *fi, char *fmt, ...)
0091 {
0092     struct layer2 *l2 = fi->userdata;
0093     struct va_format vaf;
0094     va_list va;
0095 
0096     if (!(*debug & DEBUG_L2_FSM))
0097         return;
0098 
0099     va_start(va, fmt);
0100 
0101     vaf.fmt = fmt;
0102     vaf.va = &va;
0103 
0104     printk(KERN_DEBUG "%s l2 (sapi %d tei %d): %pV\n",
0105            mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei, &vaf);
0106 
0107     va_end(va);
0108 }
0109 
0110 inline u_int
0111 l2headersize(struct layer2 *l2, int ui)
0112 {
0113     return ((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
0114         (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
0115 }
0116 
0117 inline u_int
0118 l2addrsize(struct layer2 *l2)
0119 {
0120     return test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
0121 }
0122 
0123 static u_int
0124 l2_newid(struct layer2 *l2)
0125 {
0126     u_int   id;
0127 
0128     id = l2->next_id++;
0129     if (id == 0x7fff)
0130         l2->next_id = 1;
0131     id <<= 16;
0132     id |= l2->tei << 8;
0133     id |= l2->sapi;
0134     return id;
0135 }
0136 
0137 static void
0138 l2up(struct layer2 *l2, u_int prim, struct sk_buff *skb)
0139 {
0140     int err;
0141 
0142     if (!l2->up)
0143         return;
0144     mISDN_HEAD_PRIM(skb) = prim;
0145     mISDN_HEAD_ID(skb) = (l2->ch.nr << 16) | l2->ch.addr;
0146     err = l2->up->send(l2->up, skb);
0147     if (err) {
0148         printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
0149                mISDNDevName4ch(&l2->ch), err);
0150         dev_kfree_skb(skb);
0151     }
0152 }
0153 
0154 static void
0155 l2up_create(struct layer2 *l2, u_int prim, int len, void *arg)
0156 {
0157     struct sk_buff  *skb;
0158     struct mISDNhead *hh;
0159     int     err;
0160 
0161     if (!l2->up)
0162         return;
0163     skb = mI_alloc_skb(len, GFP_ATOMIC);
0164     if (!skb)
0165         return;
0166     hh = mISDN_HEAD_P(skb);
0167     hh->prim = prim;
0168     hh->id = (l2->ch.nr << 16) | l2->ch.addr;
0169     if (len)
0170         skb_put_data(skb, arg, len);
0171     err = l2->up->send(l2->up, skb);
0172     if (err) {
0173         printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
0174                mISDNDevName4ch(&l2->ch), err);
0175         dev_kfree_skb(skb);
0176     }
0177 }
0178 
0179 static int
0180 l2down_skb(struct layer2 *l2, struct sk_buff *skb) {
0181     int ret;
0182 
0183     ret = l2->ch.recv(l2->ch.peer, skb);
0184     if (ret && (*debug & DEBUG_L2_RECV))
0185         printk(KERN_DEBUG "l2down_skb: dev %s ret(%d)\n",
0186                mISDNDevName4ch(&l2->ch), ret);
0187     return ret;
0188 }
0189 
0190 static int
0191 l2down_raw(struct layer2 *l2, struct sk_buff *skb)
0192 {
0193     struct mISDNhead *hh = mISDN_HEAD_P(skb);
0194 
0195     if (hh->prim == PH_DATA_REQ) {
0196         if (test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
0197             skb_queue_tail(&l2->down_queue, skb);
0198             return 0;
0199         }
0200         l2->down_id = mISDN_HEAD_ID(skb);
0201     }
0202     return l2down_skb(l2, skb);
0203 }
0204 
0205 static int
0206 l2down(struct layer2 *l2, u_int prim, u_int id, struct sk_buff *skb)
0207 {
0208     struct mISDNhead *hh = mISDN_HEAD_P(skb);
0209 
0210     hh->prim = prim;
0211     hh->id = id;
0212     return l2down_raw(l2, skb);
0213 }
0214 
0215 static int
0216 l2down_create(struct layer2 *l2, u_int prim, u_int id, int len, void *arg)
0217 {
0218     struct sk_buff  *skb;
0219     int     err;
0220     struct mISDNhead *hh;
0221 
0222     skb = mI_alloc_skb(len, GFP_ATOMIC);
0223     if (!skb)
0224         return -ENOMEM;
0225     hh = mISDN_HEAD_P(skb);
0226     hh->prim = prim;
0227     hh->id = id;
0228     if (len)
0229         skb_put_data(skb, arg, len);
0230     err = l2down_raw(l2, skb);
0231     if (err)
0232         dev_kfree_skb(skb);
0233     return err;
0234 }
0235 
0236 static int
0237 ph_data_confirm(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb) {
0238     struct sk_buff *nskb = skb;
0239     int ret = -EAGAIN;
0240 
0241     if (test_bit(FLG_L1_NOTREADY, &l2->flag)) {
0242         if (hh->id == l2->down_id) {
0243             nskb = skb_dequeue(&l2->down_queue);
0244             if (nskb) {
0245                 l2->down_id = mISDN_HEAD_ID(nskb);
0246                 if (l2down_skb(l2, nskb)) {
0247                     dev_kfree_skb(nskb);
0248                     l2->down_id = MISDN_ID_NONE;
0249                 }
0250             } else
0251                 l2->down_id = MISDN_ID_NONE;
0252             if (ret) {
0253                 dev_kfree_skb(skb);
0254                 ret = 0;
0255             }
0256             if (l2->down_id == MISDN_ID_NONE) {
0257                 test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
0258                 mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
0259             }
0260         }
0261     }
0262     if (!test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
0263         nskb = skb_dequeue(&l2->down_queue);
0264         if (nskb) {
0265             l2->down_id = mISDN_HEAD_ID(nskb);
0266             if (l2down_skb(l2, nskb)) {
0267                 dev_kfree_skb(nskb);
0268                 l2->down_id = MISDN_ID_NONE;
0269                 test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
0270             }
0271         } else
0272             test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
0273     }
0274     return ret;
0275 }
0276 
0277 static void
0278 l2_timeout(struct FsmInst *fi, int event, void *arg)
0279 {
0280     struct layer2 *l2 = fi->userdata;
0281     struct sk_buff *skb;
0282     struct mISDNhead *hh;
0283 
0284     skb = mI_alloc_skb(0, GFP_ATOMIC);
0285     if (!skb) {
0286         printk(KERN_WARNING "%s: L2(%d,%d) nr:%x timer %s no skb\n",
0287                mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
0288                l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
0289         return;
0290     }
0291     hh = mISDN_HEAD_P(skb);
0292     hh->prim = event == EV_L2_T200 ? DL_TIMER200_IND : DL_TIMER203_IND;
0293     hh->id = l2->ch.nr;
0294     if (*debug & DEBUG_TIMER)
0295         printk(KERN_DEBUG "%s: L2(%d,%d) nr:%x timer %s expired\n",
0296                mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
0297                l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
0298     if (l2->ch.st)
0299         l2->ch.st->own.recv(&l2->ch.st->own, skb);
0300 }
0301 
0302 static int
0303 l2mgr(struct layer2 *l2, u_int prim, void *arg) {
0304     long c = (long)arg;
0305 
0306     printk(KERN_WARNING "l2mgr: dev %s addr:%x prim %x %c\n",
0307            mISDNDevName4ch(&l2->ch), l2->id, prim, (char)c);
0308     if (test_bit(FLG_LAPD, &l2->flag) &&
0309         !test_bit(FLG_FIXED_TEI, &l2->flag)) {
0310         switch (c) {
0311         case 'C':
0312         case 'D':
0313         case 'G':
0314         case 'H':
0315             l2_tei(l2, prim, (u_long)arg);
0316             break;
0317         }
0318     }
0319     return 0;
0320 }
0321 
0322 static void
0323 set_peer_busy(struct layer2 *l2) {
0324     test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
0325     if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
0326         test_and_set_bit(FLG_L2BLOCK, &l2->flag);
0327 }
0328 
0329 static void
0330 clear_peer_busy(struct layer2 *l2) {
0331     if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
0332         test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
0333 }
0334 
0335 static void
0336 InitWin(struct layer2 *l2)
0337 {
0338     int i;
0339 
0340     for (i = 0; i < MAX_WINDOW; i++)
0341         l2->windowar[i] = NULL;
0342 }
0343 
0344 static int
0345 freewin(struct layer2 *l2)
0346 {
0347     int i, cnt = 0;
0348 
0349     for (i = 0; i < MAX_WINDOW; i++) {
0350         if (l2->windowar[i]) {
0351             cnt++;
0352             dev_kfree_skb(l2->windowar[i]);
0353             l2->windowar[i] = NULL;
0354         }
0355     }
0356     return cnt;
0357 }
0358 
0359 static void
0360 ReleaseWin(struct layer2 *l2)
0361 {
0362     int cnt = freewin(l2);
0363 
0364     if (cnt)
0365         printk(KERN_WARNING
0366                "isdnl2 freed %d skbuffs in release\n", cnt);
0367 }
0368 
0369 inline unsigned int
0370 cansend(struct layer2 *l2)
0371 {
0372     unsigned int p1;
0373 
0374     if (test_bit(FLG_MOD128, &l2->flag))
0375         p1 = (l2->vs - l2->va) % 128;
0376     else
0377         p1 = (l2->vs - l2->va) % 8;
0378     return (p1 < l2->window) && !test_bit(FLG_PEER_BUSY, &l2->flag);
0379 }
0380 
0381 inline void
0382 clear_exception(struct layer2 *l2)
0383 {
0384     test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
0385     test_and_clear_bit(FLG_REJEXC, &l2->flag);
0386     test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
0387     clear_peer_busy(l2);
0388 }
0389 
0390 static int
0391 sethdraddr(struct layer2 *l2, u_char *header, int rsp)
0392 {
0393     u_char *ptr = header;
0394     int crbit = rsp;
0395 
0396     if (test_bit(FLG_LAPD, &l2->flag)) {
0397         if (test_bit(FLG_LAPD_NET, &l2->flag))
0398             crbit = !crbit;
0399         *ptr++ = (l2->sapi << 2) | (crbit ? 2 : 0);
0400         *ptr++ = (l2->tei << 1) | 1;
0401         return 2;
0402     } else {
0403         if (test_bit(FLG_ORIG, &l2->flag))
0404             crbit = !crbit;
0405         if (crbit)
0406             *ptr++ = l2->addr.B;
0407         else
0408             *ptr++ = l2->addr.A;
0409         return 1;
0410     }
0411 }
0412 
0413 static inline void
0414 enqueue_super(struct layer2 *l2, struct sk_buff *skb)
0415 {
0416     if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
0417         dev_kfree_skb(skb);
0418 }
0419 
0420 static inline void
0421 enqueue_ui(struct layer2 *l2, struct sk_buff *skb)
0422 {
0423     if (l2->tm)
0424         l2_tei(l2, MDL_STATUS_UI_IND, 0);
0425     if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
0426         dev_kfree_skb(skb);
0427 }
0428 
0429 inline int
0430 IsUI(u_char *data)
0431 {
0432     return (data[0] & 0xef) == UI;
0433 }
0434 
0435 inline int
0436 IsUA(u_char *data)
0437 {
0438     return (data[0] & 0xef) == UA;
0439 }
0440 
0441 inline int
0442 IsDM(u_char *data)
0443 {
0444     return (data[0] & 0xef) == DM;
0445 }
0446 
0447 inline int
0448 IsDISC(u_char *data)
0449 {
0450     return (data[0] & 0xef) == DISC;
0451 }
0452 
0453 inline int
0454 IsRR(u_char *data, struct layer2 *l2)
0455 {
0456     if (test_bit(FLG_MOD128, &l2->flag))
0457         return data[0] == RR;
0458     else
0459         return (data[0] & 0xf) == 1;
0460 }
0461 
0462 inline int
0463 IsSFrame(u_char *data, struct layer2 *l2)
0464 {
0465     register u_char d = *data;
0466 
0467     if (!test_bit(FLG_MOD128, &l2->flag))
0468         d &= 0xf;
0469     return ((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c);
0470 }
0471 
0472 inline int
0473 IsSABME(u_char *data, struct layer2 *l2)
0474 {
0475     u_char d = data[0] & ~0x10;
0476 
0477     return test_bit(FLG_MOD128, &l2->flag) ? d == SABME : d == SABM;
0478 }
0479 
0480 inline int
0481 IsREJ(u_char *data, struct layer2 *l2)
0482 {
0483     return test_bit(FLG_MOD128, &l2->flag) ?
0484         data[0] == REJ : (data[0] & 0xf) == REJ;
0485 }
0486 
0487 inline int
0488 IsFRMR(u_char *data)
0489 {
0490     return (data[0] & 0xef) == FRMR;
0491 }
0492 
0493 inline int
0494 IsRNR(u_char *data, struct layer2 *l2)
0495 {
0496     return test_bit(FLG_MOD128, &l2->flag) ?
0497         data[0] == RNR : (data[0] & 0xf) == RNR;
0498 }
0499 
0500 static int
0501 iframe_error(struct layer2 *l2, struct sk_buff *skb)
0502 {
0503     u_int   i;
0504     int rsp = *skb->data & 0x2;
0505 
0506     i = l2addrsize(l2) + (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1);
0507     if (test_bit(FLG_ORIG, &l2->flag))
0508         rsp = !rsp;
0509     if (rsp)
0510         return 'L';
0511     if (skb->len < i)
0512         return 'N';
0513     if ((skb->len - i) > l2->maxlen)
0514         return 'O';
0515     return 0;
0516 }
0517 
0518 static int
0519 super_error(struct layer2 *l2, struct sk_buff *skb)
0520 {
0521     if (skb->len != l2addrsize(l2) +
0522         (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1))
0523         return 'N';
0524     return 0;
0525 }
0526 
0527 static int
0528 unnum_error(struct layer2 *l2, struct sk_buff *skb, int wantrsp)
0529 {
0530     int rsp = (*skb->data & 0x2) >> 1;
0531     if (test_bit(FLG_ORIG, &l2->flag))
0532         rsp = !rsp;
0533     if (rsp != wantrsp)
0534         return 'L';
0535     if (skb->len != l2addrsize(l2) + 1)
0536         return 'N';
0537     return 0;
0538 }
0539 
0540 static int
0541 UI_error(struct layer2 *l2, struct sk_buff *skb)
0542 {
0543     int rsp = *skb->data & 0x2;
0544     if (test_bit(FLG_ORIG, &l2->flag))
0545         rsp = !rsp;
0546     if (rsp)
0547         return 'L';
0548     if (skb->len > l2->maxlen + l2addrsize(l2) + 1)
0549         return 'O';
0550     return 0;
0551 }
0552 
0553 static int
0554 FRMR_error(struct layer2 *l2, struct sk_buff *skb)
0555 {
0556     u_int   headers = l2addrsize(l2) + 1;
0557     u_char  *datap = skb->data + headers;
0558     int rsp = *skb->data & 0x2;
0559 
0560     if (test_bit(FLG_ORIG, &l2->flag))
0561         rsp = !rsp;
0562     if (!rsp)
0563         return 'L';
0564     if (test_bit(FLG_MOD128, &l2->flag)) {
0565         if (skb->len < headers + 5)
0566             return 'N';
0567         else if (*debug & DEBUG_L2)
0568             l2m_debug(&l2->l2m,
0569                   "FRMR information %2x %2x %2x %2x %2x",
0570                   datap[0], datap[1], datap[2], datap[3], datap[4]);
0571     } else {
0572         if (skb->len < headers + 3)
0573             return 'N';
0574         else if (*debug & DEBUG_L2)
0575             l2m_debug(&l2->l2m,
0576                   "FRMR information %2x %2x %2x",
0577                   datap[0], datap[1], datap[2]);
0578     }
0579     return 0;
0580 }
0581 
0582 static unsigned int
0583 legalnr(struct layer2 *l2, unsigned int nr)
0584 {
0585     if (test_bit(FLG_MOD128, &l2->flag))
0586         return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
0587     else
0588         return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
0589 }
0590 
0591 static void
0592 setva(struct layer2 *l2, unsigned int nr)
0593 {
0594     struct sk_buff  *skb;
0595 
0596     while (l2->va != nr) {
0597         l2->va++;
0598         if (test_bit(FLG_MOD128, &l2->flag))
0599             l2->va %= 128;
0600         else
0601             l2->va %= 8;
0602         if (l2->windowar[l2->sow]) {
0603             skb_trim(l2->windowar[l2->sow], 0);
0604             skb_queue_tail(&l2->tmp_queue, l2->windowar[l2->sow]);
0605             l2->windowar[l2->sow] = NULL;
0606         }
0607         l2->sow = (l2->sow + 1) % l2->window;
0608     }
0609     skb = skb_dequeue(&l2->tmp_queue);
0610     while (skb) {
0611         dev_kfree_skb(skb);
0612         skb = skb_dequeue(&l2->tmp_queue);
0613     }
0614 }
0615 
0616 static void
0617 send_uframe(struct layer2 *l2, struct sk_buff *skb, u_char cmd, u_char cr)
0618 {
0619     u_char tmp[MAX_L2HEADER_LEN];
0620     int i;
0621 
0622     i = sethdraddr(l2, tmp, cr);
0623     tmp[i++] = cmd;
0624     if (skb)
0625         skb_trim(skb, 0);
0626     else {
0627         skb = mI_alloc_skb(i, GFP_ATOMIC);
0628         if (!skb) {
0629             printk(KERN_WARNING "%s: can't alloc skbuff in %s\n",
0630                    mISDNDevName4ch(&l2->ch), __func__);
0631             return;
0632         }
0633     }
0634     skb_put_data(skb, tmp, i);
0635     enqueue_super(l2, skb);
0636 }
0637 
0638 
0639 inline u_char
0640 get_PollFlag(struct layer2 *l2, struct sk_buff *skb)
0641 {
0642     return skb->data[l2addrsize(l2)] & 0x10;
0643 }
0644 
0645 inline u_char
0646 get_PollFlagFree(struct layer2 *l2, struct sk_buff *skb)
0647 {
0648     u_char PF;
0649 
0650     PF = get_PollFlag(l2, skb);
0651     dev_kfree_skb(skb);
0652     return PF;
0653 }
0654 
0655 inline void
0656 start_t200(struct layer2 *l2, int i)
0657 {
0658     mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
0659     test_and_set_bit(FLG_T200_RUN, &l2->flag);
0660 }
0661 
0662 inline void
0663 restart_t200(struct layer2 *l2, int i)
0664 {
0665     mISDN_FsmRestartTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
0666     test_and_set_bit(FLG_T200_RUN, &l2->flag);
0667 }
0668 
0669 inline void
0670 stop_t200(struct layer2 *l2, int i)
0671 {
0672     if (test_and_clear_bit(FLG_T200_RUN, &l2->flag))
0673         mISDN_FsmDelTimer(&l2->t200, i);
0674 }
0675 
0676 inline void
0677 st5_dl_release_l2l3(struct layer2 *l2)
0678 {
0679     int pr;
0680 
0681     if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
0682         pr = DL_RELEASE_CNF;
0683     else
0684         pr = DL_RELEASE_IND;
0685     l2up_create(l2, pr, 0, NULL);
0686 }
0687 
0688 inline void
0689 lapb_dl_release_l2l3(struct layer2 *l2, int f)
0690 {
0691     if (test_bit(FLG_LAPB, &l2->flag))
0692         l2down_create(l2, PH_DEACTIVATE_REQ, l2_newid(l2), 0, NULL);
0693     l2up_create(l2, f, 0, NULL);
0694 }
0695 
0696 static void
0697 establishlink(struct FsmInst *fi)
0698 {
0699     struct layer2 *l2 = fi->userdata;
0700     u_char cmd;
0701 
0702     clear_exception(l2);
0703     l2->rc = 0;
0704     cmd = (test_bit(FLG_MOD128, &l2->flag) ? SABME : SABM) | 0x10;
0705     send_uframe(l2, NULL, cmd, CMD);
0706     mISDN_FsmDelTimer(&l2->t203, 1);
0707     restart_t200(l2, 1);
0708     test_and_clear_bit(FLG_PEND_REL, &l2->flag);
0709     freewin(l2);
0710     mISDN_FsmChangeState(fi, ST_L2_5);
0711 }
0712 
0713 static void
0714 l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
0715 {
0716     struct sk_buff *skb = arg;
0717     struct layer2 *l2 = fi->userdata;
0718 
0719     if (get_PollFlagFree(l2, skb))
0720         l2mgr(l2, MDL_ERROR_IND, (void *) 'C');
0721     else
0722         l2mgr(l2, MDL_ERROR_IND, (void *) 'D');
0723 
0724 }
0725 
0726 static void
0727 l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
0728 {
0729     struct sk_buff *skb = arg;
0730     struct layer2 *l2 = fi->userdata;
0731 
0732     if (get_PollFlagFree(l2, skb))
0733         l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
0734     else {
0735         l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
0736         establishlink(fi);
0737         test_and_clear_bit(FLG_L3_INIT, &l2->flag);
0738     }
0739 }
0740 
0741 static void
0742 l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
0743 {
0744     struct sk_buff *skb = arg;
0745     struct layer2 *l2 = fi->userdata;
0746 
0747     if (get_PollFlagFree(l2, skb))
0748         l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
0749     else
0750         l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
0751     establishlink(fi);
0752     test_and_clear_bit(FLG_L3_INIT, &l2->flag);
0753 }
0754 
0755 static void
0756 l2_go_st3(struct FsmInst *fi, int event, void *arg)
0757 {
0758     dev_kfree_skb((struct sk_buff *)arg);
0759     mISDN_FsmChangeState(fi, ST_L2_3);
0760 }
0761 
0762 static void
0763 l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
0764 {
0765     struct layer2   *l2 = fi->userdata;
0766 
0767     mISDN_FsmChangeState(fi, ST_L2_3);
0768     dev_kfree_skb((struct sk_buff *)arg);
0769     l2_tei(l2, MDL_ASSIGN_IND, 0);
0770 }
0771 
0772 static void
0773 l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
0774 {
0775     struct layer2 *l2 = fi->userdata;
0776     struct sk_buff *skb = arg;
0777 
0778     skb_queue_tail(&l2->ui_queue, skb);
0779     mISDN_FsmChangeState(fi, ST_L2_2);
0780     l2_tei(l2, MDL_ASSIGN_IND, 0);
0781 }
0782 
0783 static void
0784 l2_queue_ui(struct FsmInst *fi, int event, void *arg)
0785 {
0786     struct layer2 *l2 = fi->userdata;
0787     struct sk_buff *skb = arg;
0788 
0789     skb_queue_tail(&l2->ui_queue, skb);
0790 }
0791 
0792 static void
0793 tx_ui(struct layer2 *l2)
0794 {
0795     struct sk_buff *skb;
0796     u_char header[MAX_L2HEADER_LEN];
0797     int i;
0798 
0799     i = sethdraddr(l2, header, CMD);
0800     if (test_bit(FLG_LAPD_NET, &l2->flag))
0801         header[1] = 0xff; /* tei 127 */
0802     header[i++] = UI;
0803     while ((skb = skb_dequeue(&l2->ui_queue))) {
0804         memcpy(skb_push(skb, i), header, i);
0805         enqueue_ui(l2, skb);
0806     }
0807 }
0808 
0809 static void
0810 l2_send_ui(struct FsmInst *fi, int event, void *arg)
0811 {
0812     struct layer2 *l2 = fi->userdata;
0813     struct sk_buff *skb = arg;
0814 
0815     skb_queue_tail(&l2->ui_queue, skb);
0816     tx_ui(l2);
0817 }
0818 
0819 static void
0820 l2_got_ui(struct FsmInst *fi, int event, void *arg)
0821 {
0822     struct layer2 *l2 = fi->userdata;
0823     struct sk_buff *skb = arg;
0824 
0825     skb_pull(skb, l2headersize(l2, 1));
0826 /*
0827  *      in states 1-3 for broadcast
0828  */
0829 
0830     if (l2->tm)
0831         l2_tei(l2, MDL_STATUS_UI_IND, 0);
0832     l2up(l2, DL_UNITDATA_IND, skb);
0833 }
0834 
0835 static void
0836 l2_establish(struct FsmInst *fi, int event, void *arg)
0837 {
0838     struct sk_buff *skb = arg;
0839     struct layer2 *l2 = fi->userdata;
0840 
0841     establishlink(fi);
0842     test_and_set_bit(FLG_L3_INIT, &l2->flag);
0843     dev_kfree_skb(skb);
0844 }
0845 
0846 static void
0847 l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
0848 {
0849     struct sk_buff *skb = arg;
0850     struct layer2 *l2 = fi->userdata;
0851 
0852     skb_queue_purge(&l2->i_queue);
0853     test_and_set_bit(FLG_L3_INIT, &l2->flag);
0854     test_and_clear_bit(FLG_PEND_REL, &l2->flag);
0855     dev_kfree_skb(skb);
0856 }
0857 
0858 static void
0859 l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
0860 {
0861     struct sk_buff *skb = arg;
0862     struct layer2 *l2 = fi->userdata;
0863 
0864     skb_queue_purge(&l2->i_queue);
0865     establishlink(fi);
0866     test_and_set_bit(FLG_L3_INIT, &l2->flag);
0867     dev_kfree_skb(skb);
0868 }
0869 
0870 static void
0871 l2_release(struct FsmInst *fi, int event, void *arg)
0872 {
0873     struct layer2 *l2 = fi->userdata;
0874     struct sk_buff *skb = arg;
0875 
0876     skb_trim(skb, 0);
0877     l2up(l2, DL_RELEASE_CNF, skb);
0878 }
0879 
0880 static void
0881 l2_pend_rel(struct FsmInst *fi, int event, void *arg)
0882 {
0883     struct sk_buff *skb = arg;
0884     struct layer2 *l2 = fi->userdata;
0885 
0886     test_and_set_bit(FLG_PEND_REL, &l2->flag);
0887     dev_kfree_skb(skb);
0888 }
0889 
0890 static void
0891 l2_disconnect(struct FsmInst *fi, int event, void *arg)
0892 {
0893     struct layer2 *l2 = fi->userdata;
0894     struct sk_buff *skb = arg;
0895 
0896     skb_queue_purge(&l2->i_queue);
0897     freewin(l2);
0898     mISDN_FsmChangeState(fi, ST_L2_6);
0899     l2->rc = 0;
0900     send_uframe(l2, NULL, DISC | 0x10, CMD);
0901     mISDN_FsmDelTimer(&l2->t203, 1);
0902     restart_t200(l2, 2);
0903     dev_kfree_skb(skb);
0904 }
0905 
0906 static void
0907 l2_start_multi(struct FsmInst *fi, int event, void *arg)
0908 {
0909     struct layer2   *l2 = fi->userdata;
0910     struct sk_buff  *skb = arg;
0911 
0912     l2->vs = 0;
0913     l2->va = 0;
0914     l2->vr = 0;
0915     l2->sow = 0;
0916     clear_exception(l2);
0917     send_uframe(l2, NULL, UA | get_PollFlag(l2, skb), RSP);
0918     mISDN_FsmChangeState(fi, ST_L2_7);
0919     mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
0920     skb_trim(skb, 0);
0921     l2up(l2, DL_ESTABLISH_IND, skb);
0922     if (l2->tm)
0923         l2_tei(l2, MDL_STATUS_UP_IND, 0);
0924 }
0925 
0926 static void
0927 l2_send_UA(struct FsmInst *fi, int event, void *arg)
0928 {
0929     struct layer2 *l2 = fi->userdata;
0930     struct sk_buff *skb = arg;
0931 
0932     send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
0933 }
0934 
0935 static void
0936 l2_send_DM(struct FsmInst *fi, int event, void *arg)
0937 {
0938     struct layer2 *l2 = fi->userdata;
0939     struct sk_buff *skb = arg;
0940 
0941     send_uframe(l2, skb, DM | get_PollFlag(l2, skb), RSP);
0942 }
0943 
0944 static void
0945 l2_restart_multi(struct FsmInst *fi, int event, void *arg)
0946 {
0947     struct layer2   *l2 = fi->userdata;
0948     struct sk_buff  *skb = arg;
0949     int     est = 0;
0950 
0951     send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
0952 
0953     l2mgr(l2, MDL_ERROR_IND, (void *) 'F');
0954 
0955     if (l2->vs != l2->va) {
0956         skb_queue_purge(&l2->i_queue);
0957         est = 1;
0958     }
0959 
0960     clear_exception(l2);
0961     l2->vs = 0;
0962     l2->va = 0;
0963     l2->vr = 0;
0964     l2->sow = 0;
0965     mISDN_FsmChangeState(fi, ST_L2_7);
0966     stop_t200(l2, 3);
0967     mISDN_FsmRestartTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
0968 
0969     if (est)
0970         l2up_create(l2, DL_ESTABLISH_IND, 0, NULL);
0971 /*      mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
0972  *          MGR_SHORTSTATUS | INDICATION, SSTATUS_L2_ESTABLISHED,
0973  *          0, NULL, 0);
0974  */
0975     if (skb_queue_len(&l2->i_queue) && cansend(l2))
0976         mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
0977 }
0978 
0979 static void
0980 l2_stop_multi(struct FsmInst *fi, int event, void *arg)
0981 {
0982     struct layer2   *l2 = fi->userdata;
0983     struct sk_buff  *skb = arg;
0984 
0985     mISDN_FsmChangeState(fi, ST_L2_4);
0986     mISDN_FsmDelTimer(&l2->t203, 3);
0987     stop_t200(l2, 4);
0988 
0989     send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
0990     skb_queue_purge(&l2->i_queue);
0991     freewin(l2);
0992     lapb_dl_release_l2l3(l2, DL_RELEASE_IND);
0993     if (l2->tm)
0994         l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
0995 }
0996 
0997 static void
0998 l2_connected(struct FsmInst *fi, int event, void *arg)
0999 {
1000     struct layer2   *l2 = fi->userdata;
1001     struct sk_buff  *skb = arg;
1002     int pr = -1;
1003 
1004     if (!get_PollFlag(l2, skb)) {
1005         l2_mdl_error_ua(fi, event, arg);
1006         return;
1007     }
1008     dev_kfree_skb(skb);
1009     if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
1010         l2_disconnect(fi, event, NULL);
1011     if (test_and_clear_bit(FLG_L3_INIT, &l2->flag)) {
1012         pr = DL_ESTABLISH_CNF;
1013     } else if (l2->vs != l2->va) {
1014         skb_queue_purge(&l2->i_queue);
1015         pr = DL_ESTABLISH_IND;
1016     }
1017     stop_t200(l2, 5);
1018     l2->vr = 0;
1019     l2->vs = 0;
1020     l2->va = 0;
1021     l2->sow = 0;
1022     mISDN_FsmChangeState(fi, ST_L2_7);
1023     mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 4);
1024     if (pr != -1)
1025         l2up_create(l2, pr, 0, NULL);
1026 
1027     if (skb_queue_len(&l2->i_queue) && cansend(l2))
1028         mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1029 
1030     if (l2->tm)
1031         l2_tei(l2, MDL_STATUS_UP_IND, 0);
1032 }
1033 
1034 static void
1035 l2_released(struct FsmInst *fi, int event, void *arg)
1036 {
1037     struct layer2 *l2 = fi->userdata;
1038     struct sk_buff *skb = arg;
1039 
1040     if (!get_PollFlag(l2, skb)) {
1041         l2_mdl_error_ua(fi, event, arg);
1042         return;
1043     }
1044     dev_kfree_skb(skb);
1045     stop_t200(l2, 6);
1046     lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1047     mISDN_FsmChangeState(fi, ST_L2_4);
1048     if (l2->tm)
1049         l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1050 }
1051 
1052 static void
1053 l2_reestablish(struct FsmInst *fi, int event, void *arg)
1054 {
1055     struct layer2 *l2 = fi->userdata;
1056     struct sk_buff *skb = arg;
1057 
1058     if (!get_PollFlagFree(l2, skb)) {
1059         establishlink(fi);
1060         test_and_set_bit(FLG_L3_INIT, &l2->flag);
1061     }
1062 }
1063 
1064 static void
1065 l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
1066 {
1067     struct layer2 *l2 = fi->userdata;
1068     struct sk_buff *skb = arg;
1069 
1070     if (get_PollFlagFree(l2, skb)) {
1071         stop_t200(l2, 7);
1072         if (!test_bit(FLG_L3_INIT, &l2->flag))
1073             skb_queue_purge(&l2->i_queue);
1074         if (test_bit(FLG_LAPB, &l2->flag))
1075             l2down_create(l2, PH_DEACTIVATE_REQ,
1076                       l2_newid(l2), 0, NULL);
1077         st5_dl_release_l2l3(l2);
1078         mISDN_FsmChangeState(fi, ST_L2_4);
1079         if (l2->tm)
1080             l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1081     }
1082 }
1083 
1084 static void
1085 l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
1086 {
1087     struct layer2 *l2 = fi->userdata;
1088     struct sk_buff *skb = arg;
1089 
1090     if (get_PollFlagFree(l2, skb)) {
1091         stop_t200(l2, 8);
1092         lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1093         mISDN_FsmChangeState(fi, ST_L2_4);
1094         if (l2->tm)
1095             l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1096     }
1097 }
1098 
1099 static void
1100 enquiry_cr(struct layer2 *l2, u_char typ, u_char cr, u_char pf)
1101 {
1102     struct sk_buff *skb;
1103     u_char tmp[MAX_L2HEADER_LEN];
1104     int i;
1105 
1106     i = sethdraddr(l2, tmp, cr);
1107     if (test_bit(FLG_MOD128, &l2->flag)) {
1108         tmp[i++] = typ;
1109         tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
1110     } else
1111         tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
1112     skb = mI_alloc_skb(i, GFP_ATOMIC);
1113     if (!skb) {
1114         printk(KERN_WARNING "%s: isdnl2 can't alloc sbbuff in %s\n",
1115                mISDNDevName4ch(&l2->ch), __func__);
1116         return;
1117     }
1118     skb_put_data(skb, tmp, i);
1119     enqueue_super(l2, skb);
1120 }
1121 
1122 inline void
1123 enquiry_response(struct layer2 *l2)
1124 {
1125     if (test_bit(FLG_OWN_BUSY, &l2->flag))
1126         enquiry_cr(l2, RNR, RSP, 1);
1127     else
1128         enquiry_cr(l2, RR, RSP, 1);
1129     test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1130 }
1131 
1132 inline void
1133 transmit_enquiry(struct layer2 *l2)
1134 {
1135     if (test_bit(FLG_OWN_BUSY, &l2->flag))
1136         enquiry_cr(l2, RNR, CMD, 1);
1137     else
1138         enquiry_cr(l2, RR, CMD, 1);
1139     test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1140     start_t200(l2, 9);
1141 }
1142 
1143 
1144 static void
1145 nrerrorrecovery(struct FsmInst *fi)
1146 {
1147     struct layer2 *l2 = fi->userdata;
1148 
1149     l2mgr(l2, MDL_ERROR_IND, (void *) 'J');
1150     establishlink(fi);
1151     test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1152 }
1153 
1154 static void
1155 invoke_retransmission(struct layer2 *l2, unsigned int nr)
1156 {
1157     u_int   p1;
1158 
1159     if (l2->vs != nr) {
1160         while (l2->vs != nr) {
1161             (l2->vs)--;
1162             if (test_bit(FLG_MOD128, &l2->flag)) {
1163                 l2->vs %= 128;
1164                 p1 = (l2->vs - l2->va) % 128;
1165             } else {
1166                 l2->vs %= 8;
1167                 p1 = (l2->vs - l2->va) % 8;
1168             }
1169             p1 = (p1 + l2->sow) % l2->window;
1170             if (l2->windowar[p1])
1171                 skb_queue_head(&l2->i_queue, l2->windowar[p1]);
1172             else
1173                 printk(KERN_WARNING
1174                        "%s: windowar[%d] is NULL\n",
1175                        mISDNDevName4ch(&l2->ch), p1);
1176             l2->windowar[p1] = NULL;
1177         }
1178         mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
1179     }
1180 }
1181 
1182 static void
1183 l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
1184 {
1185     struct layer2 *l2 = fi->userdata;
1186     struct sk_buff *skb = arg;
1187     int PollFlag, rsp, typ = RR;
1188     unsigned int nr;
1189 
1190     rsp = *skb->data & 0x2;
1191     if (test_bit(FLG_ORIG, &l2->flag))
1192         rsp = !rsp;
1193 
1194     skb_pull(skb, l2addrsize(l2));
1195     if (IsRNR(skb->data, l2)) {
1196         set_peer_busy(l2);
1197         typ = RNR;
1198     } else
1199         clear_peer_busy(l2);
1200     if (IsREJ(skb->data, l2))
1201         typ = REJ;
1202 
1203     if (test_bit(FLG_MOD128, &l2->flag)) {
1204         PollFlag = (skb->data[1] & 0x1) == 0x1;
1205         nr = skb->data[1] >> 1;
1206     } else {
1207         PollFlag = (skb->data[0] & 0x10);
1208         nr = (skb->data[0] >> 5) & 0x7;
1209     }
1210     dev_kfree_skb(skb);
1211 
1212     if (PollFlag) {
1213         if (rsp)
1214             l2mgr(l2, MDL_ERROR_IND, (void *) 'A');
1215         else
1216             enquiry_response(l2);
1217     }
1218     if (legalnr(l2, nr)) {
1219         if (typ == REJ) {
1220             setva(l2, nr);
1221             invoke_retransmission(l2, nr);
1222             stop_t200(l2, 10);
1223             if (mISDN_FsmAddTimer(&l2->t203, l2->T203,
1224                           EV_L2_T203, NULL, 6))
1225                 l2m_debug(&l2->l2m, "Restart T203 ST7 REJ");
1226         } else if ((nr == l2->vs) && (typ == RR)) {
1227             setva(l2, nr);
1228             stop_t200(l2, 11);
1229             mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1230                           EV_L2_T203, NULL, 7);
1231         } else if ((l2->va != nr) || (typ == RNR)) {
1232             setva(l2, nr);
1233             if (typ != RR)
1234                 mISDN_FsmDelTimer(&l2->t203, 9);
1235             restart_t200(l2, 12);
1236         }
1237         if (skb_queue_len(&l2->i_queue) && (typ == RR))
1238             mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1239     } else
1240         nrerrorrecovery(fi);
1241 }
1242 
1243 static void
1244 l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
1245 {
1246     struct layer2 *l2 = fi->userdata;
1247     struct sk_buff *skb = arg;
1248 
1249     if (!test_bit(FLG_L3_INIT, &l2->flag))
1250         skb_queue_tail(&l2->i_queue, skb);
1251     else
1252         dev_kfree_skb(skb);
1253 }
1254 
1255 static void
1256 l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
1257 {
1258     struct layer2 *l2 = fi->userdata;
1259     struct sk_buff *skb = arg;
1260 
1261     skb_queue_tail(&l2->i_queue, skb);
1262     mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1263 }
1264 
1265 static void
1266 l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
1267 {
1268     struct layer2 *l2 = fi->userdata;
1269     struct sk_buff *skb = arg;
1270 
1271     skb_queue_tail(&l2->i_queue, skb);
1272 }
1273 
1274 static void
1275 l2_got_iframe(struct FsmInst *fi, int event, void *arg)
1276 {
1277     struct layer2   *l2 = fi->userdata;
1278     struct sk_buff  *skb = arg;
1279     int     PollFlag, i;
1280     u_int       ns, nr;
1281 
1282     i = l2addrsize(l2);
1283     if (test_bit(FLG_MOD128, &l2->flag)) {
1284         PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
1285         ns = skb->data[i] >> 1;
1286         nr = (skb->data[i + 1] >> 1) & 0x7f;
1287     } else {
1288         PollFlag = (skb->data[i] & 0x10);
1289         ns = (skb->data[i] >> 1) & 0x7;
1290         nr = (skb->data[i] >> 5) & 0x7;
1291     }
1292     if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
1293         dev_kfree_skb(skb);
1294         if (PollFlag)
1295             enquiry_response(l2);
1296     } else {
1297         if (l2->vr == ns) {
1298             l2->vr++;
1299             if (test_bit(FLG_MOD128, &l2->flag))
1300                 l2->vr %= 128;
1301             else
1302                 l2->vr %= 8;
1303             test_and_clear_bit(FLG_REJEXC, &l2->flag);
1304             if (PollFlag)
1305                 enquiry_response(l2);
1306             else
1307                 test_and_set_bit(FLG_ACK_PEND, &l2->flag);
1308             skb_pull(skb, l2headersize(l2, 0));
1309             l2up(l2, DL_DATA_IND, skb);
1310         } else {
1311             /* n(s)!=v(r) */
1312             dev_kfree_skb(skb);
1313             if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
1314                 if (PollFlag)
1315                     enquiry_response(l2);
1316             } else {
1317                 enquiry_cr(l2, REJ, RSP, PollFlag);
1318                 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1319             }
1320         }
1321     }
1322     if (legalnr(l2, nr)) {
1323         if (!test_bit(FLG_PEER_BUSY, &l2->flag) &&
1324             (fi->state == ST_L2_7)) {
1325             if (nr == l2->vs) {
1326                 stop_t200(l2, 13);
1327                 mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1328                               EV_L2_T203, NULL, 7);
1329             } else if (nr != l2->va)
1330                 restart_t200(l2, 14);
1331         }
1332         setva(l2, nr);
1333     } else {
1334         nrerrorrecovery(fi);
1335         return;
1336     }
1337     if (skb_queue_len(&l2->i_queue) && (fi->state == ST_L2_7))
1338         mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1339     if (test_and_clear_bit(FLG_ACK_PEND, &l2->flag))
1340         enquiry_cr(l2, RR, RSP, 0);
1341 }
1342 
1343 static void
1344 l2_got_tei(struct FsmInst *fi, int event, void *arg)
1345 {
1346     struct layer2   *l2 = fi->userdata;
1347     u_int       info;
1348 
1349     l2->tei = (signed char)(long)arg;
1350     set_channel_address(&l2->ch, l2->sapi, l2->tei);
1351     info = DL_INFO_L2_CONNECT;
1352     l2up_create(l2, DL_INFORMATION_IND, sizeof(info), &info);
1353     if (fi->state == ST_L2_3) {
1354         establishlink(fi);
1355         test_and_set_bit(FLG_L3_INIT, &l2->flag);
1356     } else
1357         mISDN_FsmChangeState(fi, ST_L2_4);
1358     if (skb_queue_len(&l2->ui_queue))
1359         tx_ui(l2);
1360 }
1361 
1362 static void
1363 l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
1364 {
1365     struct layer2 *l2 = fi->userdata;
1366 
1367     if (test_bit(FLG_LAPD, &l2->flag) &&
1368         test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1369         mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1370     } else if (l2->rc == l2->N200) {
1371         mISDN_FsmChangeState(fi, ST_L2_4);
1372         test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1373         skb_queue_purge(&l2->i_queue);
1374         l2mgr(l2, MDL_ERROR_IND, (void *) 'G');
1375         if (test_bit(FLG_LAPB, &l2->flag))
1376             l2down_create(l2, PH_DEACTIVATE_REQ,
1377                       l2_newid(l2), 0, NULL);
1378         st5_dl_release_l2l3(l2);
1379         if (l2->tm)
1380             l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1381     } else {
1382         l2->rc++;
1383         mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1384         send_uframe(l2, NULL, (test_bit(FLG_MOD128, &l2->flag) ?
1385                        SABME : SABM) | 0x10, CMD);
1386     }
1387 }
1388 
1389 static void
1390 l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
1391 {
1392     struct layer2 *l2 = fi->userdata;
1393 
1394     if (test_bit(FLG_LAPD, &l2->flag) &&
1395         test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1396         mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1397     } else if (l2->rc == l2->N200) {
1398         mISDN_FsmChangeState(fi, ST_L2_4);
1399         test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1400         l2mgr(l2, MDL_ERROR_IND, (void *) 'H');
1401         lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1402         if (l2->tm)
1403             l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1404     } else {
1405         l2->rc++;
1406         mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200,
1407                   NULL, 9);
1408         send_uframe(l2, NULL, DISC | 0x10, CMD);
1409     }
1410 }
1411 
1412 static void
1413 l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
1414 {
1415     struct layer2 *l2 = fi->userdata;
1416 
1417     if (test_bit(FLG_LAPD, &l2->flag) &&
1418         test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1419         mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1420         return;
1421     }
1422     test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1423     l2->rc = 0;
1424     mISDN_FsmChangeState(fi, ST_L2_8);
1425     transmit_enquiry(l2);
1426     l2->rc++;
1427 }
1428 
1429 static void
1430 l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
1431 {
1432     struct layer2 *l2 = fi->userdata;
1433 
1434     if (test_bit(FLG_LAPD, &l2->flag) &&
1435         test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1436         mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1437         return;
1438     }
1439     test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1440     if (l2->rc == l2->N200) {
1441         l2mgr(l2, MDL_ERROR_IND, (void *) 'I');
1442         establishlink(fi);
1443         test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1444     } else {
1445         transmit_enquiry(l2);
1446         l2->rc++;
1447     }
1448 }
1449 
1450 static void
1451 l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
1452 {
1453     struct layer2 *l2 = fi->userdata;
1454 
1455     if (test_bit(FLG_LAPD, &l2->flag) &&
1456         test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1457         mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 9);
1458         return;
1459     }
1460     mISDN_FsmChangeState(fi, ST_L2_8);
1461     transmit_enquiry(l2);
1462     l2->rc = 0;
1463 }
1464 
1465 static void
1466 l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
1467 {
1468     struct layer2   *l2 = fi->userdata;
1469     struct sk_buff  *skb, *nskb;
1470     u_char      header[MAX_L2HEADER_LEN];
1471     u_int       i, p1;
1472 
1473     if (!cansend(l2))
1474         return;
1475 
1476     skb = skb_dequeue(&l2->i_queue);
1477     if (!skb)
1478         return;
1479     i = sethdraddr(l2, header, CMD);
1480     if (test_bit(FLG_MOD128, &l2->flag)) {
1481         header[i++] = l2->vs << 1;
1482         header[i++] = l2->vr << 1;
1483     } else
1484         header[i++] = (l2->vr << 5) | (l2->vs << 1);
1485     nskb = skb_realloc_headroom(skb, i);
1486     if (!nskb) {
1487         printk(KERN_WARNING "%s: no headroom(%d) copy for IFrame\n",
1488                mISDNDevName4ch(&l2->ch), i);
1489         skb_queue_head(&l2->i_queue, skb);
1490         return;
1491     }
1492     if (test_bit(FLG_MOD128, &l2->flag)) {
1493         p1 = (l2->vs - l2->va) % 128;
1494         l2->vs = (l2->vs + 1) % 128;
1495     } else {
1496         p1 = (l2->vs - l2->va) % 8;
1497         l2->vs = (l2->vs + 1) % 8;
1498     }
1499     p1 = (p1 + l2->sow) % l2->window;
1500     if (l2->windowar[p1]) {
1501         printk(KERN_WARNING "%s: l2 try overwrite ack queue entry %d\n",
1502                mISDNDevName4ch(&l2->ch), p1);
1503         dev_kfree_skb(l2->windowar[p1]);
1504     }
1505     l2->windowar[p1] = skb;
1506     memcpy(skb_push(nskb, i), header, i);
1507     l2down(l2, PH_DATA_REQ, l2_newid(l2), nskb);
1508     test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1509     if (!test_and_set_bit(FLG_T200_RUN, &l2->flag)) {
1510         mISDN_FsmDelTimer(&l2->t203, 13);
1511         mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 11);
1512     }
1513 }
1514 
1515 static void
1516 l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
1517 {
1518     struct layer2 *l2 = fi->userdata;
1519     struct sk_buff *skb = arg;
1520     int PollFlag, rsp, rnr = 0;
1521     unsigned int nr;
1522 
1523     rsp = *skb->data & 0x2;
1524     if (test_bit(FLG_ORIG, &l2->flag))
1525         rsp = !rsp;
1526 
1527     skb_pull(skb, l2addrsize(l2));
1528 
1529     if (IsRNR(skb->data, l2)) {
1530         set_peer_busy(l2);
1531         rnr = 1;
1532     } else
1533         clear_peer_busy(l2);
1534 
1535     if (test_bit(FLG_MOD128, &l2->flag)) {
1536         PollFlag = (skb->data[1] & 0x1) == 0x1;
1537         nr = skb->data[1] >> 1;
1538     } else {
1539         PollFlag = (skb->data[0] & 0x10);
1540         nr = (skb->data[0] >> 5) & 0x7;
1541     }
1542     dev_kfree_skb(skb);
1543     if (rsp && PollFlag) {
1544         if (legalnr(l2, nr)) {
1545             if (rnr) {
1546                 restart_t200(l2, 15);
1547             } else {
1548                 stop_t200(l2, 16);
1549                 mISDN_FsmAddTimer(&l2->t203, l2->T203,
1550                           EV_L2_T203, NULL, 5);
1551                 setva(l2, nr);
1552             }
1553             invoke_retransmission(l2, nr);
1554             mISDN_FsmChangeState(fi, ST_L2_7);
1555             if (skb_queue_len(&l2->i_queue) && cansend(l2))
1556                 mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1557         } else
1558             nrerrorrecovery(fi);
1559     } else {
1560         if (!rsp && PollFlag)
1561             enquiry_response(l2);
1562         if (legalnr(l2, nr))
1563             setva(l2, nr);
1564         else
1565             nrerrorrecovery(fi);
1566     }
1567 }
1568 
1569 static void
1570 l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
1571 {
1572     struct layer2 *l2 = fi->userdata;
1573     struct sk_buff *skb = arg;
1574 
1575     skb_pull(skb, l2addrsize(l2) + 1);
1576 
1577     if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) || /* I or S */
1578         (IsUA(skb->data) && (fi->state == ST_L2_7))) {
1579         l2mgr(l2, MDL_ERROR_IND, (void *) 'K');
1580         establishlink(fi);
1581         test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1582     }
1583     dev_kfree_skb(skb);
1584 }
1585 
1586 static void
1587 l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
1588 {
1589     struct layer2 *l2 = fi->userdata;
1590 
1591     skb_queue_purge(&l2->ui_queue);
1592     l2->tei = GROUP_TEI;
1593     mISDN_FsmChangeState(fi, ST_L2_1);
1594 }
1595 
1596 static void
1597 l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
1598 {
1599     struct layer2 *l2 = fi->userdata;
1600 
1601     skb_queue_purge(&l2->ui_queue);
1602     l2->tei = GROUP_TEI;
1603     l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1604     mISDN_FsmChangeState(fi, ST_L2_1);
1605 }
1606 
1607 static void
1608 l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
1609 {
1610     struct layer2 *l2 = fi->userdata;
1611 
1612     skb_queue_purge(&l2->i_queue);
1613     skb_queue_purge(&l2->ui_queue);
1614     freewin(l2);
1615     l2->tei = GROUP_TEI;
1616     stop_t200(l2, 17);
1617     st5_dl_release_l2l3(l2);
1618     mISDN_FsmChangeState(fi, ST_L2_1);
1619 }
1620 
1621 static void
1622 l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
1623 {
1624     struct layer2 *l2 = fi->userdata;
1625 
1626     skb_queue_purge(&l2->ui_queue);
1627     l2->tei = GROUP_TEI;
1628     stop_t200(l2, 18);
1629     l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1630     mISDN_FsmChangeState(fi, ST_L2_1);
1631 }
1632 
1633 static void
1634 l2_tei_remove(struct FsmInst *fi, int event, void *arg)
1635 {
1636     struct layer2 *l2 = fi->userdata;
1637 
1638     skb_queue_purge(&l2->i_queue);
1639     skb_queue_purge(&l2->ui_queue);
1640     freewin(l2);
1641     l2->tei = GROUP_TEI;
1642     stop_t200(l2, 17);
1643     mISDN_FsmDelTimer(&l2->t203, 19);
1644     l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1645 /*  mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
1646  *      MGR_SHORTSTATUS_IND, SSTATUS_L2_RELEASED,
1647  *      0, NULL, 0);
1648  */
1649     mISDN_FsmChangeState(fi, ST_L2_1);
1650 }
1651 
1652 static void
1653 l2_st14_persistent_da(struct FsmInst *fi, int event, void *arg)
1654 {
1655     struct layer2 *l2 = fi->userdata;
1656     struct sk_buff *skb = arg;
1657 
1658     skb_queue_purge(&l2->i_queue);
1659     skb_queue_purge(&l2->ui_queue);
1660     if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
1661         l2up(l2, DL_RELEASE_IND, skb);
1662     else
1663         dev_kfree_skb(skb);
1664 }
1665 
1666 static void
1667 l2_st5_persistent_da(struct FsmInst *fi, int event, void *arg)
1668 {
1669     struct layer2 *l2 = fi->userdata;
1670     struct sk_buff *skb = arg;
1671 
1672     skb_queue_purge(&l2->i_queue);
1673     skb_queue_purge(&l2->ui_queue);
1674     freewin(l2);
1675     stop_t200(l2, 19);
1676     st5_dl_release_l2l3(l2);
1677     mISDN_FsmChangeState(fi, ST_L2_4);
1678     if (l2->tm)
1679         l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1680     dev_kfree_skb(skb);
1681 }
1682 
1683 static void
1684 l2_st6_persistent_da(struct FsmInst *fi, int event, void *arg)
1685 {
1686     struct layer2 *l2 = fi->userdata;
1687     struct sk_buff *skb = arg;
1688 
1689     skb_queue_purge(&l2->ui_queue);
1690     stop_t200(l2, 20);
1691     l2up(l2, DL_RELEASE_CNF, skb);
1692     mISDN_FsmChangeState(fi, ST_L2_4);
1693     if (l2->tm)
1694         l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1695 }
1696 
1697 static void
1698 l2_persistent_da(struct FsmInst *fi, int event, void *arg)
1699 {
1700     struct layer2 *l2 = fi->userdata;
1701     struct sk_buff *skb = arg;
1702 
1703     skb_queue_purge(&l2->i_queue);
1704     skb_queue_purge(&l2->ui_queue);
1705     freewin(l2);
1706     stop_t200(l2, 19);
1707     mISDN_FsmDelTimer(&l2->t203, 19);
1708     l2up(l2, DL_RELEASE_IND, skb);
1709     mISDN_FsmChangeState(fi, ST_L2_4);
1710     if (l2->tm)
1711         l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1712 }
1713 
1714 static void
1715 l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
1716 {
1717     struct layer2 *l2 = fi->userdata;
1718     struct sk_buff *skb = arg;
1719 
1720     if (!test_and_set_bit(FLG_OWN_BUSY, &l2->flag)) {
1721         enquiry_cr(l2, RNR, RSP, 0);
1722         test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1723     }
1724     dev_kfree_skb(skb);
1725 }
1726 
1727 static void
1728 l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
1729 {
1730     struct layer2 *l2 = fi->userdata;
1731     struct sk_buff *skb = arg;
1732 
1733     if (!test_and_clear_bit(FLG_OWN_BUSY, &l2->flag)) {
1734         enquiry_cr(l2, RR, RSP, 0);
1735         test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1736     }
1737     dev_kfree_skb(skb);
1738 }
1739 
1740 static void
1741 l2_frame_error(struct FsmInst *fi, int event, void *arg)
1742 {
1743     struct layer2 *l2 = fi->userdata;
1744 
1745     l2mgr(l2, MDL_ERROR_IND, arg);
1746 }
1747 
1748 static void
1749 l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
1750 {
1751     struct layer2 *l2 = fi->userdata;
1752 
1753     l2mgr(l2, MDL_ERROR_IND, arg);
1754     establishlink(fi);
1755     test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1756 }
1757 
1758 static struct FsmNode L2FnList[] =
1759 {
1760     {ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
1761     {ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
1762     {ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
1763     {ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
1764     {ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1765     {ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1766     {ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
1767     {ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
1768     {ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1769     {ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1770     {ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
1771     {ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
1772     {ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
1773     {ST_L2_1, EV_L2_DL_UNITDATA, l2_queue_ui_assign},
1774     {ST_L2_2, EV_L2_DL_UNITDATA, l2_queue_ui},
1775     {ST_L2_3, EV_L2_DL_UNITDATA, l2_queue_ui},
1776     {ST_L2_4, EV_L2_DL_UNITDATA, l2_send_ui},
1777     {ST_L2_5, EV_L2_DL_UNITDATA, l2_send_ui},
1778     {ST_L2_6, EV_L2_DL_UNITDATA, l2_send_ui},
1779     {ST_L2_7, EV_L2_DL_UNITDATA, l2_send_ui},
1780     {ST_L2_8, EV_L2_DL_UNITDATA, l2_send_ui},
1781     {ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
1782     {ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
1783     {ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
1784     {ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
1785     {ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
1786     {ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
1787     {ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
1788     {ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
1789     {ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
1790     {ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
1791     {ST_L2_4, EV_L2_SABME, l2_start_multi},
1792     {ST_L2_5, EV_L2_SABME, l2_send_UA},
1793     {ST_L2_6, EV_L2_SABME, l2_send_DM},
1794     {ST_L2_7, EV_L2_SABME, l2_restart_multi},
1795     {ST_L2_8, EV_L2_SABME, l2_restart_multi},
1796     {ST_L2_4, EV_L2_DISC, l2_send_DM},
1797     {ST_L2_5, EV_L2_DISC, l2_send_DM},
1798     {ST_L2_6, EV_L2_DISC, l2_send_UA},
1799     {ST_L2_7, EV_L2_DISC, l2_stop_multi},
1800     {ST_L2_8, EV_L2_DISC, l2_stop_multi},
1801     {ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
1802     {ST_L2_5, EV_L2_UA, l2_connected},
1803     {ST_L2_6, EV_L2_UA, l2_released},
1804     {ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
1805     {ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
1806     {ST_L2_4, EV_L2_DM, l2_reestablish},
1807     {ST_L2_5, EV_L2_DM, l2_st5_dm_release},
1808     {ST_L2_6, EV_L2_DM, l2_st6_dm_release},
1809     {ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
1810     {ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
1811     {ST_L2_1, EV_L2_UI, l2_got_ui},
1812     {ST_L2_2, EV_L2_UI, l2_got_ui},
1813     {ST_L2_3, EV_L2_UI, l2_got_ui},
1814     {ST_L2_4, EV_L2_UI, l2_got_ui},
1815     {ST_L2_5, EV_L2_UI, l2_got_ui},
1816     {ST_L2_6, EV_L2_UI, l2_got_ui},
1817     {ST_L2_7, EV_L2_UI, l2_got_ui},
1818     {ST_L2_8, EV_L2_UI, l2_got_ui},
1819     {ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
1820     {ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
1821     {ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
1822     {ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
1823     {ST_L2_7, EV_L2_I, l2_got_iframe},
1824     {ST_L2_8, EV_L2_I, l2_got_iframe},
1825     {ST_L2_5, EV_L2_T200, l2_timeout},
1826     {ST_L2_6, EV_L2_T200, l2_timeout},
1827     {ST_L2_7, EV_L2_T200, l2_timeout},
1828     {ST_L2_8, EV_L2_T200, l2_timeout},
1829     {ST_L2_7, EV_L2_T203, l2_timeout},
1830     {ST_L2_5, EV_L2_T200I, l2_st5_tout_200},
1831     {ST_L2_6, EV_L2_T200I, l2_st6_tout_200},
1832     {ST_L2_7, EV_L2_T200I, l2_st7_tout_200},
1833     {ST_L2_8, EV_L2_T200I, l2_st8_tout_200},
1834     {ST_L2_7, EV_L2_T203I, l2_st7_tout_203},
1835     {ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
1836     {ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1837     {ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1838     {ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1839     {ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1840     {ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
1841     {ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
1842     {ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
1843     {ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1844     {ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1845     {ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistent_da},
1846     {ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
1847     {ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
1848     {ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistent_da},
1849     {ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistent_da},
1850     {ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistent_da},
1851     {ST_L2_7, EV_L1_DEACTIVATE, l2_persistent_da},
1852     {ST_L2_8, EV_L1_DEACTIVATE, l2_persistent_da},
1853 };
1854 
1855 static int
1856 ph_data_indication(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb)
1857 {
1858     u_char  *datap = skb->data;
1859     int ret = -EINVAL;
1860     int psapi, ptei;
1861     u_int   l;
1862     int c = 0;
1863 
1864     l = l2addrsize(l2);
1865     if (skb->len <= l) {
1866         mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *) 'N');
1867         return ret;
1868     }
1869     if (test_bit(FLG_LAPD, &l2->flag)) { /* Maybe not needed */
1870         psapi = *datap++;
1871         ptei = *datap++;
1872         if ((psapi & 1) || !(ptei & 1)) {
1873             printk(KERN_WARNING
1874                    "%s l2 D-channel frame wrong EA0/EA1\n",
1875                    mISDNDevName4ch(&l2->ch));
1876             return ret;
1877         }
1878         psapi >>= 2;
1879         ptei >>= 1;
1880         if (psapi != l2->sapi) {
1881             /* not our business */
1882             if (*debug & DEBUG_L2)
1883                 printk(KERN_DEBUG "%s: sapi %d/%d mismatch\n",
1884                        mISDNDevName4ch(&l2->ch), psapi,
1885                        l2->sapi);
1886             dev_kfree_skb(skb);
1887             return 0;
1888         }
1889         if ((ptei != l2->tei) && (ptei != GROUP_TEI)) {
1890             /* not our business */
1891             if (*debug & DEBUG_L2)
1892                 printk(KERN_DEBUG "%s: tei %d/%d mismatch\n",
1893                        mISDNDevName4ch(&l2->ch), ptei, l2->tei);
1894             dev_kfree_skb(skb);
1895             return 0;
1896         }
1897     } else
1898         datap += l;
1899     if (!(*datap & 1)) {    /* I-Frame */
1900         c = iframe_error(l2, skb);
1901         if (!c)
1902             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_I, skb);
1903     } else if (IsSFrame(datap, l2)) {   /* S-Frame */
1904         c = super_error(l2, skb);
1905         if (!c)
1906             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SUPER, skb);
1907     } else if (IsUI(datap)) {
1908         c = UI_error(l2, skb);
1909         if (!c)
1910             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UI, skb);
1911     } else if (IsSABME(datap, l2)) {
1912         c = unnum_error(l2, skb, CMD);
1913         if (!c)
1914             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SABME, skb);
1915     } else if (IsUA(datap)) {
1916         c = unnum_error(l2, skb, RSP);
1917         if (!c)
1918             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UA, skb);
1919     } else if (IsDISC(datap)) {
1920         c = unnum_error(l2, skb, CMD);
1921         if (!c)
1922             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DISC, skb);
1923     } else if (IsDM(datap)) {
1924         c = unnum_error(l2, skb, RSP);
1925         if (!c)
1926             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DM, skb);
1927     } else if (IsFRMR(datap)) {
1928         c = FRMR_error(l2, skb);
1929         if (!c)
1930             ret = mISDN_FsmEvent(&l2->l2m, EV_L2_FRMR, skb);
1931     } else
1932         c = 'L';
1933     if (c) {
1934         printk(KERN_WARNING "%s:l2 D-channel frame error %c\n",
1935                mISDNDevName4ch(&l2->ch), c);
1936         mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
1937     }
1938     return ret;
1939 }
1940 
1941 static int
1942 l2_send(struct mISDNchannel *ch, struct sk_buff *skb)
1943 {
1944     struct layer2       *l2 = container_of(ch, struct layer2, ch);
1945     struct mISDNhead    *hh =  mISDN_HEAD_P(skb);
1946     int         ret = -EINVAL;
1947 
1948     if (*debug & DEBUG_L2_RECV)
1949         printk(KERN_DEBUG "%s: %s prim(%x) id(%x) sapi(%d) tei(%d)\n",
1950                __func__, mISDNDevName4ch(&l2->ch), hh->prim, hh->id,
1951                l2->sapi, l2->tei);
1952     if (hh->prim == DL_INTERN_MSG) {
1953         struct mISDNhead *chh = hh + 1; /* saved copy */
1954 
1955         *hh = *chh;
1956         if (*debug & DEBUG_L2_RECV)
1957             printk(KERN_DEBUG "%s: prim(%x) id(%x) internal msg\n",
1958                 mISDNDevName4ch(&l2->ch), hh->prim, hh->id);
1959     }
1960     switch (hh->prim) {
1961     case PH_DATA_IND:
1962         ret = ph_data_indication(l2, hh, skb);
1963         break;
1964     case PH_DATA_CNF:
1965         ret = ph_data_confirm(l2, hh, skb);
1966         break;
1967     case PH_ACTIVATE_IND:
1968         test_and_set_bit(FLG_L1_ACTIV, &l2->flag);
1969         l2up_create(l2, MPH_ACTIVATE_IND, 0, NULL);
1970         if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
1971             ret = mISDN_FsmEvent(&l2->l2m,
1972                          EV_L2_DL_ESTABLISH_REQ, skb);
1973         break;
1974     case PH_DEACTIVATE_IND:
1975         test_and_clear_bit(FLG_L1_ACTIV, &l2->flag);
1976         l2up_create(l2, MPH_DEACTIVATE_IND, 0, NULL);
1977         ret = mISDN_FsmEvent(&l2->l2m, EV_L1_DEACTIVATE, skb);
1978         break;
1979     case MPH_INFORMATION_IND:
1980         if (!l2->up)
1981             break;
1982         ret = l2->up->send(l2->up, skb);
1983         break;
1984     case DL_DATA_REQ:
1985         ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_DATA, skb);
1986         break;
1987     case DL_UNITDATA_REQ:
1988         ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_UNITDATA, skb);
1989         break;
1990     case DL_ESTABLISH_REQ:
1991         if (test_bit(FLG_LAPB, &l2->flag))
1992             test_and_set_bit(FLG_ORIG, &l2->flag);
1993         if (test_bit(FLG_L1_ACTIV, &l2->flag)) {
1994             if (test_bit(FLG_LAPD, &l2->flag) ||
1995                 test_bit(FLG_ORIG, &l2->flag))
1996                 ret = mISDN_FsmEvent(&l2->l2m,
1997                              EV_L2_DL_ESTABLISH_REQ, skb);
1998         } else {
1999             if (test_bit(FLG_LAPD, &l2->flag) ||
2000                 test_bit(FLG_ORIG, &l2->flag)) {
2001                 test_and_set_bit(FLG_ESTAB_PEND,
2002                          &l2->flag);
2003             }
2004             ret = l2down(l2, PH_ACTIVATE_REQ, l2_newid(l2),
2005                      skb);
2006         }
2007         break;
2008     case DL_RELEASE_REQ:
2009         if (test_bit(FLG_LAPB, &l2->flag))
2010             l2down_create(l2, PH_DEACTIVATE_REQ,
2011                       l2_newid(l2), 0, NULL);
2012         ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_RELEASE_REQ,
2013                      skb);
2014         break;
2015     case DL_TIMER200_IND:
2016         mISDN_FsmEvent(&l2->l2m, EV_L2_T200I, NULL);
2017         break;
2018     case DL_TIMER203_IND:
2019         mISDN_FsmEvent(&l2->l2m, EV_L2_T203I, NULL);
2020         break;
2021     default:
2022         if (*debug & DEBUG_L2)
2023             l2m_debug(&l2->l2m, "l2 unknown pr %04x",
2024                   hh->prim);
2025     }
2026     if (ret) {
2027         dev_kfree_skb(skb);
2028         ret = 0;
2029     }
2030     return ret;
2031 }
2032 
2033 int
2034 tei_l2(struct layer2 *l2, u_int cmd, u_long arg)
2035 {
2036     int     ret = -EINVAL;
2037 
2038     if (*debug & DEBUG_L2_TEI)
2039         printk(KERN_DEBUG "%s: cmd(%x) in %s\n",
2040                mISDNDevName4ch(&l2->ch), cmd, __func__);
2041     switch (cmd) {
2042     case (MDL_ASSIGN_REQ):
2043         ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ASSIGN, (void *)arg);
2044         break;
2045     case (MDL_REMOVE_REQ):
2046         ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_REMOVE, NULL);
2047         break;
2048     case (MDL_ERROR_IND):
2049         ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
2050         break;
2051     case (MDL_ERROR_RSP):
2052         /* ETS 300-125 5.3.2.1 Test: TC13010 */
2053         printk(KERN_NOTICE "%s: MDL_ERROR|REQ (tei_l2)\n",
2054                mISDNDevName4ch(&l2->ch));
2055         ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
2056         break;
2057     }
2058     return ret;
2059 }
2060 
2061 static void
2062 release_l2(struct layer2 *l2)
2063 {
2064     mISDN_FsmDelTimer(&l2->t200, 21);
2065     mISDN_FsmDelTimer(&l2->t203, 16);
2066     skb_queue_purge(&l2->i_queue);
2067     skb_queue_purge(&l2->ui_queue);
2068     skb_queue_purge(&l2->down_queue);
2069     ReleaseWin(l2);
2070     if (test_bit(FLG_LAPD, &l2->flag)) {
2071         TEIrelease(l2);
2072         if (l2->ch.st)
2073             l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D,
2074                            CLOSE_CHANNEL, NULL);
2075     }
2076     kfree(l2);
2077 }
2078 
2079 static int
2080 l2_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
2081 {
2082     struct layer2       *l2 = container_of(ch, struct layer2, ch);
2083     u_int           info;
2084 
2085     if (*debug & DEBUG_L2_CTRL)
2086         printk(KERN_DEBUG "%s: %s cmd(%x)\n",
2087                mISDNDevName4ch(ch), __func__, cmd);
2088 
2089     switch (cmd) {
2090     case OPEN_CHANNEL:
2091         if (test_bit(FLG_LAPD, &l2->flag)) {
2092             set_channel_address(&l2->ch, l2->sapi, l2->tei);
2093             info = DL_INFO_L2_CONNECT;
2094             l2up_create(l2, DL_INFORMATION_IND,
2095                     sizeof(info), &info);
2096         }
2097         break;
2098     case CLOSE_CHANNEL:
2099         if (l2->ch.peer)
2100             l2->ch.peer->ctrl(l2->ch.peer, CLOSE_CHANNEL, NULL);
2101         release_l2(l2);
2102         break;
2103     }
2104     return 0;
2105 }
2106 
2107 struct layer2 *
2108 create_l2(struct mISDNchannel *ch, u_int protocol, u_long options, int tei,
2109       int sapi)
2110 {
2111     struct layer2       *l2;
2112     struct channel_req  rq;
2113 
2114     l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
2115     if (!l2) {
2116         printk(KERN_ERR "kzalloc layer2 failed\n");
2117         return NULL;
2118     }
2119     l2->next_id = 1;
2120     l2->down_id = MISDN_ID_NONE;
2121     l2->up = ch;
2122     l2->ch.st = ch->st;
2123     l2->ch.send = l2_send;
2124     l2->ch.ctrl = l2_ctrl;
2125     switch (protocol) {
2126     case ISDN_P_LAPD_NT:
2127         test_and_set_bit(FLG_LAPD, &l2->flag);
2128         test_and_set_bit(FLG_LAPD_NET, &l2->flag);
2129         test_and_set_bit(FLG_MOD128, &l2->flag);
2130         l2->sapi = sapi;
2131         l2->maxlen = MAX_DFRAME_LEN;
2132         if (test_bit(OPTION_L2_PMX, &options))
2133             l2->window = 7;
2134         else
2135             l2->window = 1;
2136         if (test_bit(OPTION_L2_PTP, &options))
2137             test_and_set_bit(FLG_PTP, &l2->flag);
2138         if (test_bit(OPTION_L2_FIXEDTEI, &options))
2139             test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2140         l2->tei = tei;
2141         l2->T200 = 1000;
2142         l2->N200 = 3;
2143         l2->T203 = 10000;
2144         if (test_bit(OPTION_L2_PMX, &options))
2145             rq.protocol = ISDN_P_NT_E1;
2146         else
2147             rq.protocol = ISDN_P_NT_S0;
2148         rq.adr.channel = 0;
2149         l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
2150         break;
2151     case ISDN_P_LAPD_TE:
2152         test_and_set_bit(FLG_LAPD, &l2->flag);
2153         test_and_set_bit(FLG_MOD128, &l2->flag);
2154         test_and_set_bit(FLG_ORIG, &l2->flag);
2155         l2->sapi = sapi;
2156         l2->maxlen = MAX_DFRAME_LEN;
2157         if (test_bit(OPTION_L2_PMX, &options))
2158             l2->window = 7;
2159         else
2160             l2->window = 1;
2161         if (test_bit(OPTION_L2_PTP, &options))
2162             test_and_set_bit(FLG_PTP, &l2->flag);
2163         if (test_bit(OPTION_L2_FIXEDTEI, &options))
2164             test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2165         l2->tei = tei;
2166         l2->T200 = 1000;
2167         l2->N200 = 3;
2168         l2->T203 = 10000;
2169         if (test_bit(OPTION_L2_PMX, &options))
2170             rq.protocol = ISDN_P_TE_E1;
2171         else
2172             rq.protocol = ISDN_P_TE_S0;
2173         rq.adr.channel = 0;
2174         l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
2175         break;
2176     case ISDN_P_B_X75SLP:
2177         test_and_set_bit(FLG_LAPB, &l2->flag);
2178         l2->window = 7;
2179         l2->maxlen = MAX_DATA_SIZE;
2180         l2->T200 = 1000;
2181         l2->N200 = 4;
2182         l2->T203 = 5000;
2183         l2->addr.A = 3;
2184         l2->addr.B = 1;
2185         break;
2186     default:
2187         printk(KERN_ERR "layer2 create failed prt %x\n",
2188                protocol);
2189         kfree(l2);
2190         return NULL;
2191     }
2192     skb_queue_head_init(&l2->i_queue);
2193     skb_queue_head_init(&l2->ui_queue);
2194     skb_queue_head_init(&l2->down_queue);
2195     skb_queue_head_init(&l2->tmp_queue);
2196     InitWin(l2);
2197     l2->l2m.fsm = &l2fsm;
2198     if (test_bit(FLG_LAPB, &l2->flag) ||
2199         test_bit(FLG_FIXED_TEI, &l2->flag) ||
2200         test_bit(FLG_LAPD_NET, &l2->flag))
2201         l2->l2m.state = ST_L2_4;
2202     else
2203         l2->l2m.state = ST_L2_1;
2204     l2->l2m.debug = *debug;
2205     l2->l2m.userdata = l2;
2206     l2->l2m.userint = 0;
2207     l2->l2m.printdebug = l2m_debug;
2208 
2209     mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
2210     mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
2211     return l2;
2212 }
2213 
2214 static int
2215 x75create(struct channel_req *crq)
2216 {
2217     struct layer2   *l2;
2218 
2219     if (crq->protocol != ISDN_P_B_X75SLP)
2220         return -EPROTONOSUPPORT;
2221     l2 = create_l2(crq->ch, crq->protocol, 0, 0, 0);
2222     if (!l2)
2223         return -ENOMEM;
2224     crq->ch = &l2->ch;
2225     crq->protocol = ISDN_P_B_HDLC;
2226     return 0;
2227 }
2228 
2229 static struct Bprotocol X75SLP = {
2230     .Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
2231     .name = "X75SLP",
2232     .create = x75create
2233 };
2234 
2235 int
2236 Isdnl2_Init(u_int *deb)
2237 {
2238     int res;
2239     debug = deb;
2240     mISDN_register_Bprotocol(&X75SLP);
2241     l2fsm.state_count = L2_STATE_COUNT;
2242     l2fsm.event_count = L2_EVENT_COUNT;
2243     l2fsm.strEvent = strL2Event;
2244     l2fsm.strState = strL2State;
2245     res = mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
2246     if (res)
2247         goto error;
2248     res = TEIInit(deb);
2249     if (res)
2250         goto error_fsm;
2251     return 0;
2252 
2253 error_fsm:
2254     mISDN_FsmFree(&l2fsm);
2255 error:
2256     mISDN_unregister_Bprotocol(&X75SLP);
2257     return res;
2258 }
2259 
2260 void
2261 Isdnl2_cleanup(void)
2262 {
2263     mISDN_unregister_Bprotocol(&X75SLP);
2264     TEIFree();
2265     mISDN_FsmFree(&l2fsm);
2266 }