1 /* $Id: capidrv.c,v 1.1.2.2 2004/01/12 23:17:24 keil Exp $
2  *
3  * ISDN4Linux Driver, using capi20 interface (kernelcapi)
4  *
5  * Copyright 1997 by Carsten Paeth <calle@calle.de>
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11 
12 #include <linux/compiler.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/major.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
19 #include <linux/fs.h>
20 #include <linux/signal.h>
21 #include <linux/mm.h>
22 #include <linux/timer.h>
23 #include <linux/wait.h>
24 #include <linux/skbuff.h>
25 #include <linux/isdn.h>
26 #include <linux/isdnif.h>
27 #include <linux/proc_fs.h>
28 #include <linux/seq_file.h>
29 #include <linux/capi.h>
30 #include <linux/kernelcapi.h>
31 #include <linux/ctype.h>
32 #include <linux/init.h>
33 #include <linux/moduleparam.h>
34 
35 #include <linux/isdn/capiutil.h>
36 #include <linux/isdn/capicmd.h>
37 #include "capidrv.h"
38 
39 static int debugmode = 0;
40 
41 MODULE_DESCRIPTION("CAPI4Linux: Interface to ISDN4Linux");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
44 module_param(debugmode, uint, S_IRUGO | S_IWUSR);
45 
46 /* -------- type definitions ----------------------------------------- */
47 
48 
49 struct capidrv_contr {
50 
51 	struct capidrv_contr *next;
52 	struct module *owner;
53 	u32 contrnr;
54 	char name[20];
55 
56 	/*
57 	 * for isdn4linux
58 	 */
59 	isdn_if interface;
60 	int myid;
61 
62 	/*
63 	 * LISTEN state
64 	 */
65 	int state;
66 	u32 cipmask;
67 	u32 cipmask2;
68 	struct timer_list listentimer;
69 
70 	/*
71 	 * ID of capi message sent
72 	 */
73 	u16 msgid;
74 
75 	/*
76 	 * B-Channels
77 	 */
78 	int nbchan;
79 	struct capidrv_bchan {
80 		struct capidrv_contr *contr;
81 		u8 msn[ISDN_MSNLEN];
82 		int l2;
83 		int l3;
84 		u8 num[ISDN_MSNLEN];
85 		u8 mynum[ISDN_MSNLEN];
86 		int si1;
87 		int si2;
88 		int incoming;
89 		int disconnecting;
90 		struct capidrv_plci {
91 			struct capidrv_plci *next;
92 			u32 plci;
93 			u32 ncci;	/* ncci for CONNECT_ACTIVE_IND */
94 			u16 msgid;	/* to identfy CONNECT_CONF */
95 			int chan;
96 			int state;
97 			int leasedline;
98 			struct capidrv_ncci {
99 				struct capidrv_ncci *next;
100 				struct capidrv_plci *plcip;
101 				u32 ncci;
102 				u16 msgid;	/* to identfy CONNECT_B3_CONF */
103 				int chan;
104 				int state;
105 				int oldstate;
106 				/* */
107 				u16 datahandle;
108 				struct ncci_datahandle_queue {
109 					struct ncci_datahandle_queue *next;
110 					u16                         datahandle;
111 					int                           len;
112 				} *ackqueue;
113 			} *ncci_list;
114 		} *plcip;
115 		struct capidrv_ncci *nccip;
116 	} *bchans;
117 
118 	struct capidrv_plci *plci_list;
119 
120 	/* for q931 data */
121 	u8  q931_buf[4096];
122 	u8 *q931_read;
123 	u8 *q931_write;
124 	u8 *q931_end;
125 };
126 
127 
128 struct capidrv_data {
129 	struct capi20_appl ap;
130 	int ncontr;
131 	struct capidrv_contr *contr_list;
132 };
133 
134 typedef struct capidrv_plci capidrv_plci;
135 typedef struct capidrv_ncci capidrv_ncci;
136 typedef struct capidrv_contr capidrv_contr;
137 typedef struct capidrv_data capidrv_data;
138 typedef struct capidrv_bchan capidrv_bchan;
139 
140 /* -------- data definitions ----------------------------------------- */
141 
142 static capidrv_data global;
143 static DEFINE_SPINLOCK(global_lock);
144 
145 static void handle_dtrace_data(capidrv_contr *card,
146 			       int send, int level2, u8 *data, u16 len);
147 
148 /* -------- convert functions ---------------------------------------- */
149 
b1prot(int l2,int l3)150 static inline u32 b1prot(int l2, int l3)
151 {
152 	switch (l2) {
153 	case ISDN_PROTO_L2_X75I:
154 	case ISDN_PROTO_L2_X75UI:
155 	case ISDN_PROTO_L2_X75BUI:
156 		return 0;
157 	case ISDN_PROTO_L2_HDLC:
158 	default:
159 		return 0;
160 	case ISDN_PROTO_L2_TRANS:
161 		return 1;
162 	case ISDN_PROTO_L2_V11096:
163 	case ISDN_PROTO_L2_V11019:
164 	case ISDN_PROTO_L2_V11038:
165 		return 2;
166 	case ISDN_PROTO_L2_FAX:
167 		return 4;
168 	case ISDN_PROTO_L2_MODEM:
169 		return 8;
170 	}
171 }
172 
b2prot(int l2,int l3)173 static inline u32 b2prot(int l2, int l3)
174 {
175 	switch (l2) {
176 	case ISDN_PROTO_L2_X75I:
177 	case ISDN_PROTO_L2_X75UI:
178 	case ISDN_PROTO_L2_X75BUI:
179 	default:
180 		return 0;
181 	case ISDN_PROTO_L2_HDLC:
182 	case ISDN_PROTO_L2_TRANS:
183 	case ISDN_PROTO_L2_V11096:
184 	case ISDN_PROTO_L2_V11019:
185 	case ISDN_PROTO_L2_V11038:
186 	case ISDN_PROTO_L2_MODEM:
187 		return 1;
188 	case ISDN_PROTO_L2_FAX:
189 		return 4;
190 	}
191 }
192 
b3prot(int l2,int l3)193 static inline u32 b3prot(int l2, int l3)
194 {
195 	switch (l2) {
196 	case ISDN_PROTO_L2_X75I:
197 	case ISDN_PROTO_L2_X75UI:
198 	case ISDN_PROTO_L2_X75BUI:
199 	case ISDN_PROTO_L2_HDLC:
200 	case ISDN_PROTO_L2_TRANS:
201 	case ISDN_PROTO_L2_V11096:
202 	case ISDN_PROTO_L2_V11019:
203 	case ISDN_PROTO_L2_V11038:
204 	case ISDN_PROTO_L2_MODEM:
205 	default:
206 		return 0;
207 	case ISDN_PROTO_L2_FAX:
208 		return 4;
209 	}
210 }
211 
b1config_async_v110(u16 rate)212 static _cstruct b1config_async_v110(u16 rate)
213 {
214 	/* CAPI-Spec "B1 Configuration" */
215 	static unsigned char buf[9];
216 	buf[0] = 8; /* len */
217 	/* maximum bitrate */
218 	buf[1] = rate & 0xff; buf[2] = (rate >> 8) & 0xff;
219 	buf[3] = 8; buf[4] = 0; /* 8 bits per character */
220 	buf[5] = 0; buf[6] = 0; /* parity none */
221 	buf[7] = 0; buf[8] = 0; /* 1 stop bit */
222 	return buf;
223 }
224 
b1config(int l2,int l3)225 static _cstruct b1config(int l2, int l3)
226 {
227 	switch (l2) {
228 	case ISDN_PROTO_L2_X75I:
229 	case ISDN_PROTO_L2_X75UI:
230 	case ISDN_PROTO_L2_X75BUI:
231 	case ISDN_PROTO_L2_HDLC:
232 	case ISDN_PROTO_L2_TRANS:
233 	default:
234 		return NULL;
235 	case ISDN_PROTO_L2_V11096:
236 		return b1config_async_v110(9600);
237 	case ISDN_PROTO_L2_V11019:
238 		return b1config_async_v110(19200);
239 	case ISDN_PROTO_L2_V11038:
240 		return b1config_async_v110(38400);
241 	}
242 }
243 
si2cip(u8 si1,u8 si2)244 static inline u16 si2cip(u8 si1, u8 si2)
245 {
246 	static const u8 cip[17][5] =
247 		{
248 			/*  0  1  2  3  4  */
249 			{0, 0, 0, 0, 0},	/*0 */
250 			{16, 16, 4, 26, 16},	/*1 */
251 			{17, 17, 17, 4, 4},	/*2 */
252 			{2, 2, 2, 2, 2},	/*3 */
253 			{18, 18, 18, 18, 18},	/*4 */
254 			{2, 2, 2, 2, 2},	/*5 */
255 			{0, 0, 0, 0, 0},	/*6 */
256 			{2, 2, 2, 2, 2},	/*7 */
257 			{2, 2, 2, 2, 2},	/*8 */
258 			{21, 21, 21, 21, 21},	/*9 */
259 			{19, 19, 19, 19, 19},	/*10 */
260 			{0, 0, 0, 0, 0},	/*11 */
261 			{0, 0, 0, 0, 0},	/*12 */
262 			{0, 0, 0, 0, 0},	/*13 */
263 			{0, 0, 0, 0, 0},	/*14 */
264 			{22, 22, 22, 22, 22},	/*15 */
265 			{27, 27, 27, 28, 27}	/*16 */
266 		};
267 	if (si1 > 16)
268 		si1 = 0;
269 	if (si2 > 4)
270 		si2 = 0;
271 
272 	return (u16) cip[si1][si2];
273 }
274 
cip2si1(u16 cipval)275 static inline u8 cip2si1(u16 cipval)
276 {
277 	static const u8 si[32] =
278 		{7, 1, 7, 7, 1, 1, 7, 7,	/*0-7 */
279 		 7, 1, 0, 0, 0, 0, 0, 0,	/*8-15 */
280 		 1, 2, 4, 10, 9, 9, 15, 7,	/*16-23 */
281 		 7, 7, 1, 16, 16, 0, 0, 0};	/*24-31 */
282 
283 	if (cipval > 31)
284 		cipval = 0;	/* .... */
285 	return si[cipval];
286 }
287 
cip2si2(u16 cipval)288 static inline u8 cip2si2(u16 cipval)
289 {
290 	static const u8 si[32] =
291 		{0, 0, 0, 0, 2, 3, 0, 0,	/*0-7 */
292 		 0, 3, 0, 0, 0, 0, 0, 0,	/*8-15 */
293 		 1, 2, 0, 0, 9, 0, 0, 0,	/*16-23 */
294 		 0, 0, 3, 2, 3, 0, 0, 0};	/*24-31 */
295 
296 	if (cipval > 31)
297 		cipval = 0;	/* .... */
298 	return si[cipval];
299 }
300 
301 
302 /* -------- controller management ------------------------------------- */
303 
findcontrbydriverid(int driverid)304 static inline capidrv_contr *findcontrbydriverid(int driverid)
305 {
306 	unsigned long flags;
307 	capidrv_contr *p;
308 
309 	spin_lock_irqsave(&global_lock, flags);
310 	for (p = global.contr_list; p; p = p->next)
311 		if (p->myid == driverid)
312 			break;
313 	spin_unlock_irqrestore(&global_lock, flags);
314 	return p;
315 }
316 
findcontrbynumber(u32 contr)317 static capidrv_contr *findcontrbynumber(u32 contr)
318 {
319 	unsigned long flags;
320 	capidrv_contr *p = global.contr_list;
321 
322 	spin_lock_irqsave(&global_lock, flags);
323 	for (p = global.contr_list; p; p = p->next)
324 		if (p->contrnr == contr)
325 			break;
326 	spin_unlock_irqrestore(&global_lock, flags);
327 	return p;
328 }
329 
330 
331 /* -------- plci management ------------------------------------------ */
332 
new_plci(capidrv_contr * card,int chan)333 static capidrv_plci *new_plci(capidrv_contr *card, int chan)
334 {
335 	capidrv_plci *plcip;
336 
337 	plcip = kzalloc(sizeof(capidrv_plci), GFP_ATOMIC);
338 
339 	if (plcip == NULL)
340 		return NULL;
341 
342 	plcip->state = ST_PLCI_NONE;
343 	plcip->plci = 0;
344 	plcip->msgid = 0;
345 	plcip->chan = chan;
346 	plcip->next = card->plci_list;
347 	card->plci_list = plcip;
348 	card->bchans[chan].plcip = plcip;
349 
350 	return plcip;
351 }
352 
find_plci_by_plci(capidrv_contr * card,u32 plci)353 static capidrv_plci *find_plci_by_plci(capidrv_contr *card, u32 plci)
354 {
355 	capidrv_plci *p;
356 	for (p = card->plci_list; p; p = p->next)
357 		if (p->plci == plci)
358 			return p;
359 	return NULL;
360 }
361 
find_plci_by_msgid(capidrv_contr * card,u16 msgid)362 static capidrv_plci *find_plci_by_msgid(capidrv_contr *card, u16 msgid)
363 {
364 	capidrv_plci *p;
365 	for (p = card->plci_list; p; p = p->next)
366 		if (p->msgid == msgid)
367 			return p;
368 	return NULL;
369 }
370 
find_plci_by_ncci(capidrv_contr * card,u32 ncci)371 static capidrv_plci *find_plci_by_ncci(capidrv_contr *card, u32 ncci)
372 {
373 	capidrv_plci *p;
374 	for (p = card->plci_list; p; p = p->next)
375 		if (p->plci == (ncci & 0xffff))
376 			return p;
377 	return NULL;
378 }
379 
free_plci(capidrv_contr * card,capidrv_plci * plcip)380 static void free_plci(capidrv_contr *card, capidrv_plci *plcip)
381 {
382 	capidrv_plci **pp;
383 
384 	for (pp = &card->plci_list; *pp; pp = &(*pp)->next) {
385 		if (*pp == plcip) {
386 			*pp = (*pp)->next;
387 			card->bchans[plcip->chan].plcip = NULL;
388 			card->bchans[plcip->chan].disconnecting = 0;
389 			card->bchans[plcip->chan].incoming = 0;
390 			kfree(plcip);
391 			return;
392 		}
393 	}
394 	printk(KERN_ERR "capidrv-%d: free_plci %p (0x%x) not found, Huh?\n",
395 	       card->contrnr, plcip, plcip->plci);
396 }
397 
398 /* -------- ncci management ------------------------------------------ */
399 
new_ncci(capidrv_contr * card,capidrv_plci * plcip,u32 ncci)400 static inline capidrv_ncci *new_ncci(capidrv_contr *card,
401 				     capidrv_plci *plcip,
402 				     u32 ncci)
403 {
404 	capidrv_ncci *nccip;
405 
406 	nccip = kzalloc(sizeof(capidrv_ncci), GFP_ATOMIC);
407 
408 	if (nccip == NULL)
409 		return NULL;
410 
411 	nccip->ncci = ncci;
412 	nccip->state = ST_NCCI_NONE;
413 	nccip->plcip = plcip;
414 	nccip->chan = plcip->chan;
415 	nccip->datahandle = 0;
416 
417 	nccip->next = plcip->ncci_list;
418 	plcip->ncci_list = nccip;
419 
420 	card->bchans[plcip->chan].nccip = nccip;
421 
422 	return nccip;
423 }
424 
find_ncci(capidrv_contr * card,u32 ncci)425 static inline capidrv_ncci *find_ncci(capidrv_contr *card, u32 ncci)
426 {
427 	capidrv_plci *plcip;
428 	capidrv_ncci *p;
429 
430 	if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
431 		return NULL;
432 
433 	for (p = plcip->ncci_list; p; p = p->next)
434 		if (p->ncci == ncci)
435 			return p;
436 	return NULL;
437 }
438 
find_ncci_by_msgid(capidrv_contr * card,u32 ncci,u16 msgid)439 static inline capidrv_ncci *find_ncci_by_msgid(capidrv_contr *card,
440 					       u32 ncci, u16 msgid)
441 {
442 	capidrv_plci *plcip;
443 	capidrv_ncci *p;
444 
445 	if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
446 		return NULL;
447 
448 	for (p = plcip->ncci_list; p; p = p->next)
449 		if (p->msgid == msgid)
450 			return p;
451 	return NULL;
452 }
453 
free_ncci(capidrv_contr * card,struct capidrv_ncci * nccip)454 static void free_ncci(capidrv_contr *card, struct capidrv_ncci *nccip)
455 {
456 	struct capidrv_ncci **pp;
457 
458 	for (pp = &(nccip->plcip->ncci_list); *pp; pp = &(*pp)->next) {
459 		if (*pp == nccip) {
460 			*pp = (*pp)->next;
461 			break;
462 		}
463 	}
464 	card->bchans[nccip->chan].nccip = NULL;
465 	kfree(nccip);
466 }
467 
capidrv_add_ack(struct capidrv_ncci * nccip,u16 datahandle,int len)468 static int capidrv_add_ack(struct capidrv_ncci *nccip,
469 			   u16 datahandle, int len)
470 {
471 	struct ncci_datahandle_queue *n, **pp;
472 
473 	n = kmalloc(sizeof(struct ncci_datahandle_queue), GFP_ATOMIC);
474 	if (!n) {
475 		printk(KERN_ERR "capidrv: kmalloc ncci_datahandle failed\n");
476 		return -1;
477 	}
478 	n->next = NULL;
479 	n->datahandle = datahandle;
480 	n->len = len;
481 	for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next);
482 	*pp = n;
483 	return 0;
484 }
485 
capidrv_del_ack(struct capidrv_ncci * nccip,u16 datahandle)486 static int capidrv_del_ack(struct capidrv_ncci *nccip, u16 datahandle)
487 {
488 	struct ncci_datahandle_queue **pp, *p;
489 	int len;
490 
491 	for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) {
492 		if ((*pp)->datahandle == datahandle) {
493 			p = *pp;
494 			len = p->len;
495 			*pp = (*pp)->next;
496 			kfree(p);
497 			return len;
498 		}
499 	}
500 	return -1;
501 }
502 
503 /* -------- convert and send capi message ---------------------------- */
504 
send_message(capidrv_contr * card,_cmsg * cmsg)505 static void send_message(capidrv_contr *card, _cmsg *cmsg)
506 {
507 	struct sk_buff *skb;
508 	size_t len;
509 
510 	if (capi_cmsg2message(cmsg, cmsg->buf)) {
511 		printk(KERN_ERR "capidrv::send_message: parser failure\n");
512 		return;
513 	}
514 	len = CAPIMSG_LEN(cmsg->buf);
515 	skb = alloc_skb(len, GFP_ATOMIC);
516 	if (!skb) {
517 		printk(KERN_ERR "capidrv::send_message: can't allocate mem\n");
518 		return;
519 	}
520 	skb_put_data(skb, cmsg->buf, len);
521 	if (capi20_put_message(&global.ap, skb) != CAPI_NOERROR)
522 		kfree_skb(skb);
523 }
524 
525 /* -------- state machine -------------------------------------------- */
526 
527 struct listenstatechange {
528 	int actstate;
529 	int nextstate;
530 	int event;
531 };
532 
533 static struct listenstatechange listentable[] =
534 {
535 	{ST_LISTEN_NONE, ST_LISTEN_WAIT_CONF, EV_LISTEN_REQ},
536 	{ST_LISTEN_ACTIVE, ST_LISTEN_ACTIVE_WAIT_CONF, EV_LISTEN_REQ},
537 	{ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_ERROR},
538 	{ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_ERROR},
539 	{ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
540 	{ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
541 	{ST_LISTEN_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
542 	{ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
543 	{},
544 };
545 
listen_change_state(capidrv_contr * card,int event)546 static void listen_change_state(capidrv_contr *card, int event)
547 {
548 	struct listenstatechange *p = listentable;
549 	while (p->event) {
550 		if (card->state == p->actstate && p->event == event) {
551 			if (debugmode)
552 				printk(KERN_DEBUG "capidrv-%d: listen_change_state %d -> %d\n",
553 				       card->contrnr, card->state, p->nextstate);
554 			card->state = p->nextstate;
555 			return;
556 		}
557 		p++;
558 	}
559 	printk(KERN_ERR "capidrv-%d: listen_change_state state=%d event=%d ????\n",
560 	       card->contrnr, card->state, event);
561 
562 }
563 
564 /* ------------------------------------------------------------------ */
565 
p0(capidrv_contr * card,capidrv_plci * plci)566 static void p0(capidrv_contr *card, capidrv_plci *plci)
567 {
568 	isdn_ctrl cmd;
569 
570 	card->bchans[plci->chan].contr = NULL;
571 	cmd.command = ISDN_STAT_DHUP;
572 	cmd.driver = card->myid;
573 	cmd.arg = plci->chan;
574 	card->interface.statcallb(&cmd);
575 	free_plci(card, plci);
576 }
577 
578 /* ------------------------------------------------------------------ */
579 
580 struct plcistatechange {
581 	int actstate;
582 	int nextstate;
583 	int event;
584 	void (*changefunc)(capidrv_contr *card, capidrv_plci *plci);
585 };
586 
587 static struct plcistatechange plcitable[] =
588 {
589 	/* P-0 */
590 	{ST_PLCI_NONE, ST_PLCI_OUTGOING, EV_PLCI_CONNECT_REQ, NULL},
591 	{ST_PLCI_NONE, ST_PLCI_ALLOCATED, EV_PLCI_FACILITY_IND_UP, NULL},
592 	{ST_PLCI_NONE, ST_PLCI_INCOMING, EV_PLCI_CONNECT_IND, NULL},
593 	{ST_PLCI_NONE, ST_PLCI_RESUMEING, EV_PLCI_RESUME_REQ, NULL},
594 	/* P-0.1 */
595 	{ST_PLCI_OUTGOING, ST_PLCI_NONE, EV_PLCI_CONNECT_CONF_ERROR, p0},
596 	{ST_PLCI_OUTGOING, ST_PLCI_ALLOCATED, EV_PLCI_CONNECT_CONF_OK, NULL},
597 	/* P-1 */
598 	{ST_PLCI_ALLOCATED, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
599 	{ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
600 	{ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
601 	{ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
602 	/* P-ACT */
603 	{ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
604 	{ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
605 	{ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
606 	{ST_PLCI_ACTIVE, ST_PLCI_HELD, EV_PLCI_HOLD_IND, NULL},
607 	{ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_SUSPEND_IND, NULL},
608 	/* P-2 */
609 	{ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
610 	{ST_PLCI_INCOMING, ST_PLCI_FACILITY_IND, EV_PLCI_FACILITY_IND_UP, NULL},
611 	{ST_PLCI_INCOMING, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_RESP, NULL},
612 	{ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
613 	{ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
614 	{ST_PLCI_INCOMING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
615 	{ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CD_IND, NULL},
616 	/* P-3 */
617 	{ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
618 	{ST_PLCI_FACILITY_IND, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
619 	{ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
620 	{ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
621 	{ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
622 	/* P-4 */
623 	{ST_PLCI_ACCEPTING, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
624 	{ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
625 	{ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
626 	{ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
627 	/* P-5 */
628 	{ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
629 	/* P-6 */
630 	{ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0},
631 	/* P-0.Res */
632 	{ST_PLCI_RESUMEING, ST_PLCI_NONE, EV_PLCI_RESUME_CONF_ERROR, p0},
633 	{ST_PLCI_RESUMEING, ST_PLCI_RESUME, EV_PLCI_RESUME_CONF_OK, NULL},
634 	/* P-RES */
635 	{ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, NULL},
636 	/* P-HELD */
637 	{ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, NULL},
638 	{},
639 };
640 
plci_change_state(capidrv_contr * card,capidrv_plci * plci,int event)641 static void plci_change_state(capidrv_contr *card, capidrv_plci *plci, int event)
642 {
643 	struct plcistatechange *p = plcitable;
644 	while (p->event) {
645 		if (plci->state == p->actstate && p->event == event) {
646 			if (debugmode)
647 				printk(KERN_DEBUG "capidrv-%d: plci_change_state:0x%x %d -> %d\n",
648 				       card->contrnr, plci->plci, plci->state, p->nextstate);
649 			plci->state = p->nextstate;
650 			if (p->changefunc)
651 				p->changefunc(card, plci);
652 			return;
653 		}
654 		p++;
655 	}
656 	printk(KERN_ERR "capidrv-%d: plci_change_state:0x%x state=%d event=%d ????\n",
657 	       card->contrnr, plci->plci, plci->state, event);
658 }
659 
660 /* ------------------------------------------------------------------ */
661 
662 static _cmsg cmsg;
663 
n0(capidrv_contr * card,capidrv_ncci * ncci)664 static void n0(capidrv_contr *card, capidrv_ncci *ncci)
665 {
666 	isdn_ctrl cmd;
667 
668 	capi_fill_DISCONNECT_REQ(&cmsg,
669 				 global.ap.applid,
670 				 card->msgid++,
671 				 ncci->plcip->plci,
672 				 NULL,	/* BChannelinformation */
673 				 NULL,	/* Keypadfacility */
674 				 NULL,	/* Useruserdata */   /* $$$$ */
675 				 NULL	/* Facilitydataarray */
676 		);
677 	plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ);
678 	send_message(card, &cmsg);
679 
680 	cmd.command = ISDN_STAT_BHUP;
681 	cmd.driver = card->myid;
682 	cmd.arg = ncci->chan;
683 	card->interface.statcallb(&cmd);
684 	free_ncci(card, ncci);
685 }
686 
687 /* ------------------------------------------------------------------ */
688 
689 struct nccistatechange {
690 	int actstate;
691 	int nextstate;
692 	int event;
693 	void (*changefunc)(capidrv_contr *card, capidrv_ncci *ncci);
694 };
695 
696 static struct nccistatechange nccitable[] =
697 {
698 	/* N-0 */
699 	{ST_NCCI_NONE, ST_NCCI_OUTGOING, EV_NCCI_CONNECT_B3_REQ, NULL},
700 	{ST_NCCI_NONE, ST_NCCI_INCOMING, EV_NCCI_CONNECT_B3_IND, NULL},
701 	/* N-0.1 */
702 	{ST_NCCI_OUTGOING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_CONF_OK, NULL},
703 	{ST_NCCI_OUTGOING, ST_NCCI_NONE, EV_NCCI_CONNECT_B3_CONF_ERROR, n0},
704 	/* N-1 */
705 	{ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_CONNECT_B3_REJECT, NULL},
706 	{ST_NCCI_INCOMING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_RESP, NULL},
707 	{ST_NCCI_INCOMING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
708 	{ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
709 	/* N-2 */
710 	{ST_NCCI_ALLOCATED, ST_NCCI_ACTIVE, EV_NCCI_CONNECT_B3_ACTIVE_IND, NULL},
711 	{ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
712 	{ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
713 	/* N-ACT */
714 	{ST_NCCI_ACTIVE, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
715 	{ST_NCCI_ACTIVE, ST_NCCI_RESETING, EV_NCCI_RESET_B3_REQ, NULL},
716 	{ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
717 	{ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
718 	/* N-3 */
719 	{ST_NCCI_RESETING, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
720 	{ST_NCCI_RESETING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
721 	{ST_NCCI_RESETING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
722 	/* N-4 */
723 	{ST_NCCI_DISCONNECTING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
724 	{ST_NCCI_DISCONNECTING, ST_NCCI_PREVIOUS, EV_NCCI_DISCONNECT_B3_CONF_ERROR, NULL},
725 	/* N-5 */
726 	{ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0},
727 	{},
728 };
729 
ncci_change_state(capidrv_contr * card,capidrv_ncci * ncci,int event)730 static void ncci_change_state(capidrv_contr *card, capidrv_ncci *ncci, int event)
731 {
732 	struct nccistatechange *p = nccitable;
733 	while (p->event) {
734 		if (ncci->state == p->actstate && p->event == event) {
735 			if (debugmode)
736 				printk(KERN_DEBUG "capidrv-%d: ncci_change_state:0x%x %d -> %d\n",
737 				       card->contrnr, ncci->ncci, ncci->state, p->nextstate);
738 			if (p->nextstate == ST_NCCI_PREVIOUS) {
739 				ncci->state = ncci->oldstate;
740 				ncci->oldstate = p->actstate;
741 			} else {
742 				ncci->oldstate = p->actstate;
743 				ncci->state = p->nextstate;
744 			}
745 			if (p->changefunc)
746 				p->changefunc(card, ncci);
747 			return;
748 		}
749 		p++;
750 	}
751 	printk(KERN_ERR "capidrv-%d: ncci_change_state:0x%x state=%d event=%d ????\n",
752 	       card->contrnr, ncci->ncci, ncci->state, event);
753 }
754 
755 /* ------------------------------------------------------------------- */
756 
new_bchan(capidrv_contr * card)757 static inline int new_bchan(capidrv_contr *card)
758 {
759 	int i;
760 	for (i = 0; i < card->nbchan; i++) {
761 		if (card->bchans[i].plcip == NULL) {
762 			card->bchans[i].disconnecting = 0;
763 			return i;
764 		}
765 	}
766 	return -1;
767 }
768 
769 /* ------------------------------------------------------------------- */
capi_info2str(u16 reason)770 static char *capi_info2str(u16 reason)
771 {
772 #ifndef CONFIG_ISDN_CAPI_CAPIDRV_VERBOSE
773 	return "..";
774 #else
775 	switch (reason) {
776 
777 /*-- informative values (corresponding message was processed) -----*/
778 	case 0x0001:
779 		return "NCPI not supported by current protocol, NCPI ignored";
780 	case 0x0002:
781 		return "Flags not supported by current protocol, flags ignored";
782 	case 0x0003:
783 		return "Alert already sent by another application";
784 
785 /*-- error information concerning CAPI_REGISTER -----*/
786 	case 0x1001:
787 		return "Too many applications";
788 	case 0x1002:
789 		return "Logical block size too small, must be at least 128 Bytes";
790 	case 0x1003:
791 		return "Buffer exceeds 64 kByte";
792 	case 0x1004:
793 		return "Message buffer size too small, must be at least 1024 Bytes";
794 	case 0x1005:
795 		return "Max. number of logical connections not supported";
796 	case 0x1006:
797 		return "Reserved";
798 	case 0x1007:
799 		return "The message could not be accepted because of an internal busy condition";
800 	case 0x1008:
801 		return "OS resource error (no memory ?)";
802 	case 0x1009:
803 		return "CAPI not installed";
804 	case 0x100A:
805 		return "Controller does not support external equipment";
806 	case 0x100B:
807 		return "Controller does only support external equipment";
808 
809 /*-- error information concerning message exchange functions -----*/
810 	case 0x1101:
811 		return "Illegal application number";
812 	case 0x1102:
813 		return "Illegal command or subcommand or message length less than 12 bytes";
814 	case 0x1103:
815 		return "The message could not be accepted because of a queue full condition !! The error code does not imply that CAPI cannot receive messages directed to another controller, PLCI or NCCI";
816 	case 0x1104:
817 		return "Queue is empty";
818 	case 0x1105:
819 		return "Queue overflow, a message was lost !! This indicates a configuration error. The only recovery from this error is to perform a CAPI_RELEASE";
820 	case 0x1106:
821 		return "Unknown notification parameter";
822 	case 0x1107:
823 		return "The Message could not be accepted because of an internal busy condition";
824 	case 0x1108:
825 		return "OS Resource error (no memory ?)";
826 	case 0x1109:
827 		return "CAPI not installed";
828 	case 0x110A:
829 		return "Controller does not support external equipment";
830 	case 0x110B:
831 		return "Controller does only support external equipment";
832 
833 /*-- error information concerning resource / coding problems -----*/
834 	case 0x2001:
835 		return "Message not supported in current state";
836 	case 0x2002:
837 		return "Illegal Controller / PLCI / NCCI";
838 	case 0x2003:
839 		return "Out of PLCI";
840 	case 0x2004:
841 		return "Out of NCCI";
842 	case 0x2005:
843 		return "Out of LISTEN";
844 	case 0x2006:
845 		return "Out of FAX resources (protocol T.30)";
846 	case 0x2007:
847 		return "Illegal message parameter coding";
848 
849 /*-- error information concerning requested services  -----*/
850 	case 0x3001:
851 		return "B1 protocol not supported";
852 	case 0x3002:
853 		return "B2 protocol not supported";
854 	case 0x3003:
855 		return "B3 protocol not supported";
856 	case 0x3004:
857 		return "B1 protocol parameter not supported";
858 	case 0x3005:
859 		return "B2 protocol parameter not supported";
860 	case 0x3006:
861 		return "B3 protocol parameter not supported";
862 	case 0x3007:
863 		return "B protocol combination not supported";
864 	case 0x3008:
865 		return "NCPI not supported";
866 	case 0x3009:
867 		return "CIP Value unknown";
868 	case 0x300A:
869 		return "Flags not supported (reserved bits)";
870 	case 0x300B:
871 		return "Facility not supported";
872 	case 0x300C:
873 		return "Data length not supported by current protocol";
874 	case 0x300D:
875 		return "Reset procedure not supported by current protocol";
876 
877 /*-- informations about the clearing of a physical connection -----*/
878 	case 0x3301:
879 		return "Protocol error layer 1 (broken line or B-channel removed by signalling protocol)";
880 	case 0x3302:
881 		return "Protocol error layer 2";
882 	case 0x3303:
883 		return "Protocol error layer 3";
884 	case 0x3304:
885 		return "Another application got that call";
886 /*-- T.30 specific reasons -----*/
887 	case 0x3311:
888 		return "Connecting not successful (remote station is no FAX G3 machine)";
889 	case 0x3312:
890 		return "Connecting not successful (training error)";
891 	case 0x3313:
892 		return "Disconnected before transfer (remote station does not support transfer mode, e.g. resolution)";
893 	case 0x3314:
894 		return "Disconnected during transfer (remote abort)";
895 	case 0x3315:
896 		return "Disconnected during transfer (remote procedure error, e.g. unsuccessful repetition of T.30 commands)";
897 	case 0x3316:
898 		return "Disconnected during transfer (local tx data underrun)";
899 	case 0x3317:
900 		return "Disconnected during transfer (local rx data overflow)";
901 	case 0x3318:
902 		return "Disconnected during transfer (local abort)";
903 	case 0x3319:
904 		return "Illegal parameter coding (e.g. SFF coding error)";
905 
906 /*-- disconnect causes from the network according to ETS 300 102-1/Q.931 -----*/
907 	case 0x3481: return "Unallocated (unassigned) number";
908 	case 0x3482: return "No route to specified transit network";
909 	case 0x3483: return "No route to destination";
910 	case 0x3486: return "Channel unacceptable";
911 	case 0x3487:
912 		return "Call awarded and being delivered in an established channel";
913 	case 0x3490: return "Normal call clearing";
914 	case 0x3491: return "User busy";
915 	case 0x3492: return "No user responding";
916 	case 0x3493: return "No answer from user (user alerted)";
917 	case 0x3495: return "Call rejected";
918 	case 0x3496: return "Number changed";
919 	case 0x349A: return "Non-selected user clearing";
920 	case 0x349B: return "Destination out of order";
921 	case 0x349C: return "Invalid number format";
922 	case 0x349D: return "Facility rejected";
923 	case 0x349E: return "Response to STATUS ENQUIRY";
924 	case 0x349F: return "Normal, unspecified";
925 	case 0x34A2: return "No circuit / channel available";
926 	case 0x34A6: return "Network out of order";
927 	case 0x34A9: return "Temporary failure";
928 	case 0x34AA: return "Switching equipment congestion";
929 	case 0x34AB: return "Access information discarded";
930 	case 0x34AC: return "Requested circuit / channel not available";
931 	case 0x34AF: return "Resources unavailable, unspecified";
932 	case 0x34B1: return "Quality of service unavailable";
933 	case 0x34B2: return "Requested facility not subscribed";
934 	case 0x34B9: return "Bearer capability not authorized";
935 	case 0x34BA: return "Bearer capability not presently available";
936 	case 0x34BF: return "Service or option not available, unspecified";
937 	case 0x34C1: return "Bearer capability not implemented";
938 	case 0x34C2: return "Channel type not implemented";
939 	case 0x34C5: return "Requested facility not implemented";
940 	case 0x34C6: return "Only restricted digital information bearer capability is available";
941 	case 0x34CF: return "Service or option not implemented, unspecified";
942 	case 0x34D1: return "Invalid call reference value";
943 	case 0x34D2: return "Identified channel does not exist";
944 	case 0x34D3: return "A suspended call exists, but this call identity does not";
945 	case 0x34D4: return "Call identity in use";
946 	case 0x34D5: return "No call suspended";
947 	case 0x34D6: return "Call having the requested call identity has been cleared";
948 	case 0x34D8: return "Incompatible destination";
949 	case 0x34DB: return "Invalid transit network selection";
950 	case 0x34DF: return "Invalid message, unspecified";
951 	case 0x34E0: return "Mandatory information element is missing";
952 	case 0x34E1: return "Message type non-existent or not implemented";
953 	case 0x34E2: return "Message not compatible with call state or message type non-existent or not implemented";
954 	case 0x34E3: return "Information element non-existent or not implemented";
955 	case 0x34E4: return "Invalid information element contents";
956 	case 0x34E5: return "Message not compatible with call state";
957 	case 0x34E6: return "Recovery on timer expiry";
958 	case 0x34EF: return "Protocol error, unspecified";
959 	case 0x34FF: return "Interworking, unspecified";
960 
961 	default: return "No additional information";
962 	}
963 #endif
964 }
965 
handle_controller(_cmsg * cmsg)966 static void handle_controller(_cmsg *cmsg)
967 {
968 	capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
969 
970 	if (!card) {
971 		printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
972 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
973 		       cmsg->adr.adrController & 0x7f);
974 		return;
975 	}
976 	switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
977 
978 	case CAPI_LISTEN_CONF:	/* Controller */
979 		if (debugmode)
980 			printk(KERN_DEBUG "capidrv-%d: listenconf Info=0x%4x (%s) cipmask=0x%x\n",
981 			       card->contrnr, cmsg->Info, capi_info2str(cmsg->Info), card->cipmask);
982 		if (cmsg->Info) {
983 			listen_change_state(card, EV_LISTEN_CONF_ERROR);
984 		} else if (card->cipmask == 0) {
985 			listen_change_state(card, EV_LISTEN_CONF_EMPTY);
986 		} else {
987 			listen_change_state(card, EV_LISTEN_CONF_OK);
988 		}
989 		break;
990 
991 	case CAPI_MANUFACTURER_IND:	/* Controller */
992 		if (cmsg->ManuID == 0x214D5641
993 		    && cmsg->Class == 0
994 		    && cmsg->Function == 1) {
995 			u8  *data = cmsg->ManuData + 3;
996 			u16  len = cmsg->ManuData[0];
997 			u16 layer;
998 			int direction;
999 			if (len == 255) {
1000 				len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
1001 				data += 2;
1002 			}
1003 			len -= 2;
1004 			layer = ((*(data - 1)) << 8) | *(data - 2);
1005 			if (layer & 0x300)
1006 				direction = (layer & 0x200) ? 0 : 1;
1007 			else direction = (layer & 0x800) ? 0 : 1;
1008 			if (layer & 0x0C00) {
1009 				if ((layer & 0xff) == 0x80) {
1010 					handle_dtrace_data(card, direction, 1, data, len);
1011 					break;
1012 				}
1013 			} else if ((layer & 0xff) < 0x80) {
1014 				handle_dtrace_data(card, direction, 0, data, len);
1015 				break;
1016 			}
1017 			printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
1018 			       card->contrnr,
1019 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1020 			       cmsg->adr.adrController, layer);
1021 			break;
1022 		}
1023 		goto ignored;
1024 	case CAPI_MANUFACTURER_CONF:	/* Controller */
1025 		if (cmsg->ManuID == 0x214D5641) {
1026 			char *s = NULL;
1027 			switch (cmsg->Class) {
1028 			case 0: break;
1029 			case 1: s = "unknown class"; break;
1030 			case 2: s = "unknown function"; break;
1031 			default: s = "unknown error"; break;
1032 			}
1033 			if (s)
1034 				printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
1035 				       card->contrnr,
1036 				       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1037 				       cmsg->adr.adrController,
1038 				       cmsg->Function, s);
1039 			break;
1040 		}
1041 		goto ignored;
1042 	case CAPI_FACILITY_IND:	/* Controller/plci/ncci */
1043 		goto ignored;
1044 	case CAPI_FACILITY_CONF:	/* Controller/plci/ncci */
1045 		goto ignored;
1046 	case CAPI_INFO_IND:	/* Controller/plci */
1047 		goto ignored;
1048 	case CAPI_INFO_CONF:	/* Controller/plci */
1049 		goto ignored;
1050 
1051 	default:
1052 		printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
1053 		       card->contrnr,
1054 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1055 		       cmsg->adr.adrController);
1056 	}
1057 	return;
1058 
1059 ignored:
1060 	printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
1061 	       card->contrnr,
1062 	       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1063 	       cmsg->adr.adrController);
1064 }
1065 
handle_incoming_call(capidrv_contr * card,_cmsg * cmsg)1066 static void handle_incoming_call(capidrv_contr *card, _cmsg *cmsg)
1067 {
1068 	capidrv_plci *plcip;
1069 	capidrv_bchan *bchan;
1070 	isdn_ctrl cmd;
1071 	int chan;
1072 
1073 	if ((chan = new_bchan(card)) == -1) {
1074 		printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
1075 		return;
1076 	}
1077 	bchan = &card->bchans[chan];
1078 	if ((plcip = new_plci(card, chan)) == NULL) {
1079 		printk(KERN_ERR "capidrv-%d: incoming call: no memory, sorry.\n", card->contrnr);
1080 		return;
1081 	}
1082 	bchan->incoming = 1;
1083 	plcip->plci = cmsg->adr.adrPLCI;
1084 	plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
1085 
1086 	cmd.command = ISDN_STAT_ICALL;
1087 	cmd.driver = card->myid;
1088 	cmd.arg = chan;
1089 	memset(&cmd.parm.setup, 0, sizeof(cmd.parm.setup));
1090 	strncpy(cmd.parm.setup.phone,
1091 		cmsg->CallingPartyNumber + 3,
1092 		cmsg->CallingPartyNumber[0] - 2);
1093 	strncpy(cmd.parm.setup.eazmsn,
1094 		cmsg->CalledPartyNumber + 2,
1095 		cmsg->CalledPartyNumber[0] - 1);
1096 	cmd.parm.setup.si1 = cip2si1(cmsg->CIPValue);
1097 	cmd.parm.setup.si2 = cip2si2(cmsg->CIPValue);
1098 	cmd.parm.setup.plan = cmsg->CallingPartyNumber[1];
1099 	cmd.parm.setup.screen = cmsg->CallingPartyNumber[2];
1100 
1101 	printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n",
1102 	       card->contrnr,
1103 	       cmd.parm.setup.phone,
1104 	       cmd.parm.setup.si1,
1105 	       cmd.parm.setup.si2,
1106 	       cmd.parm.setup.eazmsn);
1107 
1108 	if (cmd.parm.setup.si1 == 1 && cmd.parm.setup.si2 != 0) {
1109 		printk(KERN_INFO "capidrv-%d: patching si2=%d to 0 for VBOX\n",
1110 		       card->contrnr,
1111 		       cmd.parm.setup.si2);
1112 		cmd.parm.setup.si2 = 0;
1113 	}
1114 
1115 	switch (card->interface.statcallb(&cmd)) {
1116 	case 0:
1117 	case 3:
1118 		/* No device matching this call.
1119 		 * and isdn_common.c has send a HANGUP command
1120 		 * which is ignored in state ST_PLCI_INCOMING,
1121 		 * so we send RESP to ignore the call
1122 		 */
1123 		capi_cmsg_answer(cmsg);
1124 		cmsg->Reject = 1;	/* ignore */
1125 		plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
1126 		send_message(card, cmsg);
1127 		printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s ignored\n",
1128 		       card->contrnr,
1129 		       cmd.parm.setup.phone,
1130 		       cmd.parm.setup.si1,
1131 		       cmd.parm.setup.si2,
1132 		       cmd.parm.setup.eazmsn);
1133 		break;
1134 	case 1:
1135 		/* At least one device matching this call (RING on ttyI)
1136 		 * HL-driver may send ALERTING on the D-channel in this
1137 		 * case.
1138 		 * really means: RING on ttyI or a net interface
1139 		 * accepted this call already.
1140 		 *
1141 		 * If the call was accepted, state has already changed,
1142 		 * and CONNECT_RESP already sent.
1143 		 */
1144 		if (plcip->state == ST_PLCI_INCOMING) {
1145 			printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
1146 			       card->contrnr,
1147 			       cmd.parm.setup.phone,
1148 			       cmd.parm.setup.si1,
1149 			       cmd.parm.setup.si2,
1150 			       cmd.parm.setup.eazmsn);
1151 			capi_fill_ALERT_REQ(cmsg,
1152 					    global.ap.applid,
1153 					    card->msgid++,
1154 					    plcip->plci,	/* adr */
1155 					    NULL,/* BChannelinformation */
1156 					    NULL,/* Keypadfacility */
1157 					    NULL,/* Useruserdata */
1158 					    NULL /* Facilitydataarray */
1159 				);
1160 			plcip->msgid = cmsg->Messagenumber;
1161 			send_message(card, cmsg);
1162 		} else {
1163 			printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
1164 			       card->contrnr,
1165 			       cmd.parm.setup.phone,
1166 			       cmd.parm.setup.si1,
1167 			       cmd.parm.setup.si2,
1168 			       cmd.parm.setup.eazmsn);
1169 		}
1170 		break;
1171 
1172 	case 2:		/* Call will be rejected. */
1173 		capi_cmsg_answer(cmsg);
1174 		cmsg->Reject = 2;	/* reject call, normal call clearing */
1175 		plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
1176 		send_message(card, cmsg);
1177 		break;
1178 
1179 	default:
1180 		/* An error happened. (Invalid parameters for example.) */
1181 		capi_cmsg_answer(cmsg);
1182 		cmsg->Reject = 8;	/* reject call,
1183 					   destination out of order */
1184 		plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
1185 		send_message(card, cmsg);
1186 		break;
1187 	}
1188 	return;
1189 }
1190 
handle_plci(_cmsg * cmsg)1191 static void handle_plci(_cmsg *cmsg)
1192 {
1193 	capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1194 	capidrv_plci *plcip;
1195 	isdn_ctrl cmd;
1196 	_cdebbuf *cdb;
1197 
1198 	if (!card) {
1199 		printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1200 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1201 		       cmsg->adr.adrController & 0x7f);
1202 		return;
1203 	}
1204 	switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1205 
1206 	case CAPI_DISCONNECT_IND:	/* plci */
1207 		if (cmsg->Reason) {
1208 			printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
1209 			       card->contrnr,
1210 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1211 			       cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
1212 		}
1213 		if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
1214 			capi_cmsg_answer(cmsg);
1215 			send_message(card, cmsg);
1216 			goto notfound;
1217 		}
1218 		card->bchans[plcip->chan].disconnecting = 1;
1219 		plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
1220 		capi_cmsg_answer(cmsg);
1221 		plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
1222 		send_message(card, cmsg);
1223 		break;
1224 
1225 	case CAPI_DISCONNECT_CONF:	/* plci */
1226 		if (cmsg->Info) {
1227 			printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1228 			       card->contrnr,
1229 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1230 			       cmsg->Info, capi_info2str(cmsg->Info),
1231 			       cmsg->adr.adrPLCI);
1232 		}
1233 		if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1234 			goto notfound;
1235 
1236 		card->bchans[plcip->chan].disconnecting = 1;
1237 		break;
1238 
1239 	case CAPI_ALERT_CONF:	/* plci */
1240 		if (cmsg->Info) {
1241 			printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1242 			       card->contrnr,
1243 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1244 			       cmsg->Info, capi_info2str(cmsg->Info),
1245 			       cmsg->adr.adrPLCI);
1246 		}
1247 		break;
1248 
1249 	case CAPI_CONNECT_IND:	/* plci */
1250 		handle_incoming_call(card, cmsg);
1251 		break;
1252 
1253 	case CAPI_CONNECT_CONF:	/* plci */
1254 		if (cmsg->Info) {
1255 			printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1256 			       card->contrnr,
1257 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1258 			       cmsg->Info, capi_info2str(cmsg->Info),
1259 			       cmsg->adr.adrPLCI);
1260 		}
1261 		if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
1262 			goto notfound;
1263 
1264 		plcip->plci = cmsg->adr.adrPLCI;
1265 		if (cmsg->Info) {
1266 			plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
1267 		} else {
1268 			plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
1269 		}
1270 		break;
1271 
1272 	case CAPI_CONNECT_ACTIVE_IND:	/* plci */
1273 
1274 		if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1275 			goto notfound;
1276 
1277 		if (card->bchans[plcip->chan].incoming) {
1278 			capi_cmsg_answer(cmsg);
1279 			plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1280 			send_message(card, cmsg);
1281 		} else {
1282 			capidrv_ncci *nccip;
1283 			capi_cmsg_answer(cmsg);
1284 			send_message(card, cmsg);
1285 
1286 			nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
1287 
1288 			if (!nccip) {
1289 				printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1290 				break;	/* $$$$ */
1291 			}
1292 			capi_fill_CONNECT_B3_REQ(cmsg,
1293 						 global.ap.applid,
1294 						 card->msgid++,
1295 						 plcip->plci,	/* adr */
1296 						 NULL	/* NCPI */
1297 				);
1298 			nccip->msgid = cmsg->Messagenumber;
1299 			plci_change_state(card, plcip,
1300 					  EV_PLCI_CONNECT_ACTIVE_IND);
1301 			ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
1302 			send_message(card, cmsg);
1303 			cmd.command = ISDN_STAT_DCONN;
1304 			cmd.driver = card->myid;
1305 			cmd.arg = plcip->chan;
1306 			card->interface.statcallb(&cmd);
1307 		}
1308 		break;
1309 
1310 	case CAPI_INFO_IND:	/* Controller/plci */
1311 
1312 		if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1313 			goto notfound;
1314 
1315 		if (cmsg->InfoNumber == 0x4000) {
1316 			if (cmsg->InfoElement[0] == 4) {
1317 				cmd.command = ISDN_STAT_CINF;
1318 				cmd.driver = card->myid;
1319 				cmd.arg = plcip->chan;
1320 				sprintf(cmd.parm.num, "%lu",
1321 					(unsigned long)
1322 					((u32) cmsg->InfoElement[1]
1323 					 | ((u32) (cmsg->InfoElement[2]) << 8)
1324 					 | ((u32) (cmsg->InfoElement[3]) << 16)
1325 					 | ((u32) (cmsg->InfoElement[4]) << 24)));
1326 				card->interface.statcallb(&cmd);
1327 				break;
1328 			}
1329 		}
1330 		cdb = capi_cmsg2str(cmsg);
1331 		if (cdb) {
1332 			printk(KERN_WARNING "capidrv-%d: %s\n",
1333 			       card->contrnr, cdb->buf);
1334 			cdebbuf_free(cdb);
1335 		} else
1336 			printk(KERN_WARNING "capidrv-%d: CAPI_INFO_IND InfoNumber %x not handled\n",
1337 			       card->contrnr, cmsg->InfoNumber);
1338 
1339 		break;
1340 
1341 	case CAPI_CONNECT_ACTIVE_CONF:		/* plci */
1342 		goto ignored;
1343 	case CAPI_SELECT_B_PROTOCOL_CONF:	/* plci */
1344 		goto ignored;
1345 	case CAPI_FACILITY_IND:	/* Controller/plci/ncci */
1346 		goto ignored;
1347 	case CAPI_FACILITY_CONF:	/* Controller/plci/ncci */
1348 		goto ignored;
1349 
1350 	case CAPI_INFO_CONF:	/* Controller/plci */
1351 		goto ignored;
1352 
1353 	default:
1354 		printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
1355 		       card->contrnr,
1356 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1357 		       cmsg->adr.adrPLCI);
1358 	}
1359 	return;
1360 ignored:
1361 	printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
1362 	       card->contrnr,
1363 	       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1364 	       cmsg->adr.adrPLCI);
1365 	return;
1366 notfound:
1367 	printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
1368 	       card->contrnr,
1369 	       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1370 	       cmsg->adr.adrPLCI);
1371 	return;
1372 }
1373 
handle_ncci(_cmsg * cmsg)1374 static void handle_ncci(_cmsg *cmsg)
1375 {
1376 	capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1377 	capidrv_plci *plcip;
1378 	capidrv_ncci *nccip;
1379 	isdn_ctrl cmd;
1380 	int len;
1381 
1382 	if (!card) {
1383 		printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1384 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1385 		       cmsg->adr.adrController & 0x7f);
1386 		return;
1387 	}
1388 	switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1389 
1390 	case CAPI_CONNECT_B3_ACTIVE_IND:	/* ncci */
1391 		if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1392 			goto notfound;
1393 
1394 		capi_cmsg_answer(cmsg);
1395 		ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
1396 		send_message(card, cmsg);
1397 
1398 		cmd.command = ISDN_STAT_BCONN;
1399 		cmd.driver = card->myid;
1400 		cmd.arg = nccip->chan;
1401 		card->interface.statcallb(&cmd);
1402 
1403 		printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
1404 		       card->contrnr, nccip->chan, nccip->ncci);
1405 		break;
1406 
1407 	case CAPI_CONNECT_B3_ACTIVE_CONF:	/* ncci */
1408 		goto ignored;
1409 
1410 	case CAPI_CONNECT_B3_IND:	/* ncci */
1411 
1412 		plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
1413 		if (plcip) {
1414 			nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
1415 			if (nccip) {
1416 				ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
1417 				capi_fill_CONNECT_B3_RESP(cmsg,
1418 							  global.ap.applid,
1419 							  card->msgid++,
1420 							  nccip->ncci,	/* adr */
1421 							  0,	/* Reject */
1422 							  NULL	/* NCPI */
1423 					);
1424 				ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
1425 				send_message(card, cmsg);
1426 				break;
1427 			}
1428 			printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n",							card->contrnr);
1429 		} else {
1430 			printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
1431 			       card->contrnr,
1432 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1433 			       cmsg->adr.adrNCCI);
1434 		}
1435 		capi_fill_CONNECT_B3_RESP(cmsg,
1436 					  global.ap.applid,
1437 					  card->msgid++,
1438 					  cmsg->adr.adrNCCI,
1439 					  2,	/* Reject */
1440 					  NULL	/* NCPI */
1441 			);
1442 		send_message(card, cmsg);
1443 		break;
1444 
1445 	case CAPI_CONNECT_B3_CONF:	/* ncci */
1446 
1447 		if (!(nccip = find_ncci_by_msgid(card,
1448 						 cmsg->adr.adrNCCI,
1449 						 cmsg->Messagenumber)))
1450 			goto notfound;
1451 
1452 		nccip->ncci = cmsg->adr.adrNCCI;
1453 		if (cmsg->Info) {
1454 			printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1455 			       card->contrnr,
1456 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1457 			       cmsg->Info, capi_info2str(cmsg->Info),
1458 			       cmsg->adr.adrNCCI);
1459 		}
1460 
1461 		if (cmsg->Info)
1462 			ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
1463 		else
1464 			ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
1465 		break;
1466 
1467 	case CAPI_CONNECT_B3_T90_ACTIVE_IND:	/* ncci */
1468 		capi_cmsg_answer(cmsg);
1469 		send_message(card, cmsg);
1470 		break;
1471 
1472 	case CAPI_DATA_B3_IND:	/* ncci */
1473 		/* handled in handle_data() */
1474 		goto ignored;
1475 
1476 	case CAPI_DATA_B3_CONF:	/* ncci */
1477 		if (cmsg->Info) {
1478 			printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
1479 			       cmsg->Info, capi_info2str(cmsg->Info));
1480 		}
1481 		if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1482 			goto notfound;
1483 
1484 		len = capidrv_del_ack(nccip, cmsg->DataHandle);
1485 		if (len < 0)
1486 			break;
1487 		cmd.command = ISDN_STAT_BSENT;
1488 		cmd.driver = card->myid;
1489 		cmd.arg = nccip->chan;
1490 		cmd.parm.length = len;
1491 		card->interface.statcallb(&cmd);
1492 		break;
1493 
1494 	case CAPI_DISCONNECT_B3_IND:	/* ncci */
1495 		if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1496 			goto notfound;
1497 
1498 		card->bchans[nccip->chan].disconnecting = 1;
1499 		ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
1500 		capi_cmsg_answer(cmsg);
1501 		ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
1502 		send_message(card, cmsg);
1503 		break;
1504 
1505 	case CAPI_DISCONNECT_B3_CONF:	/* ncci */
1506 		if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1507 			goto notfound;
1508 		if (cmsg->Info) {
1509 			printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1510 			       card->contrnr,
1511 			       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1512 			       cmsg->Info, capi_info2str(cmsg->Info),
1513 			       cmsg->adr.adrNCCI);
1514 			ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
1515 		}
1516 		break;
1517 
1518 	case CAPI_RESET_B3_IND:	/* ncci */
1519 		if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1520 			goto notfound;
1521 		ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
1522 		capi_cmsg_answer(cmsg);
1523 		send_message(card, cmsg);
1524 		break;
1525 
1526 	case CAPI_RESET_B3_CONF:	/* ncci */
1527 		goto ignored;	/* $$$$ */
1528 
1529 	case CAPI_FACILITY_IND:	/* Controller/plci/ncci */
1530 		goto ignored;
1531 	case CAPI_FACILITY_CONF:	/* Controller/plci/ncci */
1532 		goto ignored;
1533 
1534 	default:
1535 		printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
1536 		       card->contrnr,
1537 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1538 		       cmsg->adr.adrNCCI);
1539 	}
1540 	return;
1541 ignored:
1542 	printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
1543 	       card->contrnr,
1544 	       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1545 	       cmsg->adr.adrNCCI);
1546 	return;
1547 notfound:
1548 	printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1549 	       card->contrnr,
1550 	       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1551 	       cmsg->adr.adrNCCI);
1552 }
1553 
1554 
handle_data(_cmsg * cmsg,struct sk_buff * skb)1555 static void handle_data(_cmsg *cmsg, struct sk_buff *skb)
1556 {
1557 	capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1558 	capidrv_ncci *nccip;
1559 
1560 	if (!card) {
1561 		printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1562 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1563 		       cmsg->adr.adrController & 0x7f);
1564 		kfree_skb(skb);
1565 		return;
1566 	}
1567 	if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
1568 		printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1569 		       card->contrnr,
1570 		       capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1571 		       cmsg->adr.adrNCCI);
1572 		kfree_skb(skb);
1573 		return;
1574 	}
1575 	(void) skb_pull(skb, CAPIMSG_LEN(skb->data));
1576 	card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
1577 	capi_cmsg_answer(cmsg);
1578 	send_message(card, cmsg);
1579 }
1580 
1581 static _cmsg s_cmsg;
1582 
capidrv_recv_message(struct capi20_appl * ap,struct sk_buff * skb)1583 static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
1584 {
1585 	if (capi_message2cmsg(&s_cmsg, skb->data)) {
1586 		printk(KERN_ERR "capidrv: applid=%d: received invalid message\n",
1587 		       ap->applid);
1588 		kfree_skb(skb);
1589 		return;
1590 	}
1591 	if (debugmode > 3) {
1592 		_cdebbuf *cdb = capi_cmsg2str(&s_cmsg);
1593 
1594 		if (cdb) {
1595 			printk(KERN_DEBUG "%s: applid=%d %s\n", __func__,
1596 			       ap->applid, cdb->buf);
1597 			cdebbuf_free(cdb);
1598 		} else
1599 			printk(KERN_DEBUG "%s: applid=%d %s not traced\n",
1600 			       __func__, ap->applid,
1601 			       capi_cmd2str(s_cmsg.Command, s_cmsg.Subcommand));
1602 	}
1603 	if (s_cmsg.Command == CAPI_DATA_B3
1604 	    && s_cmsg.Subcommand == CAPI_IND) {
1605 		handle_data(&s_cmsg, skb);
1606 		return;
1607 	}
1608 	if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
1609 		handle_controller(&s_cmsg);
1610 	else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
1611 		handle_plci(&s_cmsg);
1612 	else
1613 		handle_ncci(&s_cmsg);
1614 	/*
1615 	 * data of skb used in s_cmsg,
1616 	 * free data when s_cmsg is not used again
1617 	 * thanks to Lars Heete <hel@admin.de>
1618 	 */
1619 	kfree_skb(skb);
1620 }
1621 
1622 /* ------------------------------------------------------------------- */
1623 
1624 #define PUTBYTE_TO_STATUS(card, byte)				\
1625 	do {							\
1626 		*(card)->q931_write++ = (byte);			\
1627 		if ((card)->q931_write > (card)->q931_end)	\
1628 			(card)->q931_write = (card)->q931_buf;	\
1629 	} while (0)
1630 
handle_dtrace_data(capidrv_contr * card,int send,int level2,u8 * data,u16 len)1631 static void handle_dtrace_data(capidrv_contr *card,
1632 			       int send, int level2, u8 *data, u16 len)
1633 {
1634 	u8 *p, *end;
1635 	isdn_ctrl cmd;
1636 
1637 	if (!len) {
1638 		printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
1639 		       card->contrnr, len);
1640 		return;
1641 	}
1642 
1643 	if (level2) {
1644 		PUTBYTE_TO_STATUS(card, 'D');
1645 		PUTBYTE_TO_STATUS(card, '2');
1646 		PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1647 		PUTBYTE_TO_STATUS(card, ':');
1648 	} else {
1649 		PUTBYTE_TO_STATUS(card, 'D');
1650 		PUTBYTE_TO_STATUS(card, '3');
1651 		PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1652 		PUTBYTE_TO_STATUS(card, ':');
1653 	}
1654 
1655 	for (p = data, end = data + len; p < end; p++) {
1656 		PUTBYTE_TO_STATUS(card, ' ');
1657 		PUTBYTE_TO_STATUS(card, hex_asc_hi(*p));
1658 		PUTBYTE_TO_STATUS(card, hex_asc_lo(*p));
1659 	}
1660 	PUTBYTE_TO_STATUS(card, '\n');
1661 
1662 	cmd.command = ISDN_STAT_STAVAIL;
1663 	cmd.driver = card->myid;
1664 	cmd.arg = len * 3 + 5;
1665 	card->interface.statcallb(&cmd);
1666 }
1667 
1668 /* ------------------------------------------------------------------- */
1669 
1670 static _cmsg cmdcmsg;
1671 
capidrv_ioctl(isdn_ctrl * c,capidrv_contr * card)1672 static int capidrv_ioctl(isdn_ctrl *c, capidrv_contr *card)
1673 {
1674 	switch (c->arg) {
1675 	case 1:
1676 		debugmode = (int)(*((unsigned int *)c->parm.num));
1677 		printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
1678 		       card->contrnr, debugmode);
1679 		return 0;
1680 	default:
1681 		printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
1682 		       card->contrnr, c->arg);
1683 		return -EINVAL;
1684 	}
1685 	return -EINVAL;
1686 }
1687 
1688 /*
1689  * Handle leased lines (CAPI-Bundling)
1690  */
1691 
1692 struct internal_bchannelinfo {
1693 	unsigned short channelalloc;
1694 	unsigned short operation;
1695 	unsigned char  cmask[31];
1696 };
1697 
decodeFVteln(char * teln,unsigned long * bmaskp,int * activep)1698 static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
1699 {
1700 	unsigned long bmask = 0;
1701 	int active = !0;
1702 	char *s;
1703 	int i;
1704 
1705 	if (strncmp(teln, "FV:", 3) != 0)
1706 		return 1;
1707 	s = teln + 3;
1708 	while (*s && *s == ' ') s++;
1709 	if (!*s) return -2;
1710 	if (*s == 'p' || *s == 'P') {
1711 		active = 0;
1712 		s++;
1713 	}
1714 	if (*s == 'a' || *s == 'A') {
1715 		active = !0;
1716 		s++;
1717 	}
1718 	while (*s) {
1719 		int digit1 = 0;
1720 		int digit2 = 0;
1721 		char *endp;
1722 
1723 		digit1 = simple_strtoul(s, &endp, 10);
1724 		if (s == endp)
1725 			return -3;
1726 		s = endp;
1727 
1728 		if (digit1 <= 0 || digit1 > 30) return -4;
1729 		if (*s == 0 || *s == ',' || *s == ' ') {
1730 			bmask |= (1 << digit1);
1731 			digit1 = 0;
1732 			if (*s) s++;
1733 			continue;
1734 		}
1735 		if (*s != '-') return -5;
1736 		s++;
1737 
1738 		digit2 = simple_strtoul(s, &endp, 10);
1739 		if (s == endp)
1740 			return -3;
1741 		s = endp;
1742 
1743 		if (digit2 <= 0 || digit2 > 30) return -4;
1744 		if (*s == 0 || *s == ',' || *s == ' ') {
1745 			if (digit1 > digit2)
1746 				for (i = digit2; i <= digit1; i++)
1747 					bmask |= (1 << i);
1748 			else
1749 				for (i = digit1; i <= digit2; i++)
1750 					bmask |= (1 << i);
1751 			digit1 = digit2 = 0;
1752 			if (*s) s++;
1753 			continue;
1754 		}
1755 		return -6;
1756 	}
1757 	if (activep) *activep = active;
1758 	if (bmaskp) *bmaskp = bmask;
1759 	return 0;
1760 }
1761 
FVteln2capi20(char * teln,u8 AdditionalInfo[1+2+2+31])1762 static int FVteln2capi20(char *teln, u8 AdditionalInfo[1 + 2 + 2 + 31])
1763 {
1764 	unsigned long bmask;
1765 	int active;
1766 	int rc, i;
1767 
1768 	rc = decodeFVteln(teln, &bmask, &active);
1769 	if (rc) return rc;
1770 	/* Length */
1771 	AdditionalInfo[0] = 2 + 2 + 31;
1772 	/* Channel: 3 => use channel allocation */
1773 	AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
1774 	/* Operation: 0 => DTE mode, 1 => DCE mode */
1775 	if (active) {
1776 		AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
1777 	} else {
1778 		AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
1779 	}
1780 	/* Channel mask array */
1781 	AdditionalInfo[5] = 0; /* no D-Channel */
1782 	for (i = 1; i <= 30; i++)
1783 		AdditionalInfo[5 + i] = (bmask & (1 << i)) ? 0xff : 0;
1784 	return 0;
1785 }
1786 
capidrv_command(isdn_ctrl * c,capidrv_contr * card)1787 static int capidrv_command(isdn_ctrl *c, capidrv_contr *card)
1788 {
1789 	isdn_ctrl cmd;
1790 	struct capidrv_bchan *bchan;
1791 	struct capidrv_plci *plcip;
1792 	u8 AdditionalInfo[1 + 2 + 2 + 31];
1793 	int rc, isleasedline = 0;
1794 
1795 	if (c->command == ISDN_CMD_IOCTL)
1796 		return capidrv_ioctl(c, card);
1797 
1798 	switch (c->command) {
1799 	case ISDN_CMD_DIAL: {
1800 		u8 calling[ISDN_MSNLEN + 3];
1801 		u8 called[ISDN_MSNLEN + 2];
1802 
1803 		if (debugmode)
1804 			printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
1805 			       card->contrnr,
1806 			       c->arg,
1807 			       c->parm.setup.phone,
1808 			       c->parm.setup.si1,
1809 			       c->parm.setup.si2,
1810 			       c->parm.setup.eazmsn);
1811 
1812 		bchan = &card->bchans[c->arg % card->nbchan];
1813 
1814 		if (bchan->plcip) {
1815 			printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
1816 			       card->contrnr,
1817 			       c->arg,
1818 			       c->parm.setup.phone,
1819 			       c->parm.setup.si1,
1820 			       c->parm.setup.si2,
1821 			       c->parm.setup.eazmsn,
1822 			       bchan->plcip->plci);
1823 			return 0;
1824 		}
1825 		bchan->si1 = c->parm.setup.si1;
1826 		bchan->si2 = c->parm.setup.si2;
1827 
1828 		strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
1829 		strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
1830 		rc = FVteln2capi20(bchan->num, AdditionalInfo);
1831 		isleasedline = (rc == 0);
1832 		if (rc < 0)
1833 			printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
1834 
1835 		if (isleasedline) {
1836 			calling[0] = 0;
1837 			called[0] = 0;
1838 			if (debugmode)
1839 				printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
1840 		} else {
1841 			calling[0] = strlen(bchan->mynum) + 2;
1842 			calling[1] = 0;
1843 			calling[2] = 0x80;
1844 			strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
1845 			called[0] = strlen(bchan->num) + 1;
1846 			called[1] = 0x80;
1847 			strncpy(called + 2, bchan->num, ISDN_MSNLEN);
1848 		}
1849 
1850 		capi_fill_CONNECT_REQ(&cmdcmsg,
1851 				      global.ap.applid,
1852 				      card->msgid++,
1853 				      card->contrnr,	/* adr */
1854 				      si2cip(bchan->si1, bchan->si2),	/* cipvalue */
1855 				      called,	/* CalledPartyNumber */
1856 				      calling,	/* CallingPartyNumber */
1857 				      NULL,	/* CalledPartySubaddress */
1858 				      NULL,	/* CallingPartySubaddress */
1859 				      b1prot(bchan->l2, bchan->l3),	/* B1protocol */
1860 				      b2prot(bchan->l2, bchan->l3),	/* B2protocol */
1861 				      b3prot(bchan->l2, bchan->l3),	/* B3protocol */
1862 				      b1config(bchan->l2, bchan->l3),	/* B1configuration */
1863 				      NULL,	/* B2configuration */
1864 				      NULL,	/* B3configuration */
1865 				      NULL,	/* BC */
1866 				      NULL,	/* LLC */
1867 				      NULL,	/* HLC */
1868 				      /* BChannelinformation */
1869 				      isleasedline ? AdditionalInfo : NULL,
1870 				      NULL,	/* Keypadfacility */
1871 				      NULL,	/* Useruserdata */
1872 				      NULL	/* Facilitydataarray */
1873 			);
1874 		if ((plcip = new_plci(card, (c->arg % card->nbchan))) == NULL) {
1875 			cmd.command = ISDN_STAT_DHUP;
1876 			cmd.driver = card->myid;
1877 			cmd.arg = (c->arg % card->nbchan);
1878 			card->interface.statcallb(&cmd);
1879 			return -1;
1880 		}
1881 		plcip->msgid = cmdcmsg.Messagenumber;
1882 		plcip->leasedline = isleasedline;
1883 		plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
1884 		send_message(card, &cmdcmsg);
1885 		return 0;
1886 	}
1887 
1888 	case ISDN_CMD_ACCEPTD:
1889 
1890 		bchan = &card->bchans[c->arg % card->nbchan];
1891 		if (debugmode)
1892 			printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
1893 			       card->contrnr,
1894 			       c->arg, bchan->l2, bchan->l3);
1895 
1896 		capi_fill_CONNECT_RESP(&cmdcmsg,
1897 				       global.ap.applid,
1898 				       card->msgid++,
1899 				       bchan->plcip->plci,	/* adr */
1900 				       0,	/* Reject */
1901 				       b1prot(bchan->l2, bchan->l3),	/* B1protocol */
1902 				       b2prot(bchan->l2, bchan->l3),	/* B2protocol */
1903 				       b3prot(bchan->l2, bchan->l3),	/* B3protocol */
1904 				       b1config(bchan->l2, bchan->l3),	/* B1configuration */
1905 				       NULL,	/* B2configuration */
1906 				       NULL,	/* B3configuration */
1907 				       NULL,	/* ConnectedNumber */
1908 				       NULL,	/* ConnectedSubaddress */
1909 				       NULL,	/* LLC */
1910 				       NULL,	/* BChannelinformation */
1911 				       NULL,	/* Keypadfacility */
1912 				       NULL,	/* Useruserdata */
1913 				       NULL	/* Facilitydataarray */
1914 			);
1915 		if (capi_cmsg2message(&cmdcmsg, cmdcmsg.buf)) {
1916 			printk(KERN_ERR "capidrv-%d: capidrv_command: parser failure\n",
1917 			       card->contrnr);
1918 			return -EINVAL;
1919 		}
1920 		plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
1921 		send_message(card, &cmdcmsg);
1922 		return 0;
1923 
1924 	case ISDN_CMD_ACCEPTB:
1925 		if (debugmode)
1926 			printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
1927 			       card->contrnr,
1928 			       c->arg);
1929 		return -ENOSYS;
1930 
1931 	case ISDN_CMD_HANGUP:
1932 		if (debugmode)
1933 			printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
1934 			       card->contrnr,
1935 			       c->arg);
1936 		bchan = &card->bchans[c->arg % card->nbchan];
1937 
1938 		if (bchan->disconnecting) {
1939 			if (debugmode)
1940 				printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
1941 				       card->contrnr,
1942 				       c->arg);
1943 			return 0;
1944 		}
1945 		if (bchan->nccip) {
1946 			bchan->disconnecting = 1;
1947 			capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
1948 						    global.ap.applid,
1949 						    card->msgid++,
1950 						    bchan->nccip->ncci,
1951 						    NULL	/* NCPI */
1952 				);
1953 			ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
1954 			send_message(card, &cmdcmsg);
1955 			return 0;
1956 		} else if (bchan->plcip) {
1957 			if (bchan->plcip->state == ST_PLCI_INCOMING) {
1958 				/*
1959 				 * just ignore, we a called from
1960 				 * isdn_status_callback(),
1961 				 * which will return 0 or 2, this is handled
1962 				 * by the CONNECT_IND handler
1963 				 */
1964 				bchan->disconnecting = 1;
1965 				return 0;
1966 			} else if (bchan->plcip->plci) {
1967 				bchan->disconnecting = 1;
1968 				capi_fill_DISCONNECT_REQ(&cmdcmsg,
1969 							 global.ap.applid,
1970 							 card->msgid++,
1971 							 bchan->plcip->plci,
1972 							 NULL,	/* BChannelinformation */
1973 							 NULL,	/* Keypadfacility */
1974 							 NULL,	/* Useruserdata */
1975 							 NULL	/* Facilitydataarray */
1976 					);
1977 				plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
1978 				send_message(card, &cmdcmsg);
1979 				return 0;
1980 			} else {
1981 				printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
1982 				       card->contrnr,
1983 				       c->arg);
1984 				return -EINVAL;
1985 			}
1986 		}
1987 		printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
1988 		       card->contrnr,
1989 		       c->arg);
1990 		return -EINVAL;
1991 /* ready */
1992 
1993 	case ISDN_CMD_SETL2:
1994 		if (debugmode)
1995 			printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
1996 			       card->contrnr,
1997 			       (c->arg & 0xff), (c->arg >> 8));
1998 		bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1999 		bchan->l2 = (c->arg >> 8);
2000 		return 0;
2001 
2002 	case ISDN_CMD_SETL3:
2003 		if (debugmode)
2004 			printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
2005 			       card->contrnr,
2006 			       (c->arg & 0xff), (c->arg >> 8));
2007 		bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
2008 		bchan->l3 = (c->arg >> 8);
2009 		return 0;
2010 
2011 	case ISDN_CMD_SETEAZ:
2012 		if (debugmode)
2013 			printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
2014 			       card->contrnr,
2015 			       c->parm.num, c->arg);
2016 		bchan = &card->bchans[c->arg % card->nbchan];
2017 		strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
2018 		return 0;
2019 
2020 	case ISDN_CMD_CLREAZ:
2021 		if (debugmode)
2022 			printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
2023 			       card->contrnr, c->arg);
2024 		bchan = &card->bchans[c->arg % card->nbchan];
2025 		bchan->msn[0] = 0;
2026 		return 0;
2027 
2028 	default:
2029 		printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
2030 		       card->contrnr, c->command);
2031 		return -EINVAL;
2032 	}
2033 	return 0;
2034 }
2035 
if_command(isdn_ctrl * c)2036 static int if_command(isdn_ctrl *c)
2037 {
2038 	capidrv_contr *card = findcontrbydriverid(c->driver);
2039 
2040 	if (card)
2041 		return capidrv_command(c, card);
2042 
2043 	printk(KERN_ERR
2044 	       "capidrv: if_command %d called with invalid driverId %d!\n",
2045 	       c->command, c->driver);
2046 	return -ENODEV;
2047 }
2048 
2049 static _cmsg sendcmsg;
2050 
if_sendbuf(int id,int channel,int doack,struct sk_buff * skb)2051 static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
2052 {
2053 	capidrv_contr *card = findcontrbydriverid(id);
2054 	capidrv_bchan *bchan;
2055 	capidrv_ncci *nccip;
2056 	int len = skb->len;
2057 	int msglen;
2058 	u16 errcode;
2059 	u16 datahandle;
2060 	u32 data;
2061 
2062 	if (!card) {
2063 		printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
2064 		       id);
2065 		return 0;
2066 	}
2067 	if (debugmode > 4)
2068 		printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
2069 		       card->contrnr, len, skb, doack);
2070 	bchan = &card->bchans[channel % card->nbchan];
2071 	nccip = bchan->nccip;
2072 	if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
2073 		printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
2074 		       card->contrnr, card->name, channel);
2075 		return 0;
2076 	}
2077 	datahandle = nccip->datahandle;
2078 
2079 	/*
2080 	 * Here we copy pointer skb->data into the 32-bit 'Data' field.
2081 	 * The 'Data' field is not used in practice in linux kernel
2082 	 * (neither in 32 or 64 bit), but should have some value,
2083 	 * since a CAPI message trace will display it.
2084 	 *
2085 	 * The correct value in the 32 bit case is the address of the
2086 	 * data, in 64 bit it makes no sense, we use 0 there.
2087 	 */
2088 
2089 #ifdef CONFIG_64BIT
2090 	data = 0;
2091 #else
2092 	data = (unsigned long) skb->data;
2093 #endif
2094 
2095 	capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
2096 			      nccip->ncci,	/* adr */
2097 			      data,		/* Data */
2098 			      skb->len,		/* DataLength */
2099 			      datahandle,	/* DataHandle */
2100 			      0	/* Flags */
2101 		);
2102 
2103 	if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
2104 		return 0;
2105 
2106 	if (capi_cmsg2message(&sendcmsg, sendcmsg.buf)) {
2107 		printk(KERN_ERR "capidrv-%d: if_sendbuf: parser failure\n",
2108 		       card->contrnr);
2109 		return -EINVAL;
2110 	}
2111 	msglen = CAPIMSG_LEN(sendcmsg.buf);
2112 	if (skb_headroom(skb) < msglen) {
2113 		struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
2114 		if (!nskb) {
2115 			printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
2116 			       card->contrnr);
2117 			(void)capidrv_del_ack(nccip, datahandle);
2118 			return 0;
2119 		}
2120 		printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
2121 		       card->contrnr, skb_headroom(skb), msglen);
2122 		memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
2123 		errcode = capi20_put_message(&global.ap, nskb);
2124 		if (errcode == CAPI_NOERROR) {
2125 			dev_kfree_skb(skb);
2126 			nccip->datahandle++;
2127 			return len;
2128 		}
2129 		if (debugmode > 3)
2130 			printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
2131 			       card->contrnr, errcode, capi_info2str(errcode));
2132 		(void)capidrv_del_ack(nccip, datahandle);
2133 		dev_kfree_skb(nskb);
2134 		return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
2135 	} else {
2136 		memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
2137 		errcode = capi20_put_message(&global.ap, skb);
2138 		if (errcode == CAPI_NOERROR) {
2139 			nccip->datahandle++;
2140 			return len;
2141 		}
2142 		if (debugmode > 3)
2143 			printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
2144 			       card->contrnr, errcode, capi_info2str(errcode));
2145 		skb_pull(skb, msglen);
2146 		(void)capidrv_del_ack(nccip, datahandle);
2147 		return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
2148 	}
2149 }
2150 
if_readstat(u8 __user * buf,int len,int id,int channel)2151 static int if_readstat(u8 __user *buf, int len, int id, int channel)
2152 {
2153 	capidrv_contr *card = findcontrbydriverid(id);
2154 	int count;
2155 	u8 __user *p;
2156 
2157 	if (!card) {
2158 		printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
2159 		       id);
2160 		return -ENODEV;
2161 	}
2162 
2163 	for (p = buf, count = 0; count < len; p++, count++) {
2164 		if (put_user(*card->q931_read++, p))
2165 			return -EFAULT;
2166 		if (card->q931_read > card->q931_end)
2167 			card->q931_read = card->q931_buf;
2168 	}
2169 	return count;
2170 
2171 }
2172 
enable_dchannel_trace(capidrv_contr * card)2173 static void enable_dchannel_trace(capidrv_contr *card)
2174 {
2175 	u8 manufacturer[CAPI_MANUFACTURER_LEN];
2176 	capi_version version;
2177 	u16 contr = card->contrnr;
2178 	u16 errcode;
2179 	u16 avmversion[3];
2180 
2181 	errcode = capi20_get_manufacturer(contr, manufacturer);
2182 	if (errcode != CAPI_NOERROR) {
2183 		printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
2184 		       card->name, errcode);
2185 		return;
2186 	}
2187 	if (strstr(manufacturer, "AVM") == NULL) {
2188 		printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
2189 		       card->name, manufacturer);
2190 		return;
2191 	}
2192 	errcode = capi20_get_version(contr, &version);
2193 	if (errcode != CAPI_NOERROR) {
2194 		printk(KERN_ERR "%s: can't get version (0x%x)\n",
2195 		       card->name, errcode);
2196 		return;
2197 	}
2198 	avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
2199 	avmversion[1] = (version.majormanuversion << 4) & 0xf0;
2200 	avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
2201 	avmversion[2] |= version.minormanuversion & 0x0f;
2202 
2203 	if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
2204 		printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
2205 		capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
2206 					   card->msgid++,
2207 					   contr,
2208 					   0x214D5641,  /* ManuID */
2209 					   0,           /* Class */
2210 					   1,           /* Function */
2211 					   (_cstruct)"\004\200\014\000\000");
2212 	} else {
2213 		printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
2214 		capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
2215 					   card->msgid++,
2216 					   contr,
2217 					   0x214D5641,  /* ManuID */
2218 					   0,           /* Class */
2219 					   1,           /* Function */
2220 					   (_cstruct)"\004\002\003\000\000");
2221 	}
2222 	send_message(card, &cmdcmsg);
2223 }
2224 
2225 
send_listen(capidrv_contr * card)2226 static void send_listen(capidrv_contr *card)
2227 {
2228 	capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
2229 			     card->msgid++,
2230 			     card->contrnr, /* controller */
2231 			     1 << 6,	/* Infomask */
2232 			     card->cipmask,
2233 			     card->cipmask2,
2234 			     NULL, NULL);
2235 	listen_change_state(card, EV_LISTEN_REQ);
2236 	send_message(card, &cmdcmsg);
2237 }
2238 
listentimerfunc(struct timer_list * t)2239 static void listentimerfunc(struct timer_list *t)
2240 {
2241 	capidrv_contr *card = from_timer(card, t, listentimer);
2242 	if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
2243 		printk(KERN_ERR "%s: controller dead ??\n", card->name);
2244 	send_listen(card);
2245 	mod_timer(&card->listentimer, jiffies + 60 * HZ);
2246 }
2247 
2248 
capidrv_addcontr(u16 contr,struct capi_profile * profp)2249 static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
2250 {
2251 	capidrv_contr *card;
2252 	unsigned long flags;
2253 	isdn_ctrl cmd;
2254 	char id[20];
2255 	int i;
2256 
2257 	sprintf(id, "capidrv-%d", contr);
2258 	if (!try_module_get(THIS_MODULE)) {
2259 		printk(KERN_WARNING "capidrv: (%s) Could not reserve module\n", id);
2260 		return -1;
2261 	}
2262 	if (!(card = kzalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
2263 		printk(KERN_WARNING
2264 		       "capidrv: (%s) Could not allocate contr-struct.\n", id);
2265 		return -1;
2266 	}
2267 	card->owner = THIS_MODULE;
2268 	timer_setup(&card->listentimer, listentimerfunc, 0);
2269 	strcpy(card->name, id);
2270 	card->contrnr = contr;
2271 	card->nbchan = profp->nbchannel;
2272 	card->bchans = kmalloc_array(card->nbchan, sizeof(capidrv_bchan),
2273 				     GFP_ATOMIC);
2274 	if (!card->bchans) {
2275 		printk(KERN_WARNING
2276 		       "capidrv: (%s) Could not allocate bchan-structs.\n", id);
2277 		module_put(card->owner);
2278 		kfree(card);
2279 		return -1;
2280 	}
2281 	card->interface.channels = profp->nbchannel;
2282 	card->interface.maxbufsize = 2048;
2283 	card->interface.command = if_command;
2284 	card->interface.writebuf_skb = if_sendbuf;
2285 	card->interface.writecmd = NULL;
2286 	card->interface.readstat = if_readstat;
2287 	card->interface.features =
2288 		ISDN_FEATURE_L2_HDLC |
2289 		ISDN_FEATURE_L2_TRANS |
2290 		ISDN_FEATURE_L3_TRANS |
2291 		ISDN_FEATURE_P_UNKNOWN |
2292 		ISDN_FEATURE_L2_X75I |
2293 		ISDN_FEATURE_L2_X75UI |
2294 		ISDN_FEATURE_L2_X75BUI;
2295 	if (profp->support1 & (1 << 2))
2296 		card->interface.features |=
2297 			ISDN_FEATURE_L2_V11096 |
2298 			ISDN_FEATURE_L2_V11019 |
2299 			ISDN_FEATURE_L2_V11038;
2300 	if (profp->support1 & (1 << 8))
2301 		card->interface.features |= ISDN_FEATURE_L2_MODEM;
2302 	card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
2303 	strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
2304 
2305 
2306 	card->q931_read = card->q931_buf;
2307 	card->q931_write = card->q931_buf;
2308 	card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
2309 
2310 	if (!register_isdn(&card->interface)) {
2311 		printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
2312 		kfree(card->bchans);
2313 		module_put(card->owner);
2314 		kfree(card);
2315 		return -1;
2316 	}
2317 	card->myid = card->interface.channels;
2318 	memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
2319 	for (i = 0; i < card->nbchan; i++) {
2320 		card->bchans[i].contr = card;
2321 	}
2322 
2323 	spin_lock_irqsave(&global_lock, flags);
2324 	card->next = global.contr_list;
2325 	global.contr_list = card;
2326 	global.ncontr++;
2327 	spin_unlock_irqrestore(&global_lock, flags);
2328 
2329 	cmd.command = ISDN_STAT_RUN;
2330 	cmd.driver = card->myid;
2331 	card->interface.statcallb(&cmd);
2332 
2333 	card->cipmask = 0x1FFF03FF;	/* any */
2334 	card->cipmask2 = 0;
2335 
2336 	send_listen(card);
2337 	mod_timer(&card->listentimer, jiffies + 60 * HZ);
2338 
2339 	printk(KERN_INFO "%s: now up (%d B channels)\n",
2340 	       card->name, card->nbchan);
2341 
2342 	enable_dchannel_trace(card);
2343 
2344 	return 0;
2345 }
2346 
capidrv_delcontr(u16 contr)2347 static int capidrv_delcontr(u16 contr)
2348 {
2349 	capidrv_contr **pp, *card;
2350 	unsigned long flags;
2351 	isdn_ctrl cmd;
2352 
2353 	spin_lock_irqsave(&global_lock, flags);
2354 	for (card = global.contr_list; card; card = card->next) {
2355 		if (card->contrnr == contr)
2356 			break;
2357 	}
2358 	if (!card) {
2359 		spin_unlock_irqrestore(&global_lock, flags);
2360 		printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
2361 		return -1;
2362 	}
2363 
2364 	/* FIXME: maybe a race condition the card should be removed
2365 	 * here from global list /kkeil
2366 	 */
2367 	spin_unlock_irqrestore(&global_lock, flags);
2368 
2369 	del_timer(&card->listentimer);
2370 
2371 	if (debugmode)
2372 		printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
2373 		       card->contrnr, card->myid);
2374 
2375 	cmd.command = ISDN_STAT_STOP;
2376 	cmd.driver = card->myid;
2377 	card->interface.statcallb(&cmd);
2378 
2379 	while (card->nbchan) {
2380 
2381 		cmd.command = ISDN_STAT_DISCH;
2382 		cmd.driver = card->myid;
2383 		cmd.arg = card->nbchan - 1;
2384 		cmd.parm.num[0] = 0;
2385 		if (debugmode)
2386 			printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
2387 			       card->contrnr, card->myid, cmd.arg);
2388 		card->interface.statcallb(&cmd);
2389 
2390 		if (card->bchans[card->nbchan - 1].nccip)
2391 			free_ncci(card, card->bchans[card->nbchan - 1].nccip);
2392 		if (card->bchans[card->nbchan - 1].plcip)
2393 			free_plci(card, card->bchans[card->nbchan - 1].plcip);
2394 		if (card->plci_list)
2395 			printk(KERN_ERR "capidrv: bug in free_plci()\n");
2396 		card->nbchan--;
2397 	}
2398 	kfree(card->bchans);
2399 	card->bchans = NULL;
2400 
2401 	if (debugmode)
2402 		printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
2403 		       card->contrnr, card->myid);
2404 
2405 	cmd.command = ISDN_STAT_UNLOAD;
2406 	cmd.driver = card->myid;
2407 	card->interface.statcallb(&cmd);
2408 
2409 	if (debugmode)
2410 		printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
2411 		       card->contrnr, card->myid);
2412 
2413 	spin_lock_irqsave(&global_lock, flags);
2414 	for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
2415 		if (*pp == card) {
2416 			*pp = (*pp)->next;
2417 			card->next = NULL;
2418 			global.ncontr--;
2419 			break;
2420 		}
2421 	}
2422 	spin_unlock_irqrestore(&global_lock, flags);
2423 
2424 	module_put(card->owner);
2425 	printk(KERN_INFO "%s: now down.\n", card->name);
2426 	kfree(card);
2427 	return 0;
2428 }
2429 
2430 
2431 static int
lower_callback(struct notifier_block * nb,unsigned long val,void * v)2432 lower_callback(struct notifier_block *nb, unsigned long val, void *v)
2433 {
2434 	capi_profile profile;
2435 	u32 contr = (long)v;
2436 
2437 	switch (val) {
2438 	case CAPICTR_UP:
2439 		printk(KERN_INFO "capidrv: controller %hu up\n", contr);
2440 		if (capi20_get_profile(contr, &profile) == CAPI_NOERROR)
2441 			(void) capidrv_addcontr(contr, &profile);
2442 		break;
2443 	case CAPICTR_DOWN:
2444 		printk(KERN_INFO "capidrv: controller %hu down\n", contr);
2445 		(void) capidrv_delcontr(contr);
2446 		break;
2447 	}
2448 	return NOTIFY_OK;
2449 }
2450 
2451 /*
2452  * /proc/capi/capidrv:
2453  * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
2454  */
capidrv_proc_show(struct seq_file * m,void * v)2455 static int __maybe_unused capidrv_proc_show(struct seq_file *m, void *v)
2456 {
2457 	seq_printf(m, "%lu %lu %lu %lu\n",
2458 		   global.ap.nrecvctlpkt,
2459 		   global.ap.nrecvdatapkt,
2460 		   global.ap.nsentctlpkt,
2461 		   global.ap.nsentdatapkt);
2462 	return 0;
2463 }
2464 
proc_init(void)2465 static void __init proc_init(void)
2466 {
2467 	proc_create_single("capi/capidrv", 0, NULL, capidrv_proc_show);
2468 }
2469 
proc_exit(void)2470 static void __exit proc_exit(void)
2471 {
2472 	remove_proc_entry("capi/capidrv", NULL);
2473 }
2474 
2475 static struct notifier_block capictr_nb = {
2476 	.notifier_call = lower_callback,
2477 };
2478 
capidrv_init(void)2479 static int __init capidrv_init(void)
2480 {
2481 	capi_profile profile;
2482 	u32 ncontr, contr;
2483 	u16 errcode;
2484 
2485 	global.ap.rparam.level3cnt = -2;  /* number of bchannels twice */
2486 	global.ap.rparam.datablkcnt = 16;
2487 	global.ap.rparam.datablklen = 2048;
2488 
2489 	global.ap.recv_message = capidrv_recv_message;
2490 	errcode = capi20_register(&global.ap);
2491 	if (errcode) {
2492 		return -EIO;
2493 	}
2494 
2495 	register_capictr_notifier(&capictr_nb);
2496 
2497 	errcode = capi20_get_profile(0, &profile);
2498 	if (errcode != CAPI_NOERROR) {
2499 		unregister_capictr_notifier(&capictr_nb);
2500 		capi20_release(&global.ap);
2501 		return -EIO;
2502 	}
2503 
2504 	ncontr = profile.ncontroller;
2505 	for (contr = 1; contr <= ncontr; contr++) {
2506 		errcode = capi20_get_profile(contr, &profile);
2507 		if (errcode != CAPI_NOERROR)
2508 			continue;
2509 		(void) capidrv_addcontr(contr, &profile);
2510 	}
2511 	proc_init();
2512 
2513 	return 0;
2514 }
2515 
capidrv_exit(void)2516 static void __exit capidrv_exit(void)
2517 {
2518 	unregister_capictr_notifier(&capictr_nb);
2519 	capi20_release(&global.ap);
2520 
2521 	proc_exit();
2522 }
2523 
2524 module_init(capidrv_init);
2525 module_exit(capidrv_exit);
2526