Blame view

drivers/isdn/hisax/isdnl2.c 41.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /* $Id: isdnl2.c,v 2.30.2.4 2004/02/11 13:21:34 keil Exp $
   *
   * Author       Karsten Keil
   *              based on the teles driver from Jan den Ouden
   * Copyright    by Karsten Keil      <keil@isdn4linux.de>
   * 
   * This software may be used and distributed according to the terms
   * of the GNU General Public License, incorporated herein by reference.
   *
   * For changes and modifications please read
   * Documentation/isdn/HiSax.cert
   *
   * Thanks to    Jan den Ouden
   *              Fritz Elfert
   *
   */
  
  #include <linux/init.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include "hisax.h"
  #include "isdnl2.h"
  
  const char *l2_revision = "$Revision: 2.30.2.4 $";
  
  static void l2m_debug(struct FsmInst *fi, char *fmt, ...);
  
  static struct Fsm l2fsm;
  
  enum {
  	ST_L2_1,
  	ST_L2_2,
  	ST_L2_3,
  	ST_L2_4,
  	ST_L2_5,
  	ST_L2_6,
  	ST_L2_7,
  	ST_L2_8,
  };
  
  #define L2_STATE_COUNT (ST_L2_8+1)
  
  static char *strL2State[] =
  {
  	"ST_L2_1",
  	"ST_L2_2",
  	"ST_L2_3",
  	"ST_L2_4",
  	"ST_L2_5",
  	"ST_L2_6",
  	"ST_L2_7",
  	"ST_L2_8",
  };
  
  enum {
  	EV_L2_UI,
  	EV_L2_SABME,
  	EV_L2_DISC,
  	EV_L2_DM,
  	EV_L2_UA,
  	EV_L2_FRMR,
  	EV_L2_SUPER,
  	EV_L2_I,
  	EV_L2_DL_DATA,
  	EV_L2_ACK_PULL,
  	EV_L2_DL_UNIT_DATA,
  	EV_L2_DL_ESTABLISH_REQ,
  	EV_L2_DL_RELEASE_REQ,
  	EV_L2_MDL_ASSIGN,
  	EV_L2_MDL_REMOVE,
  	EV_L2_MDL_ERROR,
  	EV_L1_DEACTIVATE,
  	EV_L2_T200,
  	EV_L2_T203,
  	EV_L2_SET_OWN_BUSY,
  	EV_L2_CLEAR_OWN_BUSY,
  	EV_L2_FRAME_ERROR,
  };
  
  #define L2_EVENT_COUNT (EV_L2_FRAME_ERROR+1)
  
  static char *strL2Event[] =
  {
  	"EV_L2_UI",
  	"EV_L2_SABME",
  	"EV_L2_DISC",
  	"EV_L2_DM",
  	"EV_L2_UA",
  	"EV_L2_FRMR",
  	"EV_L2_SUPER",
  	"EV_L2_I",
  	"EV_L2_DL_DATA",
  	"EV_L2_ACK_PULL",
  	"EV_L2_DL_UNIT_DATA",
  	"EV_L2_DL_ESTABLISH_REQ",
  	"EV_L2_DL_RELEASE_REQ",
  	"EV_L2_MDL_ASSIGN",
  	"EV_L2_MDL_REMOVE",
  	"EV_L2_MDL_ERROR",
  	"EV_L1_DEACTIVATE",
  	"EV_L2_T200",
  	"EV_L2_T203",
  	"EV_L2_SET_OWN_BUSY",
  	"EV_L2_CLEAR_OWN_BUSY",
  	"EV_L2_FRAME_ERROR",
  };
  
  static int l2addrsize(struct Layer2 *l2);
  
  static void
  set_peer_busy(struct Layer2 *l2) {
  	test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
b03efcfb2   David S. Miller   [NET]: Transform ...
112
113
  	if (!skb_queue_empty(&l2->i_queue) ||
  	    !skb_queue_empty(&l2->ui_queue))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  		test_and_set_bit(FLG_L2BLOCK, &l2->flag);
  }
  
  static void
  clear_peer_busy(struct Layer2 *l2) {
  	if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
  		test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
  }
  
  static void
  InitWin(struct Layer2 *l2)
  {
  	int i;
  
  	for (i = 0; i < MAX_WINDOW; i++)
  		l2->windowar[i] = NULL;
  }
  
  static int
  freewin1(struct Layer2 *l2)
  {
  	int i, cnt = 0;
  
  	for (i = 0; i < MAX_WINDOW; i++) {
  		if (l2->windowar[i]) {
  			cnt++;
  			dev_kfree_skb(l2->windowar[i]);
  			l2->windowar[i] = NULL;
  		}
  	}
  	return cnt;
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
146
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  freewin(struct PStack *st)
  {
  	freewin1(&st->l2);
  }
  
  static void
  ReleaseWin(struct Layer2 *l2)
  {
  	int cnt;
  
  	if((cnt = freewin1(l2)))
  		printk(KERN_WARNING "isdl2 freed %d skbuffs in release
  ", cnt);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
161
  static inline unsigned int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
163
164
165
166
167
168
169
170
171
  cansend(struct PStack *st)
  {
  	unsigned int p1;
  
  	if(test_bit(FLG_MOD128, &st->l2.flag))
  		p1 = (st->l2.vs - st->l2.va) % 128;
  	else
  		p1 = (st->l2.vs - st->l2.va) % 8;
  	return ((p1 < st->l2.window) && !test_bit(FLG_PEER_BUSY, &st->l2.flag));
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
172
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
176
177
178
179
  clear_exception(struct Layer2 *l2)
  {
  	test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  	test_and_clear_bit(FLG_REJEXC, &l2->flag);
  	test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
  	clear_peer_busy(l2);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
180
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  l2headersize(struct Layer2 *l2, int ui)
  {
  	return (((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
  		(test_bit(FLG_LAPD, &l2->flag) ? 2 : 1));
  }
  
  inline int
  l2addrsize(struct Layer2 *l2)
  {
  	return (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
  }
  
  static int
  sethdraddr(struct Layer2 *l2, u_char * header, int rsp)
  {
  	u_char *ptr = header;
  	int crbit = rsp;
  
  	if (test_bit(FLG_LAPD, &l2->flag)) {
  		*ptr++ = (l2->sap << 2) | (rsp ? 2 : 0);
  		*ptr++ = (l2->tei << 1) | 1;
  		return (2);
  	} else {
  		if (test_bit(FLG_ORIG, &l2->flag))
  			crbit = !crbit;
  		if (crbit)
  			*ptr++ = 1;
  		else
  			*ptr++ = 3;
  		return (1);
  	}
  }
77933d727   Jesper Juhl   [PATCH] clean up ...
213
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
216
217
218
219
220
221
222
  enqueue_super(struct PStack *st,
  	      struct sk_buff *skb)
  {
  	if (test_bit(FLG_LAPB, &st->l2.flag))
  		st->l1.bcs->tx_cnt += skb->len;
  	st->l2.l2l1(st, PH_DATA | REQUEST, skb);
  }
  
  #define enqueue_ui(a, b) enqueue_super(a, b)
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
223
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
225
226
227
  IsUI(u_char * data)
  {
  	return ((data[0] & 0xef) == UI);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
228
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
  IsUA(u_char * data)
  {
  	return ((data[0] & 0xef) == UA);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
233
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
236
237
  IsDM(u_char * data)
  {
  	return ((data[0] & 0xef) == DM);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
238
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
240
241
242
  IsDISC(u_char * data)
  {
  	return ((data[0] & 0xef) == DISC);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
243
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
249
250
251
  IsSFrame(u_char * data, struct PStack *st)
  {
  	register u_char d = *data;
  	
  	if (!test_bit(FLG_MOD128, &st->l2.flag))
  		d &= 0xf;
  	return(((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c));
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
252
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
258
  IsSABME(u_char * data, struct PStack *st)
  {
  	u_char d = data[0] & ~0x10;
  
  	return (test_bit(FLG_MOD128, &st->l2.flag) ? d == SABME : d == SABM);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
259
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
261
262
263
  IsREJ(u_char * data, struct PStack *st)
  {
  	return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == REJ : (data[0] & 0xf) == REJ);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
264
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
  IsFRMR(u_char * data)
  {
  	return ((data[0] & 0xef) == FRMR);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
269
  static inline int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
271
272
273
  IsRNR(u_char * data, struct PStack *st)
  {
  	return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == RNR : (data[0] & 0xf) == RNR);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
274
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  iframe_error(struct PStack *st, struct sk_buff *skb)
  {
  	int i = l2addrsize(&st->l2) + (test_bit(FLG_MOD128, &st->l2.flag) ? 2 : 1);
  	int rsp = *skb->data & 0x2;
  
  	if (test_bit(FLG_ORIG, &st->l2.flag))
  		rsp = !rsp;
  
  	if (rsp)
  		return 'L';
  
  
  	if (skb->len < i)
  		return 'N';
  
  	if ((skb->len - i) > st->l2.maxlen)
  		return 'O';
  
  
  	return 0;
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
296
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
298
299
300
301
302
303
304
  super_error(struct PStack *st, struct sk_buff *skb)
  {
  	if (skb->len != l2addrsize(&st->l2) +
  	    (test_bit(FLG_MOD128, &st->l2.flag) ? 2 : 1))
  		return 'N';
  
  	return 0;
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
305
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  unnum_error(struct PStack *st, struct sk_buff *skb, int wantrsp)
  {
  	int rsp = (*skb->data & 0x2) >> 1;
  	if (test_bit(FLG_ORIG, &st->l2.flag))
  		rsp = !rsp;
  
  	if (rsp != wantrsp)
  		return 'L';
  
  	if (skb->len != l2addrsize(&st->l2) + 1)
  		return 'N';
  
  	return 0;
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
320
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  UI_error(struct PStack *st, struct sk_buff *skb)
  {
  	int rsp = *skb->data & 0x2;
  	if (test_bit(FLG_ORIG, &st->l2.flag))
  		rsp = !rsp;
  
  	if (rsp)
  		return 'L';
  
  	if (skb->len > st->l2.maxlen + l2addrsize(&st->l2) + 1)
  		return 'O';
  
  	return 0;
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
335
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
  FRMR_error(struct PStack *st, struct sk_buff *skb)
  {
  	int headers = l2addrsize(&st->l2) + 1;
  	u_char *datap = skb->data + headers;
  	int rsp = *skb->data & 0x2;
  
  	if (test_bit(FLG_ORIG, &st->l2.flag))
  		rsp = !rsp;
  
  	if (!rsp)
  		return 'L';
  
  	if (test_bit(FLG_MOD128, &st->l2.flag)) {
  		if (skb->len < headers + 5)
  			return 'N';
  		else
  			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x %2x %2x",
  				datap[0], datap[1], datap[2],
  				datap[3], datap[4]);
  	} else {
  		if (skb->len < headers + 3)
  			return 'N';
  		else
  			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x",
  				datap[0], datap[1], datap[2]);
  	}
  
  	return 0;
  }
  
  static unsigned int
  legalnr(struct PStack *st, unsigned int nr)
  {
          struct Layer2 *l2 = &st->l2;
  
  	if(test_bit(FLG_MOD128, &l2->flag))
  		return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
  	else
  		return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
  }
  
  static void
  setva(struct PStack *st, unsigned int nr)
  {
  	struct Layer2 *l2 = &st->l2;
  	int len;
  	u_long flags;
  
  	spin_lock_irqsave(&l2->lock, flags);
  	while (l2->va != nr) {
  		(l2->va)++;
  		if(test_bit(FLG_MOD128, &l2->flag))
  			l2->va %= 128;
  		else
  			l2->va %= 8;
  		len = l2->windowar[l2->sow]->len;
  		if (PACKET_NOACK == l2->windowar[l2->sow]->pkt_type)
  			len = -1;
  		dev_kfree_skb(l2->windowar[l2->sow]);
  		l2->windowar[l2->sow] = NULL;
  		l2->sow = (l2->sow + 1) % l2->window;
  		spin_unlock_irqrestore(&l2->lock, flags);
  		if (test_bit(FLG_LLI_L2WAKEUP, &st->lli.flag) && (len >=0))
  			lli_writewakeup(st, len);
  		spin_lock_irqsave(&l2->lock, flags);
  	}
  	spin_unlock_irqrestore(&l2->lock, flags);
  }
  
  static void
  send_uframe(struct PStack *st, u_char cmd, u_char cr)
  {
  	struct sk_buff *skb;
  	u_char tmp[MAX_HEADER_LEN];
  	int i;
  
  	i = sethdraddr(&st->l2, tmp, cr);
  	tmp[i++] = cmd;
  	if (!(skb = alloc_skb(i, GFP_ATOMIC))) {
  		printk(KERN_WARNING "isdl2 can't alloc sbbuff for send_uframe
  ");
  		return;
  	}
  	memcpy(skb_put(skb, i), tmp, i);
  	enqueue_super(st, skb);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
422
  static inline u_char
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
424
425
426
  get_PollFlag(struct PStack * st, struct sk_buff * skb)
  {
  	return (skb->data[l2addrsize(&(st->l2))] & 0x10);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
427
  static inline u_char
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
429
430
431
432
  get_PollFlagFree(struct PStack *st, struct sk_buff *skb)
  {
  	u_char PF;
  
  	PF = get_PollFlag(st, skb);
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
433
  	dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
435
  	return (PF);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
436
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
438
439
440
441
  start_t200(struct PStack *st, int i)
  {
  	FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
  	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
442
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
446
447
  restart_t200(struct PStack *st, int i)
  {
  	FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
  	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
448
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
450
451
452
453
  stop_t200(struct PStack *st, int i)
  {
  	if(test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
  		FsmDelTimer(&st->l2.t200, i);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
454
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
456
457
458
459
460
461
462
463
464
465
  st5_dl_release_l2l3(struct PStack *st)
  {
  		int pr;
  
  		if(test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
  			pr = DL_RELEASE | CONFIRM;
  		else
  			pr = DL_RELEASE | INDICATION;
  
  		st->l2.l2l3(st, pr, NULL);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
466
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
  lapb_dl_release_l2l3(struct PStack *st, int f)
  {
  		if (test_bit(FLG_LAPB, &st->l2.flag))
  			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
  		st->l2.l2l3(st, DL_RELEASE | f, NULL);
  }
  
  static void
  establishlink(struct FsmInst *fi)
  {
  	struct PStack *st = fi->userdata;
  	u_char cmd;
  
  	clear_exception(&st->l2);
  	st->l2.rc = 0;
  	cmd = (test_bit(FLG_MOD128, &st->l2.flag) ? SABME : SABM) | 0x10;
  	send_uframe(st, cmd, CMD);
  	FsmDelTimer(&st->l2.t203, 1);
  	restart_t200(st, 1);
  	test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
  	freewin(st);
  	FsmChangeState(fi, ST_L2_5);
  }
  
  static void
  l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
  {
  	struct sk_buff *skb = arg;
  	struct PStack *st = fi->userdata;
  
  	if (get_PollFlagFree(st, skb))
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'C');
  	else
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'D');
  }
  
  static void
  l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
  {
  	struct sk_buff *skb = arg;
  	struct PStack *st = fi->userdata;
  
  	if (get_PollFlagFree(st, skb))
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
  	else {
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
  		establishlink(fi);
  		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
  	}
  }
  
  static void
  l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
  {
  	struct sk_buff *skb = arg;
  	struct PStack *st = fi->userdata;
  
  	if (get_PollFlagFree(st, skb))
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
  	else {
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
  	}
  	establishlink(fi);
  	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
  }
  
  static void
  l2_go_st3(struct FsmInst *fi, int event, void *arg)
  {
  	FsmChangeState(fi, ST_L2_3); 
  }
  
  static void
  l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	FsmChangeState(fi, ST_L2_3); 
  	st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
  }
  
  static void
  l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	skb_queue_tail(&st->l2.ui_queue, skb);
  	FsmChangeState(fi, ST_L2_2);
  	st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
  }
  
  static void
  l2_queue_ui(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	skb_queue_tail(&st->l2.ui_queue, skb);
  }
  
  static void
  tx_ui(struct PStack *st)
  {
  	struct sk_buff *skb;
  	u_char header[MAX_HEADER_LEN];
  	int i;
  
  	i = sethdraddr(&(st->l2), header, CMD);
  	header[i++] = UI;
  	while ((skb = skb_dequeue(&st->l2.ui_queue))) {
  		memcpy(skb_push(skb, i), header, i);
  		enqueue_ui(st, skb);
  	}
  }
  
  static void
  l2_send_ui(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	skb_queue_tail(&st->l2.ui_queue, skb);
  	tx_ui(st);
  }
  
  static void
  l2_got_ui(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	skb_pull(skb, l2headersize(&st->l2, 1));
  	st->l2.l2l3(st, DL_UNIT_DATA | INDICATION, skb);
  /*	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   *		in states 1-3 for broadcast
   */
  
  
  }
  
  static void
  l2_establish(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	establishlink(fi);
  	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
  }
  
  static void
  l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.i_queue);
  	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
  	test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
  }
  
  static void
  l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.i_queue);
  	establishlink(fi);
  	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
  }
  
  static void
  l2_release(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
  }
  
  static void
  l2_pend_rel(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	test_and_set_bit(FLG_PEND_REL, &st->l2.flag);
  }
  
  static void
  l2_disconnect(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.i_queue);
  	freewin(st);
  	FsmChangeState(fi, ST_L2_6);
  	st->l2.rc = 0;
  	send_uframe(st, DISC | 0x10, CMD);
  	FsmDelTimer(&st->l2.t203, 1);
  	restart_t200(st, 2);
  }
  
  static void
  l2_start_multi(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
  
  	clear_exception(&st->l2);
  	st->l2.vs = 0;
  	st->l2.va = 0;
  	st->l2.vr = 0;
  	st->l2.sow = 0;
  	FsmChangeState(fi, ST_L2_7);
  	FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
  
  	st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
  }
  
  static void
  l2_send_UA(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
  }
  
  static void
  l2_send_DM(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	send_uframe(st, DM | get_PollFlagFree(st, skb), RSP);
  }
  
  static void
  l2_restart_multi(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  	int est = 0, state;
  
  	state = fi->state;
  
  	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
  
  	st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'F');
  
  	if (st->l2.vs != st->l2.va) {
  		skb_queue_purge(&st->l2.i_queue);
  		est = 1;
  	}
  
  	clear_exception(&st->l2);
  	st->l2.vs = 0;
  	st->l2.va = 0;
  	st->l2.vr = 0;
  	st->l2.sow = 0;
  	FsmChangeState(fi, ST_L2_7);
  	stop_t200(st, 3);
  	FsmRestartTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
  
  	if (est)
  		st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
  
  	if ((ST_L2_7==state) || (ST_L2_8 == state))
b03efcfb2   David S. Miller   [NET]: Transform ...
735
  		if (!skb_queue_empty(&st->l2.i_queue) && cansend(st))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
  			st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  }
  
  static void
  l2_stop_multi(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	FsmChangeState(fi, ST_L2_4);
  	FsmDelTimer(&st->l2.t203, 3);
  	stop_t200(st, 4);
  
  	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
  
  	skb_queue_purge(&st->l2.i_queue);
  	freewin(st);
  	lapb_dl_release_l2l3(st, INDICATION);
  }
  
  static void
  l2_connected(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  	int pr=-1;
  
  	if (!get_PollFlag(st, skb)) {
  		l2_mdl_error_ua(fi, event, arg);
  		return;
  	}
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
767
  	dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
  
  	if (test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
  		l2_disconnect(fi, event, arg);
  
  	if (test_and_clear_bit(FLG_L3_INIT, &st->l2.flag)) {
  		pr = DL_ESTABLISH | CONFIRM;
  	} else if (st->l2.vs != st->l2.va) {
  		skb_queue_purge(&st->l2.i_queue);
  		pr = DL_ESTABLISH | INDICATION;
  	}
  
  	stop_t200(st, 5);
  
  	st->l2.vr = 0;
  	st->l2.vs = 0;
  	st->l2.va = 0;
  	st->l2.sow = 0;
  	FsmChangeState(fi, ST_L2_7);
  	FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 4);
  
  	if (pr != -1)
  		st->l2.l2l3(st, pr, NULL);
b03efcfb2   David S. Miller   [NET]: Transform ...
790
  	if (!skb_queue_empty(&st->l2.i_queue) && cansend(st))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
792
793
794
795
796
797
798
799
800
801
802
803
  		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  }
  
  static void
  l2_released(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	if (!get_PollFlag(st, skb)) {
  		l2_mdl_error_ua(fi, event, arg);
  		return;
  	}
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
804
  	dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
  
  	stop_t200(st, 6);
  	lapb_dl_release_l2l3(st, CONFIRM);
  	FsmChangeState(fi, ST_L2_4);
  }
  
  static void
  l2_reestablish(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	if (!get_PollFlagFree(st, skb)) {
  		establishlink(fi);
  		test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
  	}
  }
  
  static void
  l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	if (get_PollFlagFree(st, skb)) {
  		stop_t200(st, 7);
  	 	if (!test_bit(FLG_L3_INIT, &st->l2.flag))
  			skb_queue_purge(&st->l2.i_queue);
  		if (test_bit(FLG_LAPB, &st->l2.flag))
  			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
  		st5_dl_release_l2l3(st);
  		FsmChangeState(fi, ST_L2_4);
  	}
  }
  
  static void
  l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	if (get_PollFlagFree(st, skb)) {
  		stop_t200(st, 8);
  		lapb_dl_release_l2l3(st, CONFIRM);
  		FsmChangeState(fi, ST_L2_4);
  	}
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
852
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
  enquiry_cr(struct PStack *st, u_char typ, u_char cr, u_char pf)
  {
  	struct sk_buff *skb;
  	struct Layer2 *l2;
  	u_char tmp[MAX_HEADER_LEN];
  	int i;
  
  	l2 = &st->l2;
  	i = sethdraddr(l2, tmp, cr);
  	if (test_bit(FLG_MOD128, &l2->flag)) {
  		tmp[i++] = typ;
  		tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
  	} else
  		tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
  	if (!(skb = alloc_skb(i, GFP_ATOMIC))) {
  		printk(KERN_WARNING "isdl2 can't alloc sbbuff for enquiry_cr
  ");
  		return;
  	}
  	memcpy(skb_put(skb, i), tmp, i);
  	enqueue_super(st, skb);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
875
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
876
877
878
879
880
881
882
883
  enquiry_response(struct PStack *st)
  {
  	if (test_bit(FLG_OWN_BUSY, &st->l2.flag))
  		enquiry_cr(st, RNR, RSP, 1);
  	else
  		enquiry_cr(st, RR, RSP, 1);
  	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
  }
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
884
  static inline void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
  transmit_enquiry(struct PStack *st)
  {
  	if (test_bit(FLG_OWN_BUSY, &st->l2.flag))
  		enquiry_cr(st, RNR, CMD, 1);
  	else
  		enquiry_cr(st, RR, CMD, 1);
  	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
  	start_t200(st, 9);
  }
  
  
  static void
  nrerrorrecovery(struct FsmInst *fi)
  {
  	struct PStack *st = fi->userdata;
  
  	st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'J');
  	establishlink(fi);
  	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
  }
  
  static void
  invoke_retransmission(struct PStack *st, unsigned int nr)
  {
  	struct Layer2 *l2 = &st->l2;
  	u_int p1;
  	u_long flags;
  
  	spin_lock_irqsave(&l2->lock, flags);
  	if (l2->vs != nr) {
  		while (l2->vs != nr) {
  			(l2->vs)--;
  			if(test_bit(FLG_MOD128, &l2->flag)) {
  				l2->vs %= 128;
  				p1 = (l2->vs - l2->va) % 128;
  			} else {
  				l2->vs %= 8;
  				p1 = (l2->vs - l2->va) % 8;
  			}
  			p1 = (p1 + l2->sow) % l2->window;
  			if (test_bit(FLG_LAPB, &l2->flag))
  				st->l1.bcs->tx_cnt += l2->windowar[p1]->len + l2headersize(l2, 0);
  			skb_queue_head(&l2->i_queue, l2->windowar[p1]);
  			l2->windowar[p1] = NULL;
  		}
  		spin_unlock_irqrestore(&l2->lock, flags);
  		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  		return;
  	}
  	spin_unlock_irqrestore(&l2->lock, flags);
  }
  
  static void
  l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  	int PollFlag, rsp, typ = RR;
  	unsigned int nr;
  	struct Layer2 *l2 = &st->l2;
  
  	rsp = *skb->data & 0x2;
  	if (test_bit(FLG_ORIG, &l2->flag))
  		rsp = !rsp;
  
  	skb_pull(skb, l2addrsize(l2));
  	if (IsRNR(skb->data, st)) {
  		set_peer_busy(l2);
  		typ = RNR;
  	} else
  		clear_peer_busy(l2);
  	if (IsREJ(skb->data, st))
  		typ = REJ;
  
  	if (test_bit(FLG_MOD128, &l2->flag)) {
  		PollFlag = (skb->data[1] & 0x1) == 0x1;
  		nr = skb->data[1] >> 1;
  	} else {
  		PollFlag = (skb->data[0] & 0x10);
  		nr = (skb->data[0] >> 5) & 0x7;
  	}
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
966
  	dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
  
  	if (PollFlag) {
  		if (rsp)
  			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'A');
  		else
  			enquiry_response(st);
  	}
  	if (legalnr(st, nr)) {
  		if (typ == REJ) {
  			setva(st, nr);
  			invoke_retransmission(st, nr);
  			stop_t200(st, 10);
  			if (FsmAddTimer(&st->l2.t203, st->l2.T203,
  					EV_L2_T203, NULL, 6))
  				l2m_debug(&st->l2.l2m, "Restart T203 ST7 REJ");
  		} else if ((nr == l2->vs) && (typ == RR)) {
  			setva(st, nr);
  			stop_t200(st, 11);
  			FsmRestartTimer(&st->l2.t203, st->l2.T203,
  					EV_L2_T203, NULL, 7);
  		} else if ((l2->va != nr) || (typ == RNR)) {
  			setva(st, nr);
  			if(typ != RR) FsmDelTimer(&st->l2.t203, 9);
  			restart_t200(st, 12);
  		}
b03efcfb2   David S. Miller   [NET]: Transform ...
992
  		if (!skb_queue_empty(&st->l2.i_queue) && (typ == RR))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
  			st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  	} else
  		nrerrorrecovery(fi);
  }
  
  static void
  l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	if (test_bit(FLG_LAPB, &st->l2.flag))
  		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
  	if (!test_bit(FLG_L3_INIT, &st->l2.flag))
  		skb_queue_tail(&st->l2.i_queue, skb);
  	else
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1009
  		dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  }
  
  static void
  l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	if (test_bit(FLG_LAPB, &st->l2.flag))
  		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
  	skb_queue_tail(&st->l2.i_queue, skb);
  	st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  }
  
  static void
  l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	if (test_bit(FLG_LAPB, &st->l2.flag))
  		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
  	skb_queue_tail(&st->l2.i_queue, skb);
  }
  
  static void
  l2_got_iframe(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  	struct Layer2 *l2 = &(st->l2);
  	int PollFlag, ns, i;
  	unsigned int nr;
  
  	i = l2addrsize(l2);
  	if (test_bit(FLG_MOD128, &l2->flag)) {
  		PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
  		ns = skb->data[i] >> 1;
  		nr = (skb->data[i + 1] >> 1) & 0x7f;
  	} else {
  		PollFlag = (skb->data[i] & 0x10);
  		ns = (skb->data[i] >> 1) & 0x7;
  		nr = (skb->data[i] >> 5) & 0x7;
  	}
  	if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1055
  		dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
  		if(PollFlag) enquiry_response(st);
  	} else if (l2->vr == ns) {
  		(l2->vr)++;
  		if(test_bit(FLG_MOD128, &l2->flag))
  			l2->vr %= 128;
  		else
  			l2->vr %= 8;
  		test_and_clear_bit(FLG_REJEXC, &l2->flag);
  
  		if (PollFlag)
  			enquiry_response(st);
  		else
  			test_and_set_bit(FLG_ACK_PEND, &l2->flag);
  		skb_pull(skb, l2headersize(l2, 0));
  		st->l2.l2l3(st, DL_DATA | INDICATION, skb);
  	} else {
  		/* n(s)!=v(r) */
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1073
  		dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  		if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
  			if (PollFlag)
  				enquiry_response(st);
  		} else {
  			enquiry_cr(st, REJ, RSP, PollFlag);
  			test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  		}
  	}
  
  	if (legalnr(st, nr)) {
  		if (!test_bit(FLG_PEER_BUSY, &st->l2.flag) && (fi->state == ST_L2_7)) {
  			if (nr == st->l2.vs) {
  				stop_t200(st, 13);
  				FsmRestartTimer(&st->l2.t203, st->l2.T203,
  						EV_L2_T203, NULL, 7);
  			} else if (nr != st->l2.va)
  				restart_t200(st, 14);
  		}
  		setva(st, nr);
  	} else {
  		nrerrorrecovery(fi);
  		return;
  	}
b03efcfb2   David S. Miller   [NET]: Transform ...
1097
  	if (!skb_queue_empty(&st->l2.i_queue) && (fi->state == ST_L2_7))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
  		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  	if (test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag))
  		enquiry_cr(st, RR, RSP, 0);
  }
  
  static void
  l2_got_tei(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	st->l2.tei = (long) arg;
  
  	if (fi->state == ST_L2_3) {
  		establishlink(fi);
  		test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
  	} else
  		FsmChangeState(fi, ST_L2_4);
b03efcfb2   David S. Miller   [NET]: Transform ...
1115
  	if (!skb_queue_empty(&st->l2.ui_queue))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
  		tx_ui(st);
  }
  
  static void
  l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	if (test_bit(FLG_LAPD, &st->l2.flag) &&
  		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
  		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
  	} else if (st->l2.rc == st->l2.N200) {
  		FsmChangeState(fi, ST_L2_4);
  		test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
  		skb_queue_purge(&st->l2.i_queue);
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'G');
  		if (test_bit(FLG_LAPB, &st->l2.flag))
  			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
  		st5_dl_release_l2l3(st);
  	} else {
  		st->l2.rc++;
  		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
  		send_uframe(st, (test_bit(FLG_MOD128, &st->l2.flag) ? SABME : SABM)
  			    | 0x10, CMD);
  	}
  }
  
  static void
  l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	if (test_bit(FLG_LAPD, &st->l2.flag) &&
  		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
  		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
  	} else if (st->l2.rc == st->l2.N200) {
  		FsmChangeState(fi, ST_L2_4);
  		test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'H');
  		lapb_dl_release_l2l3(st, CONFIRM);
  	} else {
  		st->l2.rc++;
  		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200,
  			    NULL, 9);
  		send_uframe(st, DISC | 0x10, CMD);
  	}
  }
  
  static void
  l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	if (test_bit(FLG_LAPD, &st->l2.flag) &&
  		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
  		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
  		return;
  	}
  	test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
  	st->l2.rc = 0;
  	FsmChangeState(fi, ST_L2_8);
  
  	transmit_enquiry(st);
  	st->l2.rc++;
  }
  
  static void
  l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	if (test_bit(FLG_LAPD, &st->l2.flag) &&
  		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
  		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
  		return;
  	}
  	test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
  	if (st->l2.rc == st->l2.N200) {
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'I');
  		establishlink(fi);
  		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
  	} else {
  		transmit_enquiry(st);
  		st->l2.rc++;
  	}
  }
  
  static void
  l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	if (test_bit(FLG_LAPD, &st->l2.flag) &&
  		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
  		FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 9);
  		return;
  	}
  	FsmChangeState(fi, ST_L2_8);
  	transmit_enquiry(st);
  	st->l2.rc = 0;
  }
  
  static void
  l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
c978e7bb7   David S. Miller   hisax: Fix unchec...
1222
  	struct sk_buff *skb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1223
1224
  	struct Layer2 *l2 = &st->l2;
  	u_char header[MAX_HEADER_LEN];
c978e7bb7   David S. Miller   hisax: Fix unchec...
1225
  	int i, hdr_space_needed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1226
1227
1228
1229
1230
1231
1232
1233
1234
  	int unsigned p1;
  	u_long flags;
  
  	if (!cansend(st))
  		return;
  
  	skb = skb_dequeue(&l2->i_queue);
  	if (!skb)
  		return;
6d90e8f45   David S. Miller   isdn: hisax: Use ...
1235
  	hdr_space_needed = l2headersize(l2, 0);
c978e7bb7   David S. Miller   hisax: Fix unchec...
1236
1237
1238
1239
1240
1241
1242
1243
1244
  	if (hdr_space_needed > skb_headroom(skb)) {
  		struct sk_buff *orig_skb = skb;
  
  		skb = skb_realloc_headroom(skb, hdr_space_needed);
  		if (!skb) {
  			dev_kfree_skb(orig_skb);
  			return;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
  	spin_lock_irqsave(&l2->lock, flags);
  	if(test_bit(FLG_MOD128, &l2->flag))
  		p1 = (l2->vs - l2->va) % 128;
  	else
  		p1 = (l2->vs - l2->va) % 8;
  	p1 = (p1 + l2->sow) % l2->window;
  	if (l2->windowar[p1]) {
  		printk(KERN_WARNING "isdnl2 try overwrite ack queue entry %d
  ",
  		       p1);
  		dev_kfree_skb(l2->windowar[p1]);
  	}
  	l2->windowar[p1] = skb_clone(skb, GFP_ATOMIC);
  
  	i = sethdraddr(&st->l2, header, CMD);
  
  	if (test_bit(FLG_MOD128, &l2->flag)) {
  		header[i++] = l2->vs << 1;
  		header[i++] = l2->vr << 1;
  		l2->vs = (l2->vs + 1) % 128;
  	} else {
  		header[i++] = (l2->vr << 5) | (l2->vs << 1);
  		l2->vs = (l2->vs + 1) % 8;
  	}
  	spin_unlock_irqrestore(&l2->lock, flags);
c978e7bb7   David S. Miller   hisax: Fix unchec...
1270
  	memcpy(skb_push(skb, i), header, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1271
1272
1273
1274
1275
1276
  	st->l2.l2l1(st, PH_PULL | INDICATION, skb);
  	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
  	if (!test_and_set_bit(FLG_T200_RUN, &st->l2.flag)) {
  		FsmDelTimer(&st->l2.t203, 13);
  		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 11);
  	}
b03efcfb2   David S. Miller   [NET]: Transform ...
1277
  	if (!skb_queue_empty(&l2->i_queue) && cansend(st))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
  		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  }
  
  static void
  l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  	int PollFlag, rsp, rnr = 0;
  	unsigned int nr;
  	struct Layer2 *l2 = &st->l2;
  
  	rsp = *skb->data & 0x2;
  	if (test_bit(FLG_ORIG, &l2->flag))
  		rsp = !rsp;
  
  	skb_pull(skb, l2addrsize(l2));
  
  	if (IsRNR(skb->data, st)) {
  		set_peer_busy(l2);
  		rnr = 1;
  	} else
  		clear_peer_busy(l2);
  
  	if (test_bit(FLG_MOD128, &l2->flag)) {
  		PollFlag = (skb->data[1] & 0x1) == 0x1;
  		nr = skb->data[1] >> 1;
  	} else {
  		PollFlag = (skb->data[0] & 0x10);
  		nr = (skb->data[0] >> 5) & 0x7;
  	}
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1309
  	dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
  
  	if (rsp && PollFlag) {
  		if (legalnr(st, nr)) {
  			if (rnr) {
  				restart_t200(st, 15);
  			} else {
  				stop_t200(st, 16);
  				FsmAddTimer(&l2->t203, l2->T203,
  					    EV_L2_T203, NULL, 5);
  				setva(st, nr);
  			}
  			invoke_retransmission(st, nr);
  			FsmChangeState(fi, ST_L2_7);
b03efcfb2   David S. Miller   [NET]: Transform ...
1323
  			if (!skb_queue_empty(&l2->i_queue) && cansend(st))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
  				st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
  		} else
  			nrerrorrecovery(fi);
  	} else {
  		if (!rsp && PollFlag)
  			enquiry_response(st);
  		if (legalnr(st, nr)) {
  			setva(st, nr);
  		} else
  			nrerrorrecovery(fi);
  	}
  }
  
  static void
  l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  	struct sk_buff *skb = arg;
  
  	skb_pull(skb, l2addrsize(&st->l2) + 1);
  
  	if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) ||		/* I or S */
  	    (IsUA(skb->data) && (fi->state == ST_L2_7))) {
  		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'K');
  		establishlink(fi);
  		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
  	}
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1351
  	dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
  }
  
  static void
  l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.ui_queue);
  	st->l2.tei = -1;
  	FsmChangeState(fi, ST_L2_1);
  }
  
  static void
  l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.ui_queue);
  	st->l2.tei = -1;
  	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
  	FsmChangeState(fi, ST_L2_1);
  }
  
  static void
  l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.i_queue);
  	skb_queue_purge(&st->l2.ui_queue);
  	freewin(st);
  	st->l2.tei = -1;
  	stop_t200(st, 17);
  	st5_dl_release_l2l3(st);
  	FsmChangeState(fi, ST_L2_1);
  }
  
  static void
  l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.ui_queue);
  	st->l2.tei = -1;
  	stop_t200(st, 18);
  	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
  	FsmChangeState(fi, ST_L2_1);
  }
  
  static void
  l2_tei_remove(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.i_queue);
  	skb_queue_purge(&st->l2.ui_queue);
  	freewin(st);
  	st->l2.tei = -1;
  	stop_t200(st, 17);
  	FsmDelTimer(&st->l2.t203, 19);
  	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
  	FsmChangeState(fi, ST_L2_1);
  }
  
  static void
6b44d4e69   Jan Engelhardt   Fix typos in driv...
1417
  l2_st14_persistent_da(struct FsmInst *fi, int event, void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
  {
  	struct PStack *st = fi->userdata;
  	
  	skb_queue_purge(&st->l2.i_queue);
  	skb_queue_purge(&st->l2.ui_queue);
  	if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
  		st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
  }
  
  static void
6b44d4e69   Jan Engelhardt   Fix typos in driv...
1428
  l2_st5_persistent_da(struct FsmInst *fi, int event, void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.i_queue);
  	skb_queue_purge(&st->l2.ui_queue);
  	freewin(st);
  	stop_t200(st, 19);
  	st5_dl_release_l2l3(st);
  	FsmChangeState(fi, ST_L2_4);
  }
  
  static void
6b44d4e69   Jan Engelhardt   Fix typos in driv...
1441
  l2_st6_persistent_da(struct FsmInst *fi, int event, void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.ui_queue);
  	stop_t200(st, 20);
  	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
  	FsmChangeState(fi, ST_L2_4);
  }
  
  static void
6b44d4e69   Jan Engelhardt   Fix typos in driv...
1452
  l2_persistent_da(struct FsmInst *fi, int event, void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
  {
  	struct PStack *st = fi->userdata;
  
  	skb_queue_purge(&st->l2.i_queue);
  	skb_queue_purge(&st->l2.ui_queue);
  	freewin(st);
  	stop_t200(st, 19);
  	FsmDelTimer(&st->l2.t203, 19);
  	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
  	FsmChangeState(fi, ST_L2_4);
  }
  
  static void
  l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	if(!test_and_set_bit(FLG_OWN_BUSY, &st->l2.flag)) {
  		enquiry_cr(st, RNR, RSP, 0);
  		test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
  	}
  }
  
  static void
  l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	if(!test_and_clear_bit(FLG_OWN_BUSY, &st->l2.flag)) {
  		enquiry_cr(st, RR, RSP, 0);
  		test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
  	}
  }
  
  static void
  l2_frame_error(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	st->ma.layer(st, MDL_ERROR | INDICATION, arg);
  }
  
  static void
  l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
  {
  	struct PStack *st = fi->userdata;
  
  	st->ma.layer(st, MDL_ERROR | INDICATION, arg);
  	establishlink(fi);
  	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
  }
  
  static struct FsmNode L2FnList[] __initdata =
  {
  	{ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
  	{ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
  	{ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
  	{ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
  	{ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
  	{ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
  	{ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
  	{ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
  	{ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
  	{ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
  	{ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
  	{ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
  	{ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
  	{ST_L2_1, EV_L2_DL_UNIT_DATA, l2_queue_ui_assign},
  	{ST_L2_2, EV_L2_DL_UNIT_DATA, l2_queue_ui},
  	{ST_L2_3, EV_L2_DL_UNIT_DATA, l2_queue_ui},
  	{ST_L2_4, EV_L2_DL_UNIT_DATA, l2_send_ui},
  	{ST_L2_5, EV_L2_DL_UNIT_DATA, l2_send_ui},
  	{ST_L2_6, EV_L2_DL_UNIT_DATA, l2_send_ui},
  	{ST_L2_7, EV_L2_DL_UNIT_DATA, l2_send_ui},
  	{ST_L2_8, EV_L2_DL_UNIT_DATA, l2_send_ui},
  	{ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
  	{ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
  	{ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
  	{ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
  	{ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
  	{ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
  	{ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
  	{ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
  	{ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
  	{ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
  	{ST_L2_4, EV_L2_SABME, l2_start_multi},
  	{ST_L2_5, EV_L2_SABME, l2_send_UA},
  	{ST_L2_6, EV_L2_SABME, l2_send_DM},
  	{ST_L2_7, EV_L2_SABME, l2_restart_multi},
  	{ST_L2_8, EV_L2_SABME, l2_restart_multi},
  	{ST_L2_4, EV_L2_DISC, l2_send_DM},
  	{ST_L2_5, EV_L2_DISC, l2_send_DM},
  	{ST_L2_6, EV_L2_DISC, l2_send_UA},
  	{ST_L2_7, EV_L2_DISC, l2_stop_multi},
  	{ST_L2_8, EV_L2_DISC, l2_stop_multi},
  	{ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
  	{ST_L2_5, EV_L2_UA, l2_connected},
  	{ST_L2_6, EV_L2_UA, l2_released},
  	{ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
  	{ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
  	{ST_L2_4, EV_L2_DM, l2_reestablish},
  	{ST_L2_5, EV_L2_DM, l2_st5_dm_release},
  	{ST_L2_6, EV_L2_DM, l2_st6_dm_release},
  	{ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
  	{ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
  	{ST_L2_1, EV_L2_UI, l2_got_ui},
  	{ST_L2_2, EV_L2_UI, l2_got_ui},
  	{ST_L2_3, EV_L2_UI, l2_got_ui},
  	{ST_L2_4, EV_L2_UI, l2_got_ui},
  	{ST_L2_5, EV_L2_UI, l2_got_ui},
  	{ST_L2_6, EV_L2_UI, l2_got_ui},
  	{ST_L2_7, EV_L2_UI, l2_got_ui},
  	{ST_L2_8, EV_L2_UI, l2_got_ui},
  	{ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
  	{ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
  	{ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
  	{ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
  	{ST_L2_7, EV_L2_I, l2_got_iframe},
  	{ST_L2_8, EV_L2_I, l2_got_iframe},
  	{ST_L2_5, EV_L2_T200, l2_st5_tout_200},
  	{ST_L2_6, EV_L2_T200, l2_st6_tout_200},
  	{ST_L2_7, EV_L2_T200, l2_st7_tout_200},
  	{ST_L2_8, EV_L2_T200, l2_st8_tout_200},
  	{ST_L2_7, EV_L2_T203, l2_st7_tout_203},
  	{ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
  	{ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
  	{ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
  	{ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
  	{ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
  	{ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
  	{ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
  	{ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
  	{ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
  	{ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
6b44d4e69   Jan Engelhardt   Fix typos in driv...
1587
  	{ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistent_da},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1588
1589
  	{ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
  	{ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
6b44d4e69   Jan Engelhardt   Fix typos in driv...
1590
1591
1592
1593
1594
  	{ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistent_da},
  	{ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistent_da},
  	{ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistent_da},
  	{ST_L2_7, EV_L1_DEACTIVATE, l2_persistent_da},
  	{ST_L2_8, EV_L1_DEACTIVATE, l2_persistent_da},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1595
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
  static void
  isdnl2_l1l2(struct PStack *st, int pr, void *arg)
  {
  	struct sk_buff *skb = arg;
  	u_char *datap;
  	int ret = 1, len;
  	int c = 0;
  
  	switch (pr) {
  		case (PH_DATA | INDICATION):
  			datap = skb->data;
  			len = l2addrsize(&st->l2);
  			if (skb->len > len)
  				datap += len;
  			else {
  				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'N');
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1612
  				dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
  				return;
  			}
  			if (!(*datap & 1)) {	/* I-Frame */
  				if(!(c = iframe_error(st, skb)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_I, skb);
  			} else if (IsSFrame(datap, st)) {	/* S-Frame */
  				if(!(c = super_error(st, skb)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_SUPER, skb);
  			} else if (IsUI(datap)) {
  				if(!(c = UI_error(st, skb)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_UI, skb);
  			} else if (IsSABME(datap, st)) {
  				if(!(c = unnum_error(st, skb, CMD)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_SABME, skb);
  			} else if (IsUA(datap)) {
  				if(!(c = unnum_error(st, skb, RSP)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_UA, skb);
  			} else if (IsDISC(datap)) {
  				if(!(c = unnum_error(st, skb, CMD)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_DISC, skb);
  			} else if (IsDM(datap)) {
  				if(!(c = unnum_error(st, skb, RSP)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_DM, skb);
  			} else if (IsFRMR(datap)) {
  				if(!(c = FRMR_error(st,skb)))
  					ret = FsmEvent(&st->l2.l2m, EV_L2_FRMR, skb);
  			} else {
  				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'L');
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1641
  				dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1642
1643
1644
  				ret = 0;
  			}
  			if(c) {
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1645
  				dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646
1647
1648
1649
  				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
  				ret = 0;
  			}
  			if (ret)
672c3fd90   Adrian Bunk   [PATCH] drivers/i...
1650
  				dev_kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
  			break;
  		case (PH_PULL | CONFIRM):
  			FsmEvent(&st->l2.l2m, EV_L2_ACK_PULL, arg);
  			break;
  		case (PH_PAUSE | INDICATION):
  			test_and_set_bit(FLG_DCHAN_BUSY, &st->l2.flag);
  			break;
  		case (PH_PAUSE | CONFIRM):
  			test_and_clear_bit(FLG_DCHAN_BUSY, &st->l2.flag);
  			break;
  		case (PH_ACTIVATE | CONFIRM):
  		case (PH_ACTIVATE | INDICATION):
  			test_and_set_bit(FLG_L1_ACTIV, &st->l2.flag);
  			if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
  				FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
  			break;
  		case (PH_DEACTIVATE | INDICATION):
  		case (PH_DEACTIVATE | CONFIRM):
  			test_and_clear_bit(FLG_L1_ACTIV, &st->l2.flag);
  			FsmEvent(&st->l2.l2m, EV_L1_DEACTIVATE, arg);
  			break;
  		default:
  			l2m_debug(&st->l2.l2m, "l2 unknown pr %04x", pr);
  			break;
  	}
  }
  
  static void
  isdnl2_l3l2(struct PStack *st, int pr, void *arg)
  {
  	switch (pr) {
  		case (DL_DATA | REQUEST):
  			if (FsmEvent(&st->l2.l2m, EV_L2_DL_DATA, arg)) {
  				dev_kfree_skb((struct sk_buff *) arg);
  			}
  			break;
  		case (DL_UNIT_DATA | REQUEST):
  			if (FsmEvent(&st->l2.l2m, EV_L2_DL_UNIT_DATA, arg)) {
  				dev_kfree_skb((struct sk_buff *) arg);
  			}
  			break;
  		case (DL_ESTABLISH | REQUEST):
  			if (test_bit(FLG_L1_ACTIV, &st->l2.flag)) {
  				if (test_bit(FLG_LAPD, &st->l2.flag) ||
  					test_bit(FLG_ORIG, &st->l2.flag)) {
  					FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
  				}
  			} else {
  				if (test_bit(FLG_LAPD, &st->l2.flag) ||
  					test_bit(FLG_ORIG, &st->l2.flag)) {
  					test_and_set_bit(FLG_ESTAB_PEND, &st->l2.flag);
  				}
  				st->l2.l2l1(st, PH_ACTIVATE, NULL);
  			}
  			break;
  		case (DL_RELEASE | REQUEST):
  			if (test_bit(FLG_LAPB, &st->l2.flag)) {
  				st->l2.l2l1(st, PH_DEACTIVATE, NULL);
  			}
  			FsmEvent(&st->l2.l2m, EV_L2_DL_RELEASE_REQ, arg);
  			break;
  		case (MDL_ASSIGN | REQUEST):
  			FsmEvent(&st->l2.l2m, EV_L2_MDL_ASSIGN, arg);
  			break;
  		case (MDL_REMOVE | REQUEST):
  			FsmEvent(&st->l2.l2m, EV_L2_MDL_REMOVE, arg);
  			break;
  		case (MDL_ERROR | RESPONSE):
  			FsmEvent(&st->l2.l2m, EV_L2_MDL_ERROR, arg);
  			break;
  	}
  }
  
  void
  releasestack_isdnl2(struct PStack *st)
  {
  	FsmDelTimer(&st->l2.t200, 21);
  	FsmDelTimer(&st->l2.t203, 16);
  	skb_queue_purge(&st->l2.i_queue);
  	skb_queue_purge(&st->l2.ui_queue);
  	ReleaseWin(&st->l2);
  }
  
  static void
  l2m_debug(struct FsmInst *fi, char *fmt, ...)
  {
  	va_list args;
  	struct PStack *st = fi->userdata;
  
  	va_start(args, fmt);
  	VHiSax_putstatus(st->l1.hardware, st->l2.debug_id, fmt, args);
  	va_end(args);
  }
  
  void
  setstack_isdnl2(struct PStack *st, char *debug_id)
  {
  	spin_lock_init(&st->l2.lock);
  	st->l1.l1l2 = isdnl2_l1l2;
  	st->l3.l3l2 = isdnl2_l3l2;
  
  	skb_queue_head_init(&st->l2.i_queue);
  	skb_queue_head_init(&st->l2.ui_queue);
  	InitWin(&st->l2);
  	st->l2.debug = 0;
  
  	st->l2.l2m.fsm = &l2fsm;
  	if (test_bit(FLG_LAPB, &st->l2.flag))
  		st->l2.l2m.state = ST_L2_4;
  	else
  	st->l2.l2m.state = ST_L2_1;
  	st->l2.l2m.debug = 0;
  	st->l2.l2m.userdata = st;
  	st->l2.l2m.userint = 0;
  	st->l2.l2m.printdebug = l2m_debug;
  	strcpy(st->l2.debug_id, debug_id);
  
  	FsmInitTimer(&st->l2.l2m, &st->l2.t200);
  	FsmInitTimer(&st->l2.l2m, &st->l2.t203);
  }
  
  static void
  transl2_l3l2(struct PStack *st, int pr, void *arg)
  {
  	switch (pr) {
  		case (DL_DATA | REQUEST):
  		case (DL_UNIT_DATA | REQUEST):
  			st->l2.l2l1(st, PH_DATA | REQUEST, arg);
  			break;
  		case (DL_ESTABLISH | REQUEST):
  			st->l2.l2l1(st, PH_ACTIVATE | REQUEST, NULL);
  			break;
  		case (DL_RELEASE | REQUEST):
  			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
  			break;
  	}
  }
  
  void
  setstack_transl2(struct PStack *st)
  {
  	st->l3.l3l2 = transl2_l3l2;
  }
  
  void
  releasestack_transl2(struct PStack *st)
  {
  }
  
  int __init
  Isdnl2New(void)
  {
  	l2fsm.state_count = L2_STATE_COUNT;
  	l2fsm.event_count = L2_EVENT_COUNT;
  	l2fsm.strEvent = strL2Event;
  	l2fsm.strState = strL2State;
ba2d6ccb1   Karsten Keil   ISDN: ARRAY_SIZE ...
1807
  	return FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1808
1809
1810
1811
1812
1813
1814
  }
  
  void
  Isdnl2Free(void)
  {
  	FsmFree(&l2fsm);
  }