xref: /openbmc/linux/drivers/isdn/mISDN/tei.c (revision 4800cd83)
1 /*
2  *
3  * Author	Karsten Keil <kkeil@novell.com>
4  *
5  * Copyright 2008  by Karsten Keil <kkeil@novell.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 #include "layer2.h"
18 #include <linux/random.h>
19 #include <linux/slab.h>
20 #include "core.h"
21 
22 #define ID_REQUEST	1
23 #define ID_ASSIGNED	2
24 #define ID_DENIED	3
25 #define ID_CHK_REQ	4
26 #define ID_CHK_RES	5
27 #define ID_REMOVE	6
28 #define ID_VERIFY	7
29 
30 #define TEI_ENTITY_ID	0xf
31 
32 #define MGR_PH_ACTIVE	16
33 #define MGR_PH_NOTREADY	17
34 
35 #define DATIMER_VAL	10000
36 
37 static 	u_int	*debug;
38 
39 static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
40 static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
41 static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
42 
43 enum {
44 	ST_L1_DEACT,
45 	ST_L1_DEACT_PENDING,
46 	ST_L1_ACTIV,
47 };
48 #define DEACT_STATE_COUNT (ST_L1_ACTIV+1)
49 
50 static char *strDeactState[] =
51 {
52 	"ST_L1_DEACT",
53 	"ST_L1_DEACT_PENDING",
54 	"ST_L1_ACTIV",
55 };
56 
57 enum {
58 	EV_ACTIVATE,
59 	EV_ACTIVATE_IND,
60 	EV_DEACTIVATE,
61 	EV_DEACTIVATE_IND,
62 	EV_UI,
63 	EV_DATIMER,
64 };
65 
66 #define DEACT_EVENT_COUNT (EV_DATIMER+1)
67 
68 static char *strDeactEvent[] =
69 {
70 	"EV_ACTIVATE",
71 	"EV_ACTIVATE_IND",
72 	"EV_DEACTIVATE",
73 	"EV_DEACTIVATE_IND",
74 	"EV_UI",
75 	"EV_DATIMER",
76 };
77 
78 static void
79 da_debug(struct FsmInst *fi, char *fmt, ...)
80 {
81 	struct manager	*mgr = fi->userdata;
82 	struct va_format vaf;
83 	va_list va;
84 
85 	if (!(*debug & DEBUG_L2_TEIFSM))
86 		return;
87 
88 	va_start(va, fmt);
89 
90 	vaf.fmt = fmt;
91 	vaf.va = &va;
92 
93 	printk(KERN_DEBUG "mgr(%d): %pV\n", mgr->ch.st->dev->id, &vaf);
94 
95 	va_end(va);
96 }
97 
98 static void
99 da_activate(struct FsmInst *fi, int event, void *arg)
100 {
101 	struct manager	*mgr = fi->userdata;
102 
103 	if (fi->state == ST_L1_DEACT_PENDING)
104 		mISDN_FsmDelTimer(&mgr->datimer, 1);
105 	mISDN_FsmChangeState(fi, ST_L1_ACTIV);
106 }
107 
108 static void
109 da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
110 {
111 	mISDN_FsmChangeState(fi, ST_L1_DEACT);
112 }
113 
114 static void
115 da_deactivate(struct FsmInst *fi, int event, void *arg)
116 {
117 	struct manager	*mgr = fi->userdata;
118 	struct layer2	*l2;
119 	u_long		flags;
120 
121 	read_lock_irqsave(&mgr->lock, flags);
122 	list_for_each_entry(l2, &mgr->layer2, list) {
123 		if (l2->l2m.state > ST_L2_4) {
124 			/* have still activ TEI */
125 			read_unlock_irqrestore(&mgr->lock, flags);
126 			return;
127 		}
128 	}
129 	read_unlock_irqrestore(&mgr->lock, flags);
130 	/* All TEI are inactiv */
131 	if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
132 		mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
133 			NULL, 1);
134 		mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
135 	}
136 }
137 
138 static void
139 da_ui(struct FsmInst *fi, int event, void *arg)
140 {
141 	struct manager	*mgr = fi->userdata;
142 
143 	/* restart da timer */
144 	if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
145 		mISDN_FsmDelTimer(&mgr->datimer, 2);
146 		mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
147 			NULL, 2);
148 	}
149 }
150 
151 static void
152 da_timer(struct FsmInst *fi, int event, void *arg)
153 {
154 	struct manager	*mgr = fi->userdata;
155 	struct layer2	*l2;
156 	u_long		flags;
157 
158 	/* check again */
159 	read_lock_irqsave(&mgr->lock, flags);
160 	list_for_each_entry(l2, &mgr->layer2, list) {
161 		if (l2->l2m.state > ST_L2_4) {
162 			/* have still activ TEI */
163 			read_unlock_irqrestore(&mgr->lock, flags);
164 			mISDN_FsmChangeState(fi, ST_L1_ACTIV);
165 			return;
166 		}
167 	}
168 	read_unlock_irqrestore(&mgr->lock, flags);
169 	/* All TEI are inactiv */
170 	mISDN_FsmChangeState(fi, ST_L1_DEACT);
171 	_queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
172 	    GFP_ATOMIC);
173 }
174 
175 static struct FsmNode DeactFnList[] =
176 {
177 	{ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
178 	{ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
179 	{ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
180 	{ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
181 	{ST_L1_DEACT_PENDING, EV_UI, da_ui},
182 	{ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
183 };
184 
185 enum {
186 	ST_TEI_NOP,
187 	ST_TEI_IDREQ,
188 	ST_TEI_IDVERIFY,
189 };
190 
191 #define TEI_STATE_COUNT (ST_TEI_IDVERIFY+1)
192 
193 static char *strTeiState[] =
194 {
195 	"ST_TEI_NOP",
196 	"ST_TEI_IDREQ",
197 	"ST_TEI_IDVERIFY",
198 };
199 
200 enum {
201 	EV_IDREQ,
202 	EV_ASSIGN,
203 	EV_ASSIGN_REQ,
204 	EV_DENIED,
205 	EV_CHKREQ,
206 	EV_CHKRESP,
207 	EV_REMOVE,
208 	EV_VERIFY,
209 	EV_TIMER,
210 };
211 
212 #define TEI_EVENT_COUNT (EV_TIMER+1)
213 
214 static char *strTeiEvent[] =
215 {
216 	"EV_IDREQ",
217 	"EV_ASSIGN",
218 	"EV_ASSIGN_REQ",
219 	"EV_DENIED",
220 	"EV_CHKREQ",
221 	"EV_CHKRESP",
222 	"EV_REMOVE",
223 	"EV_VERIFY",
224 	"EV_TIMER",
225 };
226 
227 static void
228 tei_debug(struct FsmInst *fi, char *fmt, ...)
229 {
230 	struct teimgr	*tm = fi->userdata;
231 	struct va_format vaf;
232 	va_list va;
233 
234 	if (!(*debug & DEBUG_L2_TEIFSM))
235 		return;
236 
237 	va_start(va, fmt);
238 
239 	vaf.fmt = fmt;
240 	vaf.va = &va;
241 
242 	printk(KERN_DEBUG "sapi(%d) tei(%d): %pV\n",
243 	       tm->l2->sapi, tm->l2->tei, &vaf);
244 
245 	va_end(va);
246 }
247 
248 
249 
250 static int
251 get_free_id(struct manager *mgr)
252 {
253 	u64		ids = 0;
254 	int		i;
255 	struct layer2	*l2;
256 
257 	list_for_each_entry(l2, &mgr->layer2, list) {
258 		if (l2->ch.nr > 63) {
259 			printk(KERN_WARNING
260 			    "%s: more as 63 layer2 for one device\n",
261 			    __func__);
262 			return -EBUSY;
263 		}
264 		test_and_set_bit(l2->ch.nr, (u_long *)&ids);
265 	}
266 	for (i = 1; i < 64; i++)
267 		if (!test_bit(i, (u_long *)&ids))
268 			return i;
269 	printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
270 	    __func__);
271 	return -EBUSY;
272 }
273 
274 static int
275 get_free_tei(struct manager *mgr)
276 {
277 	u64		ids = 0;
278 	int		i;
279 	struct layer2	*l2;
280 
281 	list_for_each_entry(l2, &mgr->layer2, list) {
282 		if (l2->ch.nr == 0)
283 			continue;
284 		if ((l2->ch.addr & 0xff) != 0)
285 			continue;
286 		i = l2->ch.addr >> 8;
287 		if (i < 64)
288 			continue;
289 		i -= 64;
290 
291 		test_and_set_bit(i, (u_long *)&ids);
292 	}
293 	for (i = 0; i < 64; i++)
294 		if (!test_bit(i, (u_long *)&ids))
295 			return i + 64;
296 	printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
297 	    __func__);
298 	return -1;
299 }
300 
301 static void
302 teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
303 {
304 	struct sk_buff	*skb;
305 	struct mISDNhead *hh;
306 	int		err;
307 
308 	skb = mI_alloc_skb(len, GFP_ATOMIC);
309 	if (!skb)
310 		return;
311 	hh = mISDN_HEAD_P(skb);
312 	hh->prim = prim;
313 	hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
314 	if (len)
315 		memcpy(skb_put(skb, len), arg, len);
316 	err = mgr->up->send(mgr->up, skb);
317 	if (err) {
318 		printk(KERN_WARNING "%s: err=%d\n", __func__, err);
319 		dev_kfree_skb(skb);
320 	}
321 }
322 
323 static u_int
324 new_id(struct manager *mgr)
325 {
326 	u_int	id;
327 
328 	id = mgr->nextid++;
329 	if (id == 0x7fff)
330 		mgr->nextid = 1;
331 	id <<= 16;
332 	id |= GROUP_TEI << 8;
333 	id |= TEI_SAPI;
334 	return id;
335 }
336 
337 static void
338 do_send(struct manager *mgr)
339 {
340 	if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
341 		return;
342 
343 	if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
344 		struct sk_buff	*skb = skb_dequeue(&mgr->sendq);
345 
346 		if (!skb) {
347 			test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
348 			return;
349 		}
350 		mgr->lastid = mISDN_HEAD_ID(skb);
351 		mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
352 		if (mgr->ch.recv(mgr->ch.peer, skb)) {
353 			dev_kfree_skb(skb);
354 			test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
355 			mgr->lastid = MISDN_ID_NONE;
356 		}
357 	}
358 }
359 
360 static void
361 do_ack(struct manager *mgr, u_int id)
362 {
363 	if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
364 		if (id == mgr->lastid) {
365 			if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
366 				struct sk_buff	*skb;
367 
368 				skb = skb_dequeue(&mgr->sendq);
369 				if (skb) {
370 					mgr->lastid = mISDN_HEAD_ID(skb);
371 					if (!mgr->ch.recv(mgr->ch.peer, skb))
372 						return;
373 					dev_kfree_skb(skb);
374 				}
375 			}
376 			mgr->lastid = MISDN_ID_NONE;
377 			test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
378 		}
379 	}
380 }
381 
382 static void
383 mgr_send_down(struct manager *mgr, struct sk_buff *skb)
384 {
385 	skb_queue_tail(&mgr->sendq, skb);
386 	if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
387 		_queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
388 		    NULL, GFP_KERNEL);
389 	} else {
390 		do_send(mgr);
391 	}
392 }
393 
394 static int
395 dl_unit_data(struct manager *mgr, struct sk_buff *skb)
396 {
397 	if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
398 		return -EINVAL;
399 	if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
400 		_queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
401 		    NULL, GFP_KERNEL);
402 	skb_push(skb, 3);
403 	skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
404 	skb->data[1] = 0xff; /* TEI 127 */
405 	skb->data[2] = UI;   /* UI frame */
406 	mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
407 	mISDN_HEAD_ID(skb) = new_id(mgr);
408 	skb_queue_tail(&mgr->sendq, skb);
409 	do_send(mgr);
410 	return 0;
411 }
412 
413 static unsigned int
414 random_ri(void)
415 {
416 	u16 x;
417 
418 	get_random_bytes(&x, sizeof(x));
419 	return x;
420 }
421 
422 static struct layer2 *
423 findtei(struct manager *mgr, int tei)
424 {
425 	struct layer2	*l2;
426 	u_long		flags;
427 
428 	read_lock_irqsave(&mgr->lock, flags);
429 	list_for_each_entry(l2, &mgr->layer2, list) {
430 		if ((l2->sapi == 0) && (l2->tei > 0) &&
431 		    (l2->tei != GROUP_TEI) && (l2->tei == tei))
432 			goto done;
433 	}
434 	l2 = NULL;
435 done:
436 	read_unlock_irqrestore(&mgr->lock, flags);
437 	return l2;
438 }
439 
440 static void
441 put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
442 {
443 	struct sk_buff *skb;
444 	u_char bp[8];
445 
446 	bp[0] = (TEI_SAPI << 2);
447 	if (test_bit(MGR_OPT_NETWORK, &mgr->options))
448 		bp[0] |= 2; /* CR:=1 for net command */
449 	bp[1] = (GROUP_TEI << 1) | 0x1;
450 	bp[2] = UI;
451 	bp[3] = TEI_ENTITY_ID;
452 	bp[4] = ri >> 8;
453 	bp[5] = ri & 0xff;
454 	bp[6] = m_id;
455 	bp[7] = ((tei << 1) & 0xff) | 1;
456 	skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
457 	if (!skb) {
458 		printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
459 		return;
460 	}
461 	mgr_send_down(mgr, skb);
462 }
463 
464 static void
465 tei_id_request(struct FsmInst *fi, int event, void *arg)
466 {
467 	struct teimgr *tm = fi->userdata;
468 
469 	if (tm->l2->tei != GROUP_TEI) {
470 		tm->tei_m.printdebug(&tm->tei_m,
471 			"assign request for already assigned tei %d",
472 			tm->l2->tei);
473 		return;
474 	}
475 	tm->ri = random_ri();
476 	if (*debug & DEBUG_L2_TEI)
477 		tm->tei_m.printdebug(&tm->tei_m,
478 			"assign request ri %d", tm->ri);
479 	put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
480 	mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
481 	mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
482 	tm->nval = 3;
483 }
484 
485 static void
486 tei_id_assign(struct FsmInst *fi, int event, void *arg)
487 {
488 	struct teimgr	*tm = fi->userdata;
489 	struct layer2	*l2;
490 	u_char *dp = arg;
491 	int ri, tei;
492 
493 	ri = ((unsigned int) *dp++ << 8);
494 	ri += *dp++;
495 	dp++;
496 	tei = *dp >> 1;
497 	if (*debug & DEBUG_L2_TEI)
498 		tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
499 			ri, tei);
500 	l2 = findtei(tm->mgr, tei);
501 	if (l2) {	/* same tei is in use */
502 		if (ri != l2->tm->ri) {
503 			tm->tei_m.printdebug(fi,
504 				"possible duplicate assignment tei %d", tei);
505 			tei_l2(l2, MDL_ERROR_RSP, 0);
506 		}
507 	} else if (ri == tm->ri) {
508 		mISDN_FsmDelTimer(&tm->timer, 1);
509 		mISDN_FsmChangeState(fi, ST_TEI_NOP);
510 		tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
511 	}
512 }
513 
514 static void
515 tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
516 {
517 	struct teimgr	*tm = fi->userdata;
518 	struct layer2	*l2;
519 	u_char *dp = arg;
520 	int tei, ri;
521 
522 	ri = ((unsigned int) *dp++ << 8);
523 	ri += *dp++;
524 	dp++;
525 	tei = *dp >> 1;
526 	if (*debug & DEBUG_L2_TEI)
527 		tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
528 			ri, tei);
529 	l2 = findtei(tm->mgr, tei);
530 	if (l2) {	/* same tei is in use */
531 		if (ri != l2->tm->ri) {	/* and it wasn't our request */
532 			tm->tei_m.printdebug(fi,
533 				"possible duplicate assignment tei %d", tei);
534 			mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
535 		}
536 	}
537 }
538 
539 static void
540 tei_id_denied(struct FsmInst *fi, int event, void *arg)
541 {
542 	struct teimgr *tm = fi->userdata;
543 	u_char *dp = arg;
544 	int ri, tei;
545 
546 	ri = ((unsigned int) *dp++ << 8);
547 	ri += *dp++;
548 	dp++;
549 	tei = *dp >> 1;
550 	if (*debug & DEBUG_L2_TEI)
551 		tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
552 			ri, tei);
553 }
554 
555 static void
556 tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
557 {
558 	struct teimgr *tm = fi->userdata;
559 	u_char *dp = arg;
560 	int tei;
561 
562 	tei = *(dp+3) >> 1;
563 	if (*debug & DEBUG_L2_TEI)
564 		tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
565 	if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
566 	    (tei == tm->l2->tei))) {
567 		mISDN_FsmDelTimer(&tm->timer, 4);
568 		mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
569 		put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
570 	}
571 }
572 
573 static void
574 tei_id_remove(struct FsmInst *fi, int event, void *arg)
575 {
576 	struct teimgr *tm = fi->userdata;
577 	u_char *dp = arg;
578 	int tei;
579 
580 	tei = *(dp+3) >> 1;
581 	if (*debug & DEBUG_L2_TEI)
582 		tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
583 	if ((tm->l2->tei != GROUP_TEI) &&
584 	    ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
585 		mISDN_FsmDelTimer(&tm->timer, 5);
586 		mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
587 		tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
588 	}
589 }
590 
591 static void
592 tei_id_verify(struct FsmInst *fi, int event, void *arg)
593 {
594 	struct teimgr *tm = fi->userdata;
595 
596 	if (*debug & DEBUG_L2_TEI)
597 		tm->tei_m.printdebug(fi, "id verify request for tei %d",
598 			tm->l2->tei);
599 	put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
600 	mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
601 	mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
602 	tm->nval = 2;
603 }
604 
605 static void
606 tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
607 {
608 	struct teimgr *tm = fi->userdata;
609 
610 	if (--tm->nval) {
611 		tm->ri = random_ri();
612 		if (*debug & DEBUG_L2_TEI)
613 			tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
614 				4 - tm->nval, tm->ri);
615 		put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
616 		mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
617 	} else {
618 		tm->tei_m.printdebug(fi, "assign req failed");
619 		tei_l2(tm->l2, MDL_ERROR_RSP, 0);
620 		mISDN_FsmChangeState(fi, ST_TEI_NOP);
621 	}
622 }
623 
624 static void
625 tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
626 {
627 	struct teimgr *tm = fi->userdata;
628 
629 	if (--tm->nval) {
630 		if (*debug & DEBUG_L2_TEI)
631 			tm->tei_m.printdebug(fi,
632 				"id verify req(%d) for tei %d",
633 				3 - tm->nval, tm->l2->tei);
634 		put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
635 		mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
636 	} else {
637 		tm->tei_m.printdebug(fi, "verify req for tei %d failed",
638 			tm->l2->tei);
639 		tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
640 		mISDN_FsmChangeState(fi, ST_TEI_NOP);
641 	}
642 }
643 
644 static struct FsmNode TeiFnListUser[] =
645 {
646 	{ST_TEI_NOP, EV_IDREQ, tei_id_request},
647 	{ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
648 	{ST_TEI_NOP, EV_VERIFY, tei_id_verify},
649 	{ST_TEI_NOP, EV_REMOVE, tei_id_remove},
650 	{ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
651 	{ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
652 	{ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
653 	{ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
654 	{ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
655 	{ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
656 	{ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
657 };
658 
659 static void
660 tei_l2remove(struct layer2 *l2)
661 {
662 	put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
663 	tei_l2(l2, MDL_REMOVE_REQ, 0);
664 	list_del(&l2->ch.list);
665 	l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
666 }
667 
668 static void
669 tei_assign_req(struct FsmInst *fi, int event, void *arg)
670 {
671 	struct teimgr *tm = fi->userdata;
672 	u_char *dp = arg;
673 
674 	if (tm->l2->tei == GROUP_TEI) {
675 		tm->tei_m.printdebug(&tm->tei_m,
676 			"net tei assign request without tei");
677 		return;
678 	}
679 	tm->ri = ((unsigned int) *dp++ << 8);
680 	tm->ri += *dp++;
681 	if (*debug & DEBUG_L2_TEI)
682 		tm->tei_m.printdebug(&tm->tei_m,
683 			"net assign request ri %d teim %d", tm->ri, *dp);
684 	put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
685 	mISDN_FsmChangeState(fi, ST_TEI_NOP);
686 }
687 
688 static void
689 tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
690 {
691 	struct teimgr	*tm = fi->userdata;
692 
693 	if (*debug & DEBUG_L2_TEI)
694 		tm->tei_m.printdebug(fi, "id check request for tei %d",
695 		    tm->l2->tei);
696 	tm->rcnt = 0;
697 	put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
698 	mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
699 	mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
700 	tm->nval = 2;
701 }
702 
703 static void
704 tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
705 {
706 	struct teimgr *tm = fi->userdata;
707 	u_char *dp = arg;
708 	int tei;
709 
710 	tei = dp[3] >> 1;
711 	if (*debug & DEBUG_L2_TEI)
712 		tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
713 	if (tei == tm->l2->tei)
714 		tm->rcnt++;
715 }
716 
717 static void
718 tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
719 {
720 	struct teimgr *tm = fi->userdata;
721 	u_char *dp = arg;
722 	int tei;
723 
724 	tei = dp[3] >> 1;
725 	if (*debug & DEBUG_L2_TEI)
726 		tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
727 		    tei, tm->l2->tei);
728 	if (tei == tm->l2->tei)
729 		tei_id_chk_req_net(fi, event, arg);
730 }
731 
732 static void
733 tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
734 {
735 	struct teimgr *tm = fi->userdata;
736 
737 	if (tm->rcnt == 1) {
738 		if (*debug & DEBUG_L2_TEI)
739 			tm->tei_m.printdebug(fi,
740 			    "check req for tei %d successful\n", tm->l2->tei);
741 		mISDN_FsmChangeState(fi, ST_TEI_NOP);
742 	} else if (tm->rcnt > 1) {
743 		/* duplicate assignment; remove */
744 		tei_l2remove(tm->l2);
745 	} else if (--tm->nval) {
746 		if (*debug & DEBUG_L2_TEI)
747 			tm->tei_m.printdebug(fi,
748 				"id check req(%d) for tei %d",
749 				3 - tm->nval, tm->l2->tei);
750 		put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
751 		mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
752 	} else {
753 		tm->tei_m.printdebug(fi, "check req for tei %d failed",
754 			tm->l2->tei);
755 		mISDN_FsmChangeState(fi, ST_TEI_NOP);
756 		tei_l2remove(tm->l2);
757 	}
758 }
759 
760 static struct FsmNode TeiFnListNet[] =
761 {
762 	{ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
763 	{ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
764 	{ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
765 	{ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
766 	{ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
767 };
768 
769 static void
770 tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
771 {
772 	if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
773 		return;
774 	if (*debug & DEBUG_L2_TEI)
775 		tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
776 	if (mt == ID_ASSIGNED)
777 		mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
778 	else if (mt == ID_DENIED)
779 		mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
780 	else if (mt == ID_CHK_REQ)
781 		mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
782 	else if (mt == ID_REMOVE)
783 		mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
784 	else if (mt == ID_VERIFY)
785 		mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
786 	else if (mt == ID_CHK_RES)
787 		mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
788 }
789 
790 static struct layer2 *
791 create_new_tei(struct manager *mgr, int tei, int sapi)
792 {
793 	u_long		opt = 0;
794 	u_long		flags;
795 	int		id;
796 	struct layer2	*l2;
797 
798 	if (!mgr->up)
799 		return NULL;
800 	if ((tei >= 0) && (tei < 64))
801 		test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
802 	if (mgr->ch.st->dev->Dprotocols
803 	  & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
804 		test_and_set_bit(OPTION_L2_PMX, &opt);
805 	l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
806 	if (!l2) {
807 		printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
808 		return NULL;
809 	}
810 	l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
811 	if (!l2->tm) {
812 		kfree(l2);
813 		printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
814 		return NULL;
815 	}
816 	l2->tm->mgr = mgr;
817 	l2->tm->l2 = l2;
818 	l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
819 	l2->tm->tei_m.userdata = l2->tm;
820 	l2->tm->tei_m.printdebug = tei_debug;
821 	l2->tm->tei_m.fsm = &teifsmn;
822 	l2->tm->tei_m.state = ST_TEI_NOP;
823 	l2->tm->tval = 2000; /* T202  2 sec */
824 	mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
825 	write_lock_irqsave(&mgr->lock, flags);
826 	id = get_free_id(mgr);
827 	list_add_tail(&l2->list, &mgr->layer2);
828 	write_unlock_irqrestore(&mgr->lock, flags);
829 	if (id < 0) {
830 		l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
831 		printk(KERN_WARNING "%s:no free id\n", __func__);
832 		return NULL;
833 	} else {
834 		l2->ch.nr = id;
835 		__add_layer2(&l2->ch, mgr->ch.st);
836 		l2->ch.recv = mgr->ch.recv;
837 		l2->ch.peer = mgr->ch.peer;
838 		l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
839 	}
840 	return l2;
841 }
842 
843 static void
844 new_tei_req(struct manager *mgr, u_char *dp)
845 {
846 	int		tei, ri;
847 	struct layer2	*l2;
848 
849 	ri = dp[0] << 8;
850 	ri += dp[1];
851 	if (!mgr->up)
852 		goto denied;
853 	if (!(dp[3] & 1)) /* Extension bit != 1 */
854 		goto denied;
855 	if (dp[3] != 0xff)
856 		tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
857 	else
858 		tei = get_free_tei(mgr);
859 	if (tei < 0) {
860 		printk(KERN_WARNING "%s:No free tei\n", __func__);
861 		goto denied;
862 	}
863 	l2 = create_new_tei(mgr, tei, CTRL_SAPI);
864 	if (!l2)
865 		goto denied;
866 	else
867 		mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
868 	return;
869 denied:
870 	put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
871 }
872 
873 static int
874 ph_data_ind(struct manager *mgr, struct sk_buff *skb)
875 {
876 	int		ret = -EINVAL;
877 	struct layer2	*l2, *nl2;
878 	u_char		mt;
879 
880 	if (skb->len < 8) {
881 		if (*debug  & DEBUG_L2_TEI)
882 			printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
883 			    __func__, skb->len);
884 		goto done;
885 	}
886 
887 	if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
888 		goto done;
889 	if (skb->data[0] & 1) /* EA0 formal error */
890 		goto done;
891 	if (!(skb->data[1] & 1)) /* EA1 formal error */
892 		goto done;
893 	if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
894 		goto done;
895 	if ((skb->data[2] & 0xef) != UI) /* not UI */
896 		goto done;
897 	if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
898 		goto done;
899 	mt = skb->data[6];
900 	switch (mt) {
901 	case ID_REQUEST:
902 	case ID_CHK_RES:
903 	case ID_VERIFY:
904 		if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
905 			goto done;
906 		break;
907 	case ID_ASSIGNED:
908 	case ID_DENIED:
909 	case ID_CHK_REQ:
910 	case ID_REMOVE:
911 		if (test_bit(MGR_OPT_NETWORK, &mgr->options))
912 			goto done;
913 		break;
914 	default:
915 		goto done;
916 	}
917 	ret = 0;
918 	if (mt == ID_REQUEST) {
919 		new_tei_req(mgr, &skb->data[4]);
920 		goto done;
921 	}
922 	list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
923 		tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
924 	}
925 done:
926 	return ret;
927 }
928 
929 int
930 l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
931 {
932 	struct teimgr	*tm = l2->tm;
933 
934 	if (test_bit(FLG_FIXED_TEI, &l2->flag))
935 		return 0;
936 	if (*debug & DEBUG_L2_TEI)
937 		printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
938 	switch (cmd) {
939 	case MDL_ASSIGN_IND:
940 		mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
941 		break;
942 	case MDL_ERROR_IND:
943 		if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
944 			mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
945 		if (test_bit(MGR_OPT_USER, &tm->mgr->options))
946 			mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
947 		break;
948 	case MDL_STATUS_UP_IND:
949 		if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
950 			mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
951 		break;
952 	case MDL_STATUS_DOWN_IND:
953 		if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
954 			mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
955 		break;
956 	case MDL_STATUS_UI_IND:
957 		if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
958 			mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
959 		break;
960 	}
961 	return 0;
962 }
963 
964 void
965 TEIrelease(struct layer2 *l2)
966 {
967 	struct teimgr	*tm = l2->tm;
968 	u_long		flags;
969 
970 	mISDN_FsmDelTimer(&tm->timer, 1);
971 	write_lock_irqsave(&tm->mgr->lock, flags);
972 	list_del(&l2->list);
973 	write_unlock_irqrestore(&tm->mgr->lock, flags);
974 	l2->tm = NULL;
975 	kfree(tm);
976 }
977 
978 static int
979 create_teimgr(struct manager *mgr, struct channel_req *crq)
980 {
981 	struct layer2	*l2;
982 	u_long 		opt = 0;
983 	u_long		flags;
984 	int		id;
985 
986 	if (*debug & DEBUG_L2_TEI)
987 		printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
988 			__func__, dev_name(&mgr->ch.st->dev->dev),
989 			crq->protocol, crq->adr.dev, crq->adr.channel,
990 			crq->adr.sapi, crq->adr.tei);
991 	if (crq->adr.tei > GROUP_TEI)
992 		return -EINVAL;
993 	if (crq->adr.tei < 64)
994 		test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
995 	if (crq->adr.tei == 0)
996 		test_and_set_bit(OPTION_L2_PTP, &opt);
997 	if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
998 		if (crq->protocol == ISDN_P_LAPD_TE)
999 			return -EPROTONOSUPPORT;
1000 		if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
1001 			return -EINVAL;
1002 		if (mgr->up) {
1003 			printk(KERN_WARNING
1004 			    "%s: only one network manager is allowed\n",
1005 			    __func__);
1006 			return -EBUSY;
1007 		}
1008 	} else if (test_bit(MGR_OPT_USER, &mgr->options)) {
1009 		if (crq->protocol == ISDN_P_LAPD_NT)
1010 			return -EPROTONOSUPPORT;
1011 		if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
1012 			return -EINVAL; /* dyn tei */
1013 	} else {
1014 		if (crq->protocol == ISDN_P_LAPD_NT)
1015 			test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
1016 		if (crq->protocol == ISDN_P_LAPD_TE)
1017 			test_and_set_bit(MGR_OPT_USER, &mgr->options);
1018 	}
1019 	if (mgr->ch.st->dev->Dprotocols
1020 	  & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
1021 		test_and_set_bit(OPTION_L2_PMX, &opt);
1022 	if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
1023 		mgr->up = crq->ch;
1024 		id = DL_INFO_L2_CONNECT;
1025 		teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
1026 		crq->ch = NULL;
1027 		if (!list_empty(&mgr->layer2)) {
1028 			read_lock_irqsave(&mgr->lock, flags);
1029 			list_for_each_entry(l2, &mgr->layer2, list) {
1030 				l2->up = mgr->up;
1031 				l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1032 			}
1033 			read_unlock_irqrestore(&mgr->lock, flags);
1034 		}
1035 		return 0;
1036 	}
1037 	l2 = create_l2(crq->ch, crq->protocol, opt,
1038 		crq->adr.tei, crq->adr.sapi);
1039 	if (!l2)
1040 		return -ENOMEM;
1041 	l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1042 	if (!l2->tm) {
1043 		kfree(l2);
1044 		printk(KERN_ERR "kmalloc teimgr failed\n");
1045 		return -ENOMEM;
1046 	}
1047 	l2->tm->mgr = mgr;
1048 	l2->tm->l2 = l2;
1049 	l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
1050 	l2->tm->tei_m.userdata = l2->tm;
1051 	l2->tm->tei_m.printdebug = tei_debug;
1052 	if (crq->protocol == ISDN_P_LAPD_TE) {
1053 		l2->tm->tei_m.fsm = &teifsmu;
1054 		l2->tm->tei_m.state = ST_TEI_NOP;
1055 		l2->tm->tval = 1000; /* T201  1 sec */
1056 	} else {
1057 		l2->tm->tei_m.fsm = &teifsmn;
1058 		l2->tm->tei_m.state = ST_TEI_NOP;
1059 		l2->tm->tval = 2000; /* T202  2 sec */
1060 	}
1061 	mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
1062 	write_lock_irqsave(&mgr->lock, flags);
1063 	id = get_free_id(mgr);
1064 	list_add_tail(&l2->list, &mgr->layer2);
1065 	write_unlock_irqrestore(&mgr->lock, flags);
1066 	if (id < 0) {
1067 		l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1068 	} else {
1069 		l2->ch.nr = id;
1070 		l2->up->nr = id;
1071 		crq->ch = &l2->ch;
1072 		id = 0;
1073 	}
1074 	return id;
1075 }
1076 
1077 static int
1078 mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1079 {
1080 	struct manager	*mgr;
1081 	struct mISDNhead	*hh =  mISDN_HEAD_P(skb);
1082 	int			ret = -EINVAL;
1083 
1084 	mgr = container_of(ch, struct manager, ch);
1085 	if (*debug & DEBUG_L2_RECV)
1086 		printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1087 		    __func__, hh->prim, hh->id);
1088 	switch (hh->prim) {
1089 	case PH_DATA_IND:
1090 		mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1091 		ret = ph_data_ind(mgr, skb);
1092 		break;
1093 	case PH_DATA_CNF:
1094 		do_ack(mgr, hh->id);
1095 		ret = 0;
1096 		break;
1097 	case PH_ACTIVATE_IND:
1098 		test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
1099 		mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1100 		do_send(mgr);
1101 		ret = 0;
1102 		break;
1103 	case PH_DEACTIVATE_IND:
1104 		test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
1105 		mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1106 		ret = 0;
1107 		break;
1108 	case DL_UNITDATA_REQ:
1109 		return dl_unit_data(mgr, skb);
1110 	}
1111 	if (!ret)
1112 		dev_kfree_skb(skb);
1113 	return ret;
1114 }
1115 
1116 static int
1117 free_teimanager(struct manager *mgr)
1118 {
1119 	struct layer2	*l2, *nl2;
1120 
1121 	test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1122 	if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1123 		/* not locked lock is taken in release tei */
1124 		mgr->up = NULL;
1125 		if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
1126 			list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1127 				put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
1128 				mutex_lock(&mgr->ch.st->lmutex);
1129 				list_del(&l2->ch.list);
1130 				mutex_unlock(&mgr->ch.st->lmutex);
1131 				l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1132 			}
1133 			test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1134 		} else {
1135 			list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1136 				l2->up = NULL;
1137 			}
1138 		}
1139 	}
1140 	if (test_bit(MGR_OPT_USER, &mgr->options)) {
1141 		if (list_empty(&mgr->layer2))
1142 			test_and_clear_bit(MGR_OPT_USER, &mgr->options);
1143 	}
1144 	mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1145 	return 0;
1146 }
1147 
1148 static int
1149 ctrl_teimanager(struct manager *mgr, void *arg)
1150 {
1151 	/* currently we only have one option */
1152 	int	*val = (int *)arg;
1153 	int	ret = 0;
1154 
1155 	switch (val[0]) {
1156 	case IMCLEAR_L2:
1157 		if (val[1])
1158 			test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1159 		else
1160 			test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1161 		break;
1162 	case IMHOLD_L1:
1163 		if (val[1])
1164 			test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
1165 		else
1166 			test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1167 		break;
1168 	default:
1169 		ret = -EINVAL;
1170 	}
1171 	return ret;
1172 }
1173 
1174 /* This function does create a L2 for fixed TEI in NT Mode */
1175 static int
1176 check_data(struct manager *mgr, struct sk_buff *skb)
1177 {
1178 	struct mISDNhead	*hh =  mISDN_HEAD_P(skb);
1179 	int			ret, tei, sapi;
1180 	struct layer2		*l2;
1181 
1182 	if (*debug & DEBUG_L2_CTRL)
1183 		printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1184 		    __func__, hh->prim, hh->id);
1185 	if (test_bit(MGR_OPT_USER, &mgr->options))
1186 		return -ENOTCONN;
1187 	if (hh->prim != PH_DATA_IND)
1188 		return -ENOTCONN;
1189 	if (skb->len != 3)
1190 		return -ENOTCONN;
1191 	if (skb->data[0] & 3) /* EA0 and CR must be  0 */
1192 		return -EINVAL;
1193 	sapi = skb->data[0] >> 2;
1194 	if (!(skb->data[1] & 1)) /* invalid EA1 */
1195 		return -EINVAL;
1196 	tei = skb->data[1] >> 1;
1197 	if (tei > 63) /* not a fixed tei */
1198 		return -ENOTCONN;
1199 	if ((skb->data[2] & ~0x10) != SABME)
1200 		return -ENOTCONN;
1201 	/* We got a SABME for a fixed TEI */
1202 	if (*debug & DEBUG_L2_CTRL)
1203 		printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n",
1204 		    __func__, sapi, tei);
1205 	l2 = create_new_tei(mgr, tei, sapi);
1206 	if (!l2) {
1207 		if (*debug & DEBUG_L2_CTRL)
1208 			printk(KERN_DEBUG "%s: failed to create new tei\n",
1209 			    __func__);
1210 		return -ENOMEM;
1211 	}
1212 	ret = l2->ch.send(&l2->ch, skb);
1213 	return ret;
1214 }
1215 
1216 void
1217 delete_teimanager(struct mISDNchannel *ch)
1218 {
1219 	struct manager	*mgr;
1220 	struct layer2	*l2, *nl2;
1221 
1222 	mgr = container_of(ch, struct manager, ch);
1223 	/* not locked lock is taken in release tei */
1224 	list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1225 		mutex_lock(&mgr->ch.st->lmutex);
1226 		list_del(&l2->ch.list);
1227 		mutex_unlock(&mgr->ch.st->lmutex);
1228 		l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1229 	}
1230 	list_del(&mgr->ch.list);
1231 	list_del(&mgr->bcast.list);
1232 	skb_queue_purge(&mgr->sendq);
1233 	kfree(mgr);
1234 }
1235 
1236 static int
1237 mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1238 {
1239 	struct manager	*mgr;
1240 	int		ret = -EINVAL;
1241 
1242 	mgr = container_of(ch, struct manager, ch);
1243 	if (*debug & DEBUG_L2_CTRL)
1244 		printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
1245 	switch (cmd) {
1246 	case OPEN_CHANNEL:
1247 		ret = create_teimgr(mgr, arg);
1248 		break;
1249 	case CLOSE_CHANNEL:
1250 		ret = free_teimanager(mgr);
1251 		break;
1252 	case CONTROL_CHANNEL:
1253 		ret = ctrl_teimanager(mgr, arg);
1254 		break;
1255 	case CHECK_DATA:
1256 		ret = check_data(mgr, arg);
1257 		break;
1258 	}
1259 	return ret;
1260 }
1261 
1262 static int
1263 mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1264 {
1265 	struct manager		*mgr = container_of(ch, struct manager, bcast);
1266 	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
1267 	struct sk_buff		*cskb = NULL;
1268 	struct layer2		*l2;
1269 	u_long			flags;
1270 	int			ret;
1271 
1272 	read_lock_irqsave(&mgr->lock, flags);
1273 	list_for_each_entry(l2, &mgr->layer2, list) {
1274 		if ((hh->id & MISDN_ID_SAPI_MASK) ==
1275 		    (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
1276 			if (list_is_last(&l2->list, &mgr->layer2)) {
1277 				cskb = skb;
1278 				skb = NULL;
1279 			} else {
1280 				if (!cskb)
1281 					cskb = skb_copy(skb, GFP_KERNEL);
1282 			}
1283 			if (cskb) {
1284 				ret = l2->ch.send(&l2->ch, cskb);
1285 				if (ret) {
1286 					if (*debug & DEBUG_SEND_ERR)
1287 						printk(KERN_DEBUG
1288 						    "%s ch%d prim(%x) addr(%x)"
1289 						    " err %d\n",
1290 						    __func__, l2->ch.nr,
1291 						    hh->prim, l2->ch.addr, ret);
1292 				} else
1293 					cskb = NULL;
1294 			} else {
1295 				printk(KERN_WARNING "%s ch%d addr %x no mem\n",
1296 				    __func__, ch->nr, ch->addr);
1297 				goto out;
1298 			}
1299 		}
1300 	}
1301 out:
1302 	read_unlock_irqrestore(&mgr->lock, flags);
1303 	if (cskb)
1304 		dev_kfree_skb(cskb);
1305 	if (skb)
1306 		dev_kfree_skb(skb);
1307 	return 0;
1308 }
1309 
1310 static int
1311 mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1312 {
1313 
1314 	return -EINVAL;
1315 }
1316 
1317 int
1318 create_teimanager(struct mISDNdevice *dev)
1319 {
1320 	struct manager *mgr;
1321 
1322 	mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1323 	if (!mgr)
1324 		return -ENOMEM;
1325 	INIT_LIST_HEAD(&mgr->layer2);
1326 	rwlock_init(&mgr->lock);
1327 	skb_queue_head_init(&mgr->sendq);
1328 	mgr->nextid = 1;
1329 	mgr->lastid = MISDN_ID_NONE;
1330 	mgr->ch.send = mgr_send;
1331 	mgr->ch.ctrl = mgr_ctrl;
1332 	mgr->ch.st = dev->D.st;
1333 	set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
1334 	add_layer2(&mgr->ch, dev->D.st);
1335 	mgr->bcast.send = mgr_bcast;
1336 	mgr->bcast.ctrl = mgr_bcast_ctrl;
1337 	mgr->bcast.st = dev->D.st;
1338 	set_channel_address(&mgr->bcast, 0, GROUP_TEI);
1339 	add_layer2(&mgr->bcast, dev->D.st);
1340 	mgr->deact.debug = *debug & DEBUG_MANAGER;
1341 	mgr->deact.userdata = mgr;
1342 	mgr->deact.printdebug = da_debug;
1343 	mgr->deact.fsm = &deactfsm;
1344 	mgr->deact.state = ST_L1_DEACT;
1345 	mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
1346 	dev->teimgr = &mgr->ch;
1347 	return 0;
1348 }
1349 
1350 int TEIInit(u_int *deb)
1351 {
1352 	debug = deb;
1353 	teifsmu.state_count = TEI_STATE_COUNT;
1354 	teifsmu.event_count = TEI_EVENT_COUNT;
1355 	teifsmu.strEvent = strTeiEvent;
1356 	teifsmu.strState = strTeiState;
1357 	mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
1358 	teifsmn.state_count = TEI_STATE_COUNT;
1359 	teifsmn.event_count = TEI_EVENT_COUNT;
1360 	teifsmn.strEvent = strTeiEvent;
1361 	teifsmn.strState = strTeiState;
1362 	mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
1363 	deactfsm.state_count =  DEACT_STATE_COUNT;
1364 	deactfsm.event_count = DEACT_EVENT_COUNT;
1365 	deactfsm.strEvent = strDeactEvent;
1366 	deactfsm.strState = strDeactState;
1367 	mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
1368 	return 0;
1369 }
1370 
1371 void TEIFree(void)
1372 {
1373 	mISDN_FsmFree(&teifsmu);
1374 	mISDN_FsmFree(&teifsmn);
1375 	mISDN_FsmFree(&deactfsm);
1376 }
1377