[PATCH] some isdn invalid/illegal fixups
[linux-2.6/history.git] / drivers / isdn / capi / capidrv.c
blob8ea64ec7eea39d48118169b90071c94fd2647b19
1 /* $Id: capidrv.c,v 1.39.6.7 2001/09/23 22:24:33 kai Exp $
3 * ISDN4Linux Driver, using capi20 interface (kernelcapi)
5 * Copyright 1997 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.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/capi.h>
29 #include <linux/kernelcapi.h>
30 #include <linux/ctype.h>
31 #include <linux/init.h>
33 #include <linux/isdn/capiutil.h>
34 #include <linux/isdn/capicmd.h>
35 #include "capidrv.h"
37 static char *revision = "$Revision: 1.39.6.7 $";
38 static int debugmode = 0;
40 MODULE_DESCRIPTION("CAPI4Linux: Interface to ISDN4Linux");
41 MODULE_AUTHOR("Carsten Paeth");
42 MODULE_LICENSE("GPL");
43 MODULE_PARM(debugmode, "i");
45 /* -------- type definitions ----------------------------------------- */
48 struct capidrv_contr {
50 struct capidrv_contr *next;
52 u32 contrnr;
53 char name[20];
56 * for isdn4linux
58 isdn_if interface;
59 int myid;
62 * LISTEN state
64 int state;
65 u32 cipmask;
66 u32 cipmask2;
67 struct timer_list listentimer;
70 * ID of capi message sent
72 u16 msgid;
75 * B-Channels
77 int nbchan;
78 struct capidrv_bchan {
79 struct capidrv_contr *contr;
80 u8 msn[ISDN_MSNLEN];
81 int l2;
82 int l3;
83 u8 num[ISDN_MSNLEN];
84 u8 mynum[ISDN_MSNLEN];
85 int si1;
86 int si2;
87 int incoming;
88 int disconnecting;
89 struct capidrv_plci {
90 struct capidrv_plci *next;
91 u32 plci;
92 u32 ncci; /* ncci for CONNECT_ACTIVE_IND */
93 u16 msgid; /* to identfy CONNECT_CONF */
94 int chan;
95 int state;
96 int leasedline;
97 struct capidrv_ncci {
98 struct capidrv_ncci *next;
99 struct capidrv_plci *plcip;
100 u32 ncci;
101 u16 msgid; /* to identfy CONNECT_B3_CONF */
102 int chan;
103 int state;
104 int oldstate;
105 /* */
106 u16 datahandle;
107 struct ncci_datahandle_queue {
108 struct ncci_datahandle_queue *next;
109 u16 datahandle;
110 int len;
111 } *ackqueue;
112 } *ncci_list;
113 } *plcip;
114 struct capidrv_ncci *nccip;
115 } *bchans;
117 struct capidrv_plci *plci_list;
119 /* for q931 data */
120 u8 q931_buf[4096];
121 u8 *q931_read;
122 u8 *q931_write;
123 u8 *q931_end;
127 struct capidrv_data {
128 struct capi20_appl ap;
129 int ncontr;
130 struct capidrv_contr *contr_list;
133 typedef struct capidrv_plci capidrv_plci;
134 typedef struct capidrv_ncci capidrv_ncci;
135 typedef struct capidrv_contr capidrv_contr;
136 typedef struct capidrv_data capidrv_data;
137 typedef struct capidrv_bchan capidrv_bchan;
139 /* -------- data definitions ----------------------------------------- */
141 static capidrv_data global;
142 static spinlock_t global_lock = SPIN_LOCK_UNLOCKED;
144 static void handle_dtrace_data(capidrv_contr *card,
145 int send, int level2, u8 *data, u16 len);
147 /* -------- convert functions ---------------------------------------- */
149 static inline u32 b1prot(int l2, int l3)
151 switch (l2) {
152 case ISDN_PROTO_L2_X75I:
153 case ISDN_PROTO_L2_X75UI:
154 case ISDN_PROTO_L2_X75BUI:
155 return 0;
156 case ISDN_PROTO_L2_HDLC:
157 default:
158 return 0;
159 case ISDN_PROTO_L2_TRANS:
160 return 1;
161 case ISDN_PROTO_L2_V11096:
162 case ISDN_PROTO_L2_V11019:
163 case ISDN_PROTO_L2_V11038:
164 return 2;
165 case ISDN_PROTO_L2_FAX:
166 return 4;
167 case ISDN_PROTO_L2_MODEM:
168 return 8;
172 static inline u32 b2prot(int l2, int l3)
174 switch (l2) {
175 case ISDN_PROTO_L2_X75I:
176 case ISDN_PROTO_L2_X75UI:
177 case ISDN_PROTO_L2_X75BUI:
178 default:
179 return 0;
180 case ISDN_PROTO_L2_HDLC:
181 case ISDN_PROTO_L2_TRANS:
182 case ISDN_PROTO_L2_V11096:
183 case ISDN_PROTO_L2_V11019:
184 case ISDN_PROTO_L2_V11038:
185 case ISDN_PROTO_L2_MODEM:
186 return 1;
187 case ISDN_PROTO_L2_FAX:
188 return 4;
192 static inline u32 b3prot(int l2, int l3)
194 switch (l2) {
195 case ISDN_PROTO_L2_X75I:
196 case ISDN_PROTO_L2_X75UI:
197 case ISDN_PROTO_L2_X75BUI:
198 case ISDN_PROTO_L2_HDLC:
199 case ISDN_PROTO_L2_TRANS:
200 case ISDN_PROTO_L2_V11096:
201 case ISDN_PROTO_L2_V11019:
202 case ISDN_PROTO_L2_V11038:
203 case ISDN_PROTO_L2_MODEM:
204 default:
205 return 0;
206 case ISDN_PROTO_L2_FAX:
207 return 4;
211 static _cstruct b1config_async_v110(u16 rate)
213 /* CAPI-Spec "B1 Configuration" */
214 static unsigned char buf[9];
215 buf[0] = 8; /* len */
216 /* maximum bitrate */
217 buf[1] = rate & 0xff; buf[2] = (rate >> 8) & 0xff;
218 buf[3] = 8; buf[4] = 0; /* 8 bits per character */
219 buf[5] = 0; buf[6] = 0; /* parity none */
220 buf[7] = 0; buf[8] = 0; /* 1 stop bit */
221 return buf;
224 static _cstruct b1config(int l2, int l3)
226 switch (l2) {
227 case ISDN_PROTO_L2_X75I:
228 case ISDN_PROTO_L2_X75UI:
229 case ISDN_PROTO_L2_X75BUI:
230 case ISDN_PROTO_L2_HDLC:
231 case ISDN_PROTO_L2_TRANS:
232 default:
233 return 0;
234 case ISDN_PROTO_L2_V11096:
235 return b1config_async_v110(9600);
236 case ISDN_PROTO_L2_V11019:
237 return b1config_async_v110(19200);
238 case ISDN_PROTO_L2_V11038:
239 return b1config_async_v110(38400);
243 static inline u16 si2cip(u8 si1, u8 si2)
245 static const u8 cip[17][5] =
247 /* 0 1 2 3 4 */
248 {0, 0, 0, 0, 0}, /*0 */
249 {16, 16, 4, 26, 16}, /*1 */
250 {17, 17, 17, 4, 4}, /*2 */
251 {2, 2, 2, 2, 2}, /*3 */
252 {18, 18, 18, 18, 18}, /*4 */
253 {2, 2, 2, 2, 2}, /*5 */
254 {0, 0, 0, 0, 0}, /*6 */
255 {2, 2, 2, 2, 2}, /*7 */
256 {2, 2, 2, 2, 2}, /*8 */
257 {21, 21, 21, 21, 21}, /*9 */
258 {19, 19, 19, 19, 19}, /*10 */
259 {0, 0, 0, 0, 0}, /*11 */
260 {0, 0, 0, 0, 0}, /*12 */
261 {0, 0, 0, 0, 0}, /*13 */
262 {0, 0, 0, 0, 0}, /*14 */
263 {22, 22, 22, 22, 22}, /*15 */
264 {27, 27, 27, 28, 27} /*16 */
266 if (si1 > 16)
267 si1 = 0;
268 if (si2 > 4)
269 si2 = 0;
271 return (u16) cip[si1][si2];
274 static inline u8 cip2si1(u16 cipval)
276 static const u8 si[32] =
277 {7, 1, 7, 7, 1, 1, 7, 7, /*0-7 */
278 7, 1, 0, 0, 0, 0, 0, 0, /*8-15 */
279 1, 2, 4, 10, 9, 9, 15, 7, /*16-23 */
280 7, 7, 1, 16, 16, 0, 0, 0}; /*24-31 */
282 if (cipval > 31)
283 cipval = 0; /* .... */
284 return si[cipval];
287 static inline u8 cip2si2(u16 cipval)
289 static const u8 si[32] =
290 {0, 0, 0, 0, 2, 3, 0, 0, /*0-7 */
291 0, 3, 0, 0, 0, 0, 0, 0, /*8-15 */
292 1, 2, 0, 0, 9, 0, 0, 0, /*16-23 */
293 0, 0, 3, 2, 3, 0, 0, 0}; /*24-31 */
295 if (cipval > 31)
296 cipval = 0; /* .... */
297 return si[cipval];
301 /* -------- controller management ------------------------------------- */
303 static inline capidrv_contr *findcontrbydriverid(int driverid)
305 unsigned long flags;
306 capidrv_contr *p;
308 spin_lock_irqsave(&global_lock, flags);
309 for (p = global.contr_list; p; p = p->next)
310 if (p->myid == driverid)
311 break;
312 spin_unlock_irqrestore(&global_lock, flags);
313 return p;
316 static capidrv_contr *findcontrbynumber(u32 contr)
318 unsigned long flags;
319 capidrv_contr *p = global.contr_list;
321 spin_lock_irqsave(&global_lock, flags);
322 for (p = global.contr_list; p; p = p->next)
323 if (p->contrnr == contr)
324 break;
325 spin_unlock_irqrestore(&global_lock, flags);
326 return p;
330 /* -------- plci management ------------------------------------------ */
332 static capidrv_plci *new_plci(capidrv_contr * card, int chan)
334 capidrv_plci *plcip;
336 plcip = (capidrv_plci *) kmalloc(sizeof(capidrv_plci), GFP_ATOMIC);
338 if (plcip == 0)
339 return 0;
341 memset(plcip, 0, sizeof(capidrv_plci));
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;
350 return plcip;
353 static capidrv_plci *find_plci_by_plci(capidrv_contr * card, u32 plci)
355 capidrv_plci *p;
356 for (p = card->plci_list; p; p = p->next)
357 if (p->plci == plci)
358 return p;
359 return 0;
362 static capidrv_plci *find_plci_by_msgid(capidrv_contr * card, u16 msgid)
364 capidrv_plci *p;
365 for (p = card->plci_list; p; p = p->next)
366 if (p->msgid == msgid)
367 return p;
368 return 0;
371 static capidrv_plci *find_plci_by_ncci(capidrv_contr * card, u32 ncci)
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 0;
380 static void free_plci(capidrv_contr * card, capidrv_plci * plcip)
382 capidrv_plci **pp;
384 for (pp = &card->plci_list; *pp; pp = &(*pp)->next) {
385 if (*pp == plcip) {
386 *pp = (*pp)->next;
387 card->bchans[plcip->chan].plcip = 0;
388 card->bchans[plcip->chan].disconnecting = 0;
389 card->bchans[plcip->chan].incoming = 0;
390 kfree(plcip);
391 return;
394 printk(KERN_ERR "capidrv-%d: free_plci %p (0x%x) not found, Huh?\n",
395 card->contrnr, plcip, plcip->plci);
398 /* -------- ncci management ------------------------------------------ */
400 static inline capidrv_ncci *new_ncci(capidrv_contr * card,
401 capidrv_plci * plcip,
402 u32 ncci)
404 capidrv_ncci *nccip;
406 nccip = (capidrv_ncci *) kmalloc(sizeof(capidrv_ncci), GFP_ATOMIC);
408 if (nccip == 0)
409 return 0;
411 memset(nccip, 0, sizeof(capidrv_ncci));
412 nccip->ncci = ncci;
413 nccip->state = ST_NCCI_NONE;
414 nccip->plcip = plcip;
415 nccip->chan = plcip->chan;
416 nccip->datahandle = 0;
418 nccip->next = plcip->ncci_list;
419 plcip->ncci_list = nccip;
421 card->bchans[plcip->chan].nccip = nccip;
423 return nccip;
426 static inline capidrv_ncci *find_ncci(capidrv_contr * card, u32 ncci)
428 capidrv_plci *plcip;
429 capidrv_ncci *p;
431 if ((plcip = find_plci_by_ncci(card, ncci)) == 0)
432 return 0;
434 for (p = plcip->ncci_list; p; p = p->next)
435 if (p->ncci == ncci)
436 return p;
437 return 0;
440 static inline capidrv_ncci *find_ncci_by_msgid(capidrv_contr * card,
441 u32 ncci, u16 msgid)
443 capidrv_plci *plcip;
444 capidrv_ncci *p;
446 if ((plcip = find_plci_by_ncci(card, ncci)) == 0)
447 return 0;
449 for (p = plcip->ncci_list; p; p = p->next)
450 if (p->msgid == msgid)
451 return p;
452 return 0;
455 static void free_ncci(capidrv_contr * card, struct capidrv_ncci *nccip)
457 struct capidrv_ncci **pp;
459 for (pp = &(nccip->plcip->ncci_list); *pp; pp = &(*pp)->next) {
460 if (*pp == nccip) {
461 *pp = (*pp)->next;
462 break;
465 card->bchans[nccip->chan].nccip = 0;
466 kfree(nccip);
469 static int capidrv_add_ack(struct capidrv_ncci *nccip,
470 u16 datahandle, int len)
472 struct ncci_datahandle_queue *n, **pp;
474 n = (struct ncci_datahandle_queue *)
475 kmalloc(sizeof(struct ncci_datahandle_queue), GFP_ATOMIC);
476 if (!n) {
477 printk(KERN_ERR "capidrv: kmalloc ncci_datahandle failed\n");
478 return -1;
480 n->next = 0;
481 n->datahandle = datahandle;
482 n->len = len;
483 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) ;
484 *pp = n;
485 return 0;
488 static int capidrv_del_ack(struct capidrv_ncci *nccip, u16 datahandle)
490 struct ncci_datahandle_queue **pp, *p;
491 int len;
493 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) {
494 if ((*pp)->datahandle == datahandle) {
495 p = *pp;
496 len = p->len;
497 *pp = (*pp)->next;
498 kfree(p);
499 return len;
502 return -1;
505 /* -------- convert and send capi message ---------------------------- */
507 static void send_message(capidrv_contr * card, _cmsg * cmsg)
509 struct sk_buff *skb;
510 size_t len;
511 capi_cmsg2message(cmsg, cmsg->buf);
512 len = CAPIMSG_LEN(cmsg->buf);
513 skb = alloc_skb(len, GFP_ATOMIC);
514 memcpy(skb_put(skb, len), cmsg->buf, len);
515 capi20_put_message(&global.ap, skb);
518 /* -------- state machine -------------------------------------------- */
520 struct listenstatechange {
521 int actstate;
522 int nextstate;
523 int event;
526 static struct listenstatechange listentable[] =
528 {ST_LISTEN_NONE, ST_LISTEN_WAIT_CONF, EV_LISTEN_REQ},
529 {ST_LISTEN_ACTIVE, ST_LISTEN_ACTIVE_WAIT_CONF, EV_LISTEN_REQ},
530 {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_ERROR},
531 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_ERROR},
532 {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
533 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
534 {ST_LISTEN_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
535 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
539 static void listen_change_state(capidrv_contr * card, int event)
541 struct listenstatechange *p = listentable;
542 while (p->event) {
543 if (card->state == p->actstate && p->event == event) {
544 if (debugmode)
545 printk(KERN_DEBUG "capidrv-%d: listen_change_state %d -> %d\n",
546 card->contrnr, card->state, p->nextstate);
547 card->state = p->nextstate;
548 return;
550 p++;
552 printk(KERN_ERR "capidrv-%d: listen_change_state state=%d event=%d ????\n",
553 card->contrnr, card->state, event);
557 /* ------------------------------------------------------------------ */
559 static void p0(capidrv_contr * card, capidrv_plci * plci)
561 isdn_ctrl cmd;
563 card->bchans[plci->chan].contr = 0;
564 cmd.command = ISDN_STAT_DHUP;
565 cmd.driver = card->myid;
566 cmd.arg = plci->chan;
567 card->interface.statcallb(&cmd);
568 free_plci(card, plci);
571 /* ------------------------------------------------------------------ */
573 struct plcistatechange {
574 int actstate;
575 int nextstate;
576 int event;
577 void (*changefunc) (capidrv_contr * card, capidrv_plci * plci);
580 static struct plcistatechange plcitable[] =
582 /* P-0 */
583 {ST_PLCI_NONE, ST_PLCI_OUTGOING, EV_PLCI_CONNECT_REQ, 0},
584 {ST_PLCI_NONE, ST_PLCI_ALLOCATED, EV_PLCI_FACILITY_IND_UP, 0},
585 {ST_PLCI_NONE, ST_PLCI_INCOMING, EV_PLCI_CONNECT_IND, 0},
586 {ST_PLCI_NONE, ST_PLCI_RESUMEING, EV_PLCI_RESUME_REQ, 0},
587 /* P-0.1 */
588 {ST_PLCI_OUTGOING, ST_PLCI_NONE, EV_PLCI_CONNECT_CONF_ERROR, p0},
589 {ST_PLCI_OUTGOING, ST_PLCI_ALLOCATED, EV_PLCI_CONNECT_CONF_OK, 0},
590 /* P-1 */
591 {ST_PLCI_ALLOCATED, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, 0},
592 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0},
593 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0},
594 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0},
595 /* P-ACT */
596 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0},
597 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0},
598 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0},
599 {ST_PLCI_ACTIVE, ST_PLCI_HELD, EV_PLCI_HOLD_IND, 0},
600 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_SUSPEND_IND, 0},
601 /* P-2 */
602 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, 0},
603 {ST_PLCI_INCOMING, ST_PLCI_FACILITY_IND, EV_PLCI_FACILITY_IND_UP, 0},
604 {ST_PLCI_INCOMING, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_RESP, 0},
605 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0},
606 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0},
607 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0},
608 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CD_IND, 0},
609 /* P-3 */
610 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, 0},
611 {ST_PLCI_FACILITY_IND, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_ACTIVE_IND, 0},
612 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0},
613 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0},
614 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0},
615 /* P-4 */
616 {ST_PLCI_ACCEPTING, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, 0},
617 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0},
618 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0},
619 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0},
620 /* P-5 */
621 {ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0},
622 /* P-6 */
623 {ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0},
624 /* P-0.Res */
625 {ST_PLCI_RESUMEING, ST_PLCI_NONE, EV_PLCI_RESUME_CONF_ERROR, p0},
626 {ST_PLCI_RESUMEING, ST_PLCI_RESUME, EV_PLCI_RESUME_CONF_OK, 0},
627 /* P-RES */
628 {ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, 0},
629 /* P-HELD */
630 {ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, 0},
634 static void plci_change_state(capidrv_contr * card, capidrv_plci * plci, int event)
636 struct plcistatechange *p = plcitable;
637 while (p->event) {
638 if (plci->state == p->actstate && p->event == event) {
639 if (debugmode)
640 printk(KERN_DEBUG "capidrv-%d: plci_change_state:0x%x %d -> %d\n",
641 card->contrnr, plci->plci, plci->state, p->nextstate);
642 plci->state = p->nextstate;
643 if (p->changefunc)
644 p->changefunc(card, plci);
645 return;
647 p++;
649 printk(KERN_ERR "capidrv-%d: plci_change_state:0x%x state=%d event=%d ????\n",
650 card->contrnr, plci->plci, plci->state, event);
653 /* ------------------------------------------------------------------ */
655 static _cmsg cmsg;
657 static void n0(capidrv_contr * card, capidrv_ncci * ncci)
659 isdn_ctrl cmd;
661 capi_fill_DISCONNECT_REQ(&cmsg,
662 global.ap.applid,
663 card->msgid++,
664 ncci->plcip->plci,
665 0, /* BChannelinformation */
666 0, /* Keypadfacility */
667 0, /* Useruserdata */ /* $$$$ */
668 0 /* Facilitydataarray */
670 send_message(card, &cmsg);
671 plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ);
673 cmd.command = ISDN_STAT_BHUP;
674 cmd.driver = card->myid;
675 cmd.arg = ncci->chan;
676 card->interface.statcallb(&cmd);
677 free_ncci(card, ncci);
680 /* ------------------------------------------------------------------ */
682 struct nccistatechange {
683 int actstate;
684 int nextstate;
685 int event;
686 void (*changefunc) (capidrv_contr * card, capidrv_ncci * ncci);
689 static struct nccistatechange nccitable[] =
691 /* N-0 */
692 {ST_NCCI_NONE, ST_NCCI_OUTGOING, EV_NCCI_CONNECT_B3_REQ, 0},
693 {ST_NCCI_NONE, ST_NCCI_INCOMING, EV_NCCI_CONNECT_B3_IND, 0},
694 /* N-0.1 */
695 {ST_NCCI_OUTGOING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_CONF_OK, 0},
696 {ST_NCCI_OUTGOING, ST_NCCI_NONE, EV_NCCI_CONNECT_B3_CONF_ERROR, n0},
697 /* N-1 */
698 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_CONNECT_B3_REJECT, 0},
699 {ST_NCCI_INCOMING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_RESP, 0},
700 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0},
701 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0},
702 /* N-2 */
703 {ST_NCCI_ALLOCATED, ST_NCCI_ACTIVE, EV_NCCI_CONNECT_B3_ACTIVE_IND, 0},
704 {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0},
705 {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0},
706 /* N-ACT */
707 {ST_NCCI_ACTIVE, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, 0},
708 {ST_NCCI_ACTIVE, ST_NCCI_RESETING, EV_NCCI_RESET_B3_REQ, 0},
709 {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0},
710 {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0},
711 /* N-3 */
712 {ST_NCCI_RESETING, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, 0},
713 {ST_NCCI_RESETING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0},
714 {ST_NCCI_RESETING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0},
715 /* N-4 */
716 {ST_NCCI_DISCONNECTING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0},
717 {ST_NCCI_DISCONNECTING, ST_NCCI_PREVIOUS, EV_NCCI_DISCONNECT_B3_CONF_ERROR,0},
718 /* N-5 */
719 {ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0},
723 static void ncci_change_state(capidrv_contr * card, capidrv_ncci * ncci, int event)
725 struct nccistatechange *p = nccitable;
726 while (p->event) {
727 if (ncci->state == p->actstate && p->event == event) {
728 if (debugmode)
729 printk(KERN_DEBUG "capidrv-%d: ncci_change_state:0x%x %d -> %d\n",
730 card->contrnr, ncci->ncci, ncci->state, p->nextstate);
731 if (p->nextstate == ST_NCCI_PREVIOUS) {
732 ncci->state = ncci->oldstate;
733 ncci->oldstate = p->actstate;
734 } else {
735 ncci->oldstate = p->actstate;
736 ncci->state = p->nextstate;
738 if (p->changefunc)
739 p->changefunc(card, ncci);
740 return;
742 p++;
744 printk(KERN_ERR "capidrv-%d: ncci_change_state:0x%x state=%d event=%d ????\n",
745 card->contrnr, ncci->ncci, ncci->state, event);
748 /* ------------------------------------------------------------------- */
750 static inline int new_bchan(capidrv_contr * card)
752 int i;
753 for (i = 0; i < card->nbchan; i++) {
754 if (card->bchans[i].plcip == 0) {
755 card->bchans[i].disconnecting = 0;
756 return i;
759 return -1;
762 /* ------------------------------------------------------------------- */
764 static void handle_controller(_cmsg * cmsg)
766 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
768 if (!card) {
769 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
770 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
771 cmsg->adr.adrController & 0x7f);
772 return;
774 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
776 case CAPI_LISTEN_CONF: /* Controller */
777 if (debugmode)
778 printk(KERN_DEBUG "capidrv-%d: listenconf Info=0x%4x (%s) cipmask=0x%x\n",
779 card->contrnr, cmsg->Info, capi_info2str(cmsg->Info), card->cipmask);
780 if (cmsg->Info) {
781 listen_change_state(card, EV_LISTEN_CONF_ERROR);
782 } else if (card->cipmask == 0) {
783 listen_change_state(card, EV_LISTEN_CONF_EMPTY);
784 } else {
785 listen_change_state(card, EV_LISTEN_CONF_OK);
787 break;
789 case CAPI_MANUFACTURER_IND: /* Controller */
790 if ( cmsg->ManuID == 0x214D5641
791 && cmsg->Class == 0
792 && cmsg->Function == 1) {
793 u8 *data = cmsg->ManuData+3;
794 u16 len = cmsg->ManuData[0];
795 u16 layer;
796 int direction;
797 if (len == 255) {
798 len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
799 data += 2;
801 len -= 2;
802 layer = ((*(data-1)) << 8) | *(data-2);
803 if (layer & 0x300)
804 direction = (layer & 0x200) ? 0 : 1;
805 else direction = (layer & 0x800) ? 0 : 1;
806 if (layer & 0x0C00) {
807 if ((layer & 0xff) == 0x80) {
808 handle_dtrace_data(card, direction, 1, data, len);
809 break;
811 } else if ((layer & 0xff) < 0x80) {
812 handle_dtrace_data(card, direction, 0, data, len);
813 break;
815 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
816 card->contrnr,
817 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
818 cmsg->adr.adrController, layer);
819 break;
821 goto ignored;
822 case CAPI_MANUFACTURER_CONF: /* Controller */
823 if (cmsg->ManuID == 0x214D5641) {
824 char *s = 0;
825 switch (cmsg->Class) {
826 case 0: break;
827 case 1: s = "unknown class"; break;
828 case 2: s = "unknown function"; break;
829 default: s = "unkown error"; break;
831 if (s)
832 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
833 card->contrnr,
834 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
835 cmsg->adr.adrController,
836 cmsg->Function, s);
837 break;
839 goto ignored;
840 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
841 goto ignored;
842 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
843 goto ignored;
844 case CAPI_INFO_IND: /* Controller/plci */
845 goto ignored;
846 case CAPI_INFO_CONF: /* Controller/plci */
847 goto ignored;
849 default:
850 printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
851 card->contrnr,
852 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
853 cmsg->adr.adrController);
855 return;
857 ignored:
858 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
859 card->contrnr,
860 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
861 cmsg->adr.adrController);
864 static void handle_incoming_call(capidrv_contr * card, _cmsg * cmsg)
866 capidrv_plci *plcip;
867 capidrv_bchan *bchan;
868 isdn_ctrl cmd;
869 int chan;
871 if ((chan = new_bchan(card)) == -1) {
872 printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
873 return;
875 bchan = &card->bchans[chan];
876 if ((plcip = new_plci(card, chan)) == 0) {
877 printk(KERN_ERR "capidrv-%d: incoming call: no memory, sorry.\n", card->contrnr);
878 return;
880 bchan->incoming = 1;
881 plcip->plci = cmsg->adr.adrPLCI;
882 plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
884 cmd.command = ISDN_STAT_ICALL;
885 cmd.driver = card->myid;
886 cmd.arg = chan;
887 memset(&cmd.parm.setup, 0, sizeof(cmd.parm.setup));
888 strncpy(cmd.parm.setup.phone,
889 cmsg->CallingPartyNumber + 3,
890 cmsg->CallingPartyNumber[0] - 2);
891 strncpy(cmd.parm.setup.eazmsn,
892 cmsg->CalledPartyNumber + 2,
893 cmsg->CalledPartyNumber[0] - 1);
894 cmd.parm.setup.si1 = cip2si1(cmsg->CIPValue);
895 cmd.parm.setup.si2 = cip2si2(cmsg->CIPValue);
896 cmd.parm.setup.plan = cmsg->CallingPartyNumber[1];
897 cmd.parm.setup.screen = cmsg->CallingPartyNumber[2];
899 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n",
900 card->contrnr,
901 cmd.parm.setup.phone,
902 cmd.parm.setup.si1,
903 cmd.parm.setup.si2,
904 cmd.parm.setup.eazmsn);
906 if (cmd.parm.setup.si1 == 1 && cmd.parm.setup.si2 != 0) {
907 printk(KERN_INFO "capidrv-%d: patching si2=%d to 0 for VBOX\n",
908 card->contrnr,
909 cmd.parm.setup.si2);
910 cmd.parm.setup.si2 = 0;
913 switch (card->interface.statcallb(&cmd)) {
914 case 0:
915 case 3:
916 /* No device matching this call.
917 * and isdn_common.c has send a HANGUP command
918 * which is ignored in state ST_PLCI_INCOMING,
919 * so we send RESP to ignore the call
921 capi_cmsg_answer(cmsg);
922 cmsg->Reject = 1; /* ignore */
923 send_message(card, cmsg);
924 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
925 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s ignored\n",
926 card->contrnr,
927 cmd.parm.setup.phone,
928 cmd.parm.setup.si1,
929 cmd.parm.setup.si2,
930 cmd.parm.setup.eazmsn);
931 break;
932 case 1:
933 /* At least one device matching this call (RING on ttyI)
934 * HL-driver may send ALERTING on the D-channel in this
935 * case.
936 * really means: RING on ttyI or a net interface
937 * accepted this call already.
939 * If the call was accepted, state has already changed,
940 * and CONNECT_RESP already sent.
942 if (plcip->state == ST_PLCI_INCOMING) {
943 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
944 card->contrnr,
945 cmd.parm.setup.phone,
946 cmd.parm.setup.si1,
947 cmd.parm.setup.si2,
948 cmd.parm.setup.eazmsn);
949 capi_fill_ALERT_REQ(cmsg,
950 global.ap.applid,
951 card->msgid++,
952 plcip->plci, /* adr */
953 0, /* BChannelinformation */
954 0, /* Keypadfacility */
955 0, /* Useruserdata */
956 0 /* Facilitydataarray */
958 plcip->msgid = cmsg->Messagenumber;
959 send_message(card, cmsg);
960 } else {
961 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
962 card->contrnr,
963 cmd.parm.setup.phone,
964 cmd.parm.setup.si1,
965 cmd.parm.setup.si2,
966 cmd.parm.setup.eazmsn);
968 break;
970 case 2: /* Call will be rejected. */
971 capi_cmsg_answer(cmsg);
972 cmsg->Reject = 2; /* reject call, normal call clearing */
973 send_message(card, cmsg);
974 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
975 break;
977 default:
978 /* An error happened. (Invalid parameters for example.) */
979 capi_cmsg_answer(cmsg);
980 cmsg->Reject = 8; /* reject call,
981 destination out of order */
982 send_message(card, cmsg);
983 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
984 break;
986 return;
989 static void handle_plci(_cmsg * cmsg)
991 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
992 capidrv_plci *plcip;
993 isdn_ctrl cmd;
995 if (!card) {
996 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
997 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
998 cmsg->adr.adrController & 0x7f);
999 return;
1001 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1003 case CAPI_DISCONNECT_IND: /* plci */
1004 if (cmsg->Reason) {
1005 printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
1006 card->contrnr,
1007 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1008 cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
1010 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
1011 capi_cmsg_answer(cmsg);
1012 send_message(card, cmsg);
1013 goto notfound;
1015 card->bchans[plcip->chan].disconnecting = 1;
1016 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
1017 capi_cmsg_answer(cmsg);
1018 send_message(card, cmsg);
1019 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
1020 break;
1022 case CAPI_DISCONNECT_CONF: /* plci */
1023 if (cmsg->Info) {
1024 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1025 card->contrnr,
1026 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1027 cmsg->Info, capi_info2str(cmsg->Info),
1028 cmsg->adr.adrPLCI);
1030 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1031 goto notfound;
1033 card->bchans[plcip->chan].disconnecting = 1;
1034 break;
1036 case CAPI_ALERT_CONF: /* plci */
1037 if (cmsg->Info) {
1038 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1039 card->contrnr,
1040 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1041 cmsg->Info, capi_info2str(cmsg->Info),
1042 cmsg->adr.adrPLCI);
1044 break;
1046 case CAPI_CONNECT_IND: /* plci */
1047 handle_incoming_call(card, cmsg);
1048 break;
1050 case CAPI_CONNECT_CONF: /* plci */
1051 if (cmsg->Info) {
1052 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1053 card->contrnr,
1054 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1055 cmsg->Info, capi_info2str(cmsg->Info),
1056 cmsg->adr.adrPLCI);
1058 if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
1059 goto notfound;
1061 plcip->plci = cmsg->adr.adrPLCI;
1062 if (cmsg->Info) {
1063 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
1064 } else {
1065 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
1067 break;
1069 case CAPI_CONNECT_ACTIVE_IND: /* plci */
1071 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1072 goto notfound;
1074 if (card->bchans[plcip->chan].incoming) {
1075 capi_cmsg_answer(cmsg);
1076 send_message(card, cmsg);
1077 plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1078 } else {
1079 capidrv_ncci *nccip;
1080 capi_cmsg_answer(cmsg);
1081 send_message(card, cmsg);
1083 nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
1085 if (!nccip) {
1086 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1087 break; /* $$$$ */
1089 capi_fill_CONNECT_B3_REQ(cmsg,
1090 global.ap.applid,
1091 card->msgid++,
1092 plcip->plci, /* adr */
1093 0 /* NCPI */
1095 nccip->msgid = cmsg->Messagenumber;
1096 send_message(card, cmsg);
1097 cmd.command = ISDN_STAT_DCONN;
1098 cmd.driver = card->myid;
1099 cmd.arg = plcip->chan;
1100 card->interface.statcallb(&cmd);
1101 plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1102 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
1104 break;
1106 case CAPI_INFO_IND: /* Controller/plci */
1108 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1109 goto notfound;
1111 if (cmsg->InfoNumber == 0x4000) {
1112 if (cmsg->InfoElement[0] == 4) {
1113 cmd.command = ISDN_STAT_CINF;
1114 cmd.driver = card->myid;
1115 cmd.arg = plcip->chan;
1116 sprintf(cmd.parm.num, "%lu",
1117 (unsigned long)
1118 ((u32) cmsg->InfoElement[1]
1119 | ((u32) (cmsg->InfoElement[2]) << 8)
1120 | ((u32) (cmsg->InfoElement[3]) << 16)
1121 | ((u32) (cmsg->InfoElement[4]) << 24)));
1122 card->interface.statcallb(&cmd);
1123 break;
1126 printk(KERN_ERR "capidrv-%d: %s\n",
1127 card->contrnr, capi_cmsg2str(cmsg));
1128 break;
1130 case CAPI_CONNECT_ACTIVE_CONF: /* plci */
1131 goto ignored;
1132 case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */
1133 goto ignored;
1134 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1135 goto ignored;
1136 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1137 goto ignored;
1139 case CAPI_INFO_CONF: /* Controller/plci */
1140 goto ignored;
1142 default:
1143 printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
1144 card->contrnr,
1145 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1146 cmsg->adr.adrPLCI);
1148 return;
1149 ignored:
1150 printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
1151 card->contrnr,
1152 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1153 cmsg->adr.adrPLCI);
1154 return;
1155 notfound:
1156 printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
1157 card->contrnr,
1158 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1159 cmsg->adr.adrPLCI);
1160 return;
1163 static void handle_ncci(_cmsg * cmsg)
1165 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1166 capidrv_plci *plcip;
1167 capidrv_ncci *nccip;
1168 isdn_ctrl cmd;
1169 int len;
1171 if (!card) {
1172 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1173 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1174 cmsg->adr.adrController & 0x7f);
1175 return;
1177 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1179 case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */
1180 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1181 goto notfound;
1183 capi_cmsg_answer(cmsg);
1184 send_message(card, cmsg);
1185 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
1187 cmd.command = ISDN_STAT_BCONN;
1188 cmd.driver = card->myid;
1189 cmd.arg = nccip->chan;
1190 card->interface.statcallb(&cmd);
1192 printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
1193 card->contrnr, nccip->chan, nccip->ncci);
1194 break;
1196 case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */
1197 goto ignored;
1199 case CAPI_CONNECT_B3_IND: /* ncci */
1201 plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
1202 if (plcip) {
1203 nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
1204 if (nccip) {
1205 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
1206 capi_fill_CONNECT_B3_RESP(cmsg,
1207 global.ap.applid,
1208 card->msgid++,
1209 nccip->ncci, /* adr */
1210 0, /* Reject */
1211 0 /* NCPI */
1213 send_message(card, cmsg);
1214 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
1215 break;
1217 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1218 } else {
1219 printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
1220 card->contrnr,
1221 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1222 cmsg->adr.adrNCCI);
1224 capi_fill_CONNECT_B3_RESP(cmsg,
1225 global.ap.applid,
1226 card->msgid++,
1227 cmsg->adr.adrNCCI,
1228 2, /* Reject */
1229 0 /* NCPI */
1231 send_message(card, cmsg);
1232 break;
1234 case CAPI_CONNECT_B3_CONF: /* ncci */
1236 if (!(nccip = find_ncci_by_msgid(card,
1237 cmsg->adr.adrNCCI,
1238 cmsg->Messagenumber)))
1239 goto notfound;
1241 nccip->ncci = cmsg->adr.adrNCCI;
1242 if (cmsg->Info) {
1243 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1244 card->contrnr,
1245 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1246 cmsg->Info, capi_info2str(cmsg->Info),
1247 cmsg->adr.adrNCCI);
1250 if (cmsg->Info)
1251 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
1252 else
1253 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
1254 break;
1256 case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */
1257 capi_cmsg_answer(cmsg);
1258 send_message(card, cmsg);
1259 break;
1261 case CAPI_DATA_B3_IND: /* ncci */
1262 /* handled in handle_data() */
1263 goto ignored;
1265 case CAPI_DATA_B3_CONF: /* ncci */
1266 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1267 goto notfound;
1269 len = capidrv_del_ack(nccip, cmsg->DataHandle);
1270 if (len < 0)
1271 break;
1272 cmd.command = ISDN_STAT_BSENT;
1273 cmd.driver = card->myid;
1274 cmd.arg = nccip->chan;
1275 cmd.parm.length = len;
1276 card->interface.statcallb(&cmd);
1277 break;
1279 case CAPI_DISCONNECT_B3_IND: /* ncci */
1280 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1281 goto notfound;
1283 card->bchans[nccip->chan].disconnecting = 1;
1284 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
1285 capi_cmsg_answer(cmsg);
1286 send_message(card, cmsg);
1287 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
1288 break;
1290 case CAPI_DISCONNECT_B3_CONF: /* ncci */
1291 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1292 goto notfound;
1293 if (cmsg->Info) {
1294 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1295 card->contrnr,
1296 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1297 cmsg->Info, capi_info2str(cmsg->Info),
1298 cmsg->adr.adrNCCI);
1299 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
1301 break;
1303 case CAPI_RESET_B3_IND: /* ncci */
1304 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1305 goto notfound;
1306 ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
1307 capi_cmsg_answer(cmsg);
1308 send_message(card, cmsg);
1309 break;
1311 case CAPI_RESET_B3_CONF: /* ncci */
1312 goto ignored; /* $$$$ */
1314 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1315 goto ignored;
1316 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1317 goto ignored;
1319 default:
1320 printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
1321 card->contrnr,
1322 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1323 cmsg->adr.adrNCCI);
1325 return;
1326 ignored:
1327 printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
1328 card->contrnr,
1329 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1330 cmsg->adr.adrNCCI);
1331 return;
1332 notfound:
1333 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1334 card->contrnr,
1335 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1336 cmsg->adr.adrNCCI);
1340 static void handle_data(_cmsg * cmsg, struct sk_buff *skb)
1342 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1343 capidrv_ncci *nccip;
1345 if (!card) {
1346 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1347 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1348 cmsg->adr.adrController & 0x7f);
1349 kfree_skb(skb);
1350 return;
1352 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
1353 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1354 card->contrnr,
1355 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1356 cmsg->adr.adrNCCI);
1357 kfree_skb(skb);
1358 return;
1360 (void) skb_pull(skb, CAPIMSG_LEN(skb->data));
1361 card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
1362 capi_cmsg_answer(cmsg);
1363 send_message(card, cmsg);
1366 static _cmsg s_cmsg;
1368 static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
1370 capi_message2cmsg(&s_cmsg, skb->data);
1371 if (debugmode > 2)
1372 printk(KERN_DEBUG "capidrv_signal: applid=%d %s\n",
1373 ap->applid, capi_cmsg2str(&s_cmsg));
1375 if (s_cmsg.Command == CAPI_DATA_B3
1376 && s_cmsg.Subcommand == CAPI_IND) {
1377 handle_data(&s_cmsg, skb);
1378 return;
1380 if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
1381 handle_controller(&s_cmsg);
1382 else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
1383 handle_plci(&s_cmsg);
1384 else
1385 handle_ncci(&s_cmsg);
1387 * data of skb used in s_cmsg,
1388 * free data when s_cmsg is not used again
1389 * thanks to Lars Heete <hel@admin.de>
1391 kfree_skb(skb);
1394 /* ------------------------------------------------------------------- */
1396 #define PUTBYTE_TO_STATUS(card, byte) \
1397 do { \
1398 *(card)->q931_write++ = (byte); \
1399 if ((card)->q931_write > (card)->q931_end) \
1400 (card)->q931_write = (card)->q931_buf; \
1401 } while (0)
1403 static void handle_dtrace_data(capidrv_contr *card,
1404 int send, int level2, u8 *data, u16 len)
1406 u8 *p, *end;
1407 isdn_ctrl cmd;
1409 if (!len) {
1410 printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
1411 card->contrnr, len);
1412 return;
1415 if (level2) {
1416 PUTBYTE_TO_STATUS(card, 'D');
1417 PUTBYTE_TO_STATUS(card, '2');
1418 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1419 PUTBYTE_TO_STATUS(card, ':');
1420 } else {
1421 PUTBYTE_TO_STATUS(card, 'D');
1422 PUTBYTE_TO_STATUS(card, '3');
1423 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1424 PUTBYTE_TO_STATUS(card, ':');
1427 for (p = data, end = data+len; p < end; p++) {
1428 u8 w;
1429 PUTBYTE_TO_STATUS(card, ' ');
1430 w = (*p >> 4) & 0xf;
1431 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1432 w = *p & 0xf;
1433 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1435 PUTBYTE_TO_STATUS(card, '\n');
1437 cmd.command = ISDN_STAT_STAVAIL;
1438 cmd.driver = card->myid;
1439 cmd.arg = len*3+5;
1440 card->interface.statcallb(&cmd);
1443 /* ------------------------------------------------------------------- */
1445 static _cmsg cmdcmsg;
1447 static int capidrv_ioctl(isdn_ctrl * c, capidrv_contr * card)
1449 switch (c->arg) {
1450 case 1:
1451 debugmode = (int)(*((unsigned int *)c->parm.num));
1452 printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
1453 card->contrnr, debugmode);
1454 return 0;
1455 default:
1456 printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
1457 card->contrnr, c->arg);
1458 return -EINVAL;
1460 return -EINVAL;
1464 * Handle leased lines (CAPI-Bundling)
1467 struct internal_bchannelinfo {
1468 unsigned short channelalloc;
1469 unsigned short operation;
1470 unsigned char cmask[31];
1473 static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
1475 unsigned long bmask = 0;
1476 int active = !0;
1477 char *s;
1478 int i;
1480 if (strncmp(teln, "FV:", 3) != 0)
1481 return 1;
1482 s = teln + 3;
1483 while (*s && *s == ' ') s++;
1484 if (!*s) return -2;
1485 if (*s == 'p' || *s == 'P') {
1486 active = 0;
1487 s++;
1489 if (*s == 'a' || *s == 'A') {
1490 active = !0;
1491 s++;
1493 while (*s) {
1494 int digit1 = 0;
1495 int digit2 = 0;
1496 if (!isdigit(*s)) return -3;
1497 while (isdigit(*s)) { digit1 = digit1*10 + (*s - '0'); s++; }
1498 if (digit1 <= 0 && digit1 > 30) return -4;
1499 if (*s == 0 || *s == ',' || *s == ' ') {
1500 bmask |= (1 << digit1);
1501 digit1 = 0;
1502 if (*s) s++;
1503 continue;
1505 if (*s != '-') return -5;
1506 s++;
1507 if (!isdigit(*s)) return -3;
1508 while (isdigit(*s)) { digit2 = digit2*10 + (*s - '0'); s++; }
1509 if (digit2 <= 0 && digit2 > 30) return -4;
1510 if (*s == 0 || *s == ',' || *s == ' ') {
1511 if (digit1 > digit2)
1512 for (i = digit2; i <= digit1 ; i++)
1513 bmask |= (1 << i);
1514 else
1515 for (i = digit1; i <= digit2 ; i++)
1516 bmask |= (1 << i);
1517 digit1 = digit2 = 0;
1518 if (*s) s++;
1519 continue;
1521 return -6;
1523 if (activep) *activep = active;
1524 if (bmaskp) *bmaskp = bmask;
1525 return 0;
1528 static int FVteln2capi20(char *teln, u8 AdditionalInfo[1+2+2+31])
1530 unsigned long bmask;
1531 int active;
1532 int rc, i;
1534 rc = decodeFVteln(teln, &bmask, &active);
1535 if (rc) return rc;
1536 /* Length */
1537 AdditionalInfo[0] = 2+2+31;
1538 /* Channel: 3 => use channel allocation */
1539 AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
1540 /* Operation: 0 => DTE mode, 1 => DCE mode */
1541 if (active) {
1542 AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
1543 } else {
1544 AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
1546 /* Channel mask array */
1547 AdditionalInfo[5] = 0; /* no D-Channel */
1548 for (i=1; i <= 30; i++)
1549 AdditionalInfo[5+i] = (bmask & (1 << i)) ? 0xff : 0;
1550 return 0;
1553 static int capidrv_command(isdn_ctrl * c, capidrv_contr * card)
1555 isdn_ctrl cmd;
1556 struct capidrv_bchan *bchan;
1557 struct capidrv_plci *plcip;
1558 u8 AdditionalInfo[1+2+2+31];
1559 int rc, isleasedline = 0;
1561 if (c->command == ISDN_CMD_IOCTL)
1562 return capidrv_ioctl(c, card);
1564 switch (c->command) {
1565 case ISDN_CMD_DIAL:{
1566 u8 calling[ISDN_MSNLEN + 3];
1567 u8 called[ISDN_MSNLEN + 2];
1569 if (debugmode)
1570 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
1571 card->contrnr,
1572 c->arg,
1573 c->parm.setup.phone,
1574 c->parm.setup.si1,
1575 c->parm.setup.si2,
1576 c->parm.setup.eazmsn);
1578 bchan = &card->bchans[c->arg % card->nbchan];
1580 if (bchan->plcip) {
1581 printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
1582 card->contrnr,
1583 c->arg,
1584 c->parm.setup.phone,
1585 c->parm.setup.si1,
1586 c->parm.setup.si2,
1587 c->parm.setup.eazmsn,
1588 bchan->plcip->plci);
1589 return 0;
1591 bchan->si1 = c->parm.setup.si1;
1592 bchan->si2 = c->parm.setup.si2;
1594 strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
1595 strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
1596 rc = FVteln2capi20(bchan->num, AdditionalInfo);
1597 isleasedline = (rc == 0);
1598 if (rc < 0)
1599 printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
1601 if (isleasedline) {
1602 calling[0] = 0;
1603 called[0] = 0;
1604 if (debugmode)
1605 printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
1606 } else {
1607 calling[0] = strlen(bchan->mynum) + 2;
1608 calling[1] = 0;
1609 calling[2] = 0x80;
1610 strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
1611 called[0] = strlen(bchan->num) + 1;
1612 called[1] = 0x80;
1613 strncpy(called + 2, bchan->num, ISDN_MSNLEN);
1616 capi_fill_CONNECT_REQ(&cmdcmsg,
1617 global.ap.applid,
1618 card->msgid++,
1619 card->contrnr, /* adr */
1620 si2cip(bchan->si1, bchan->si2), /* cipvalue */
1621 called, /* CalledPartyNumber */
1622 calling, /* CallingPartyNumber */
1623 0, /* CalledPartySubaddress */
1624 0, /* CallingPartySubaddress */
1625 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1626 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1627 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1628 b1config(bchan->l2, bchan->l3), /* B1configuration */
1629 0, /* B2configuration */
1630 0, /* B3configuration */
1631 0, /* BC */
1632 0, /* LLC */
1633 0, /* HLC */
1634 /* BChannelinformation */
1635 isleasedline ? AdditionalInfo : 0,
1636 0, /* Keypadfacility */
1637 0, /* Useruserdata */
1638 0 /* Facilitydataarray */
1640 if ((plcip = new_plci(card, (c->arg % card->nbchan))) == 0) {
1641 cmd.command = ISDN_STAT_DHUP;
1642 cmd.driver = card->myid;
1643 cmd.arg = (c->arg % card->nbchan);
1644 card->interface.statcallb(&cmd);
1645 return -1;
1647 plcip->msgid = cmdcmsg.Messagenumber;
1648 plcip->leasedline = isleasedline;
1649 plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
1650 send_message(card, &cmdcmsg);
1651 return 0;
1654 case ISDN_CMD_ACCEPTD:
1656 bchan = &card->bchans[c->arg % card->nbchan];
1657 if (debugmode)
1658 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
1659 card->contrnr,
1660 c->arg, bchan->l2, bchan->l3);
1662 capi_fill_CONNECT_RESP(&cmdcmsg,
1663 global.ap.applid,
1664 card->msgid++,
1665 bchan->plcip->plci, /* adr */
1666 0, /* Reject */
1667 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1668 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1669 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1670 b1config(bchan->l2, bchan->l3), /* B1configuration */
1671 0, /* B2configuration */
1672 0, /* B3configuration */
1673 0, /* ConnectedNumber */
1674 0, /* ConnectedSubaddress */
1675 0, /* LLC */
1676 0, /* BChannelinformation */
1677 0, /* Keypadfacility */
1678 0, /* Useruserdata */
1679 0 /* Facilitydataarray */
1681 capi_cmsg2message(&cmdcmsg, cmdcmsg.buf);
1682 plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
1683 send_message(card, &cmdcmsg);
1684 return 0;
1686 case ISDN_CMD_ACCEPTB:
1687 if (debugmode)
1688 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
1689 card->contrnr,
1690 c->arg);
1691 return -ENOSYS;
1693 case ISDN_CMD_HANGUP:
1694 if (debugmode)
1695 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
1696 card->contrnr,
1697 c->arg);
1698 bchan = &card->bchans[c->arg % card->nbchan];
1700 if (bchan->disconnecting) {
1701 if (debugmode)
1702 printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
1703 card->contrnr,
1704 c->arg);
1705 return 0;
1707 if (bchan->nccip) {
1708 bchan->disconnecting = 1;
1709 capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
1710 global.ap.applid,
1711 card->msgid++,
1712 bchan->nccip->ncci,
1713 0 /* NCPI */
1715 ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
1716 send_message(card, &cmdcmsg);
1717 return 0;
1718 } else if (bchan->plcip) {
1719 if (bchan->plcip->state == ST_PLCI_INCOMING) {
1721 * just ignore, we a called from
1722 * isdn_status_callback(),
1723 * which will return 0 or 2, this is handled
1724 * by the CONNECT_IND handler
1726 bchan->disconnecting = 1;
1727 return 0;
1728 } else if (bchan->plcip->plci) {
1729 bchan->disconnecting = 1;
1730 capi_fill_DISCONNECT_REQ(&cmdcmsg,
1731 global.ap.applid,
1732 card->msgid++,
1733 bchan->plcip->plci,
1734 0, /* BChannelinformation */
1735 0, /* Keypadfacility */
1736 0, /* Useruserdata */
1737 0 /* Facilitydataarray */
1739 plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
1740 send_message(card, &cmdcmsg);
1741 return 0;
1742 } else {
1743 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
1744 card->contrnr,
1745 c->arg);
1746 return -EINVAL;
1749 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
1750 card->contrnr,
1751 c->arg);
1752 return -EINVAL;
1753 /* ready */
1755 case ISDN_CMD_SETL2:
1756 if (debugmode)
1757 printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
1758 card->contrnr,
1759 (c->arg & 0xff), (c->arg >> 8));
1760 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1761 bchan->l2 = (c->arg >> 8);
1762 return 0;
1764 case ISDN_CMD_SETL3:
1765 if (debugmode)
1766 printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
1767 card->contrnr,
1768 (c->arg & 0xff), (c->arg >> 8));
1769 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1770 bchan->l3 = (c->arg >> 8);
1771 return 0;
1773 case ISDN_CMD_SETEAZ:
1774 if (debugmode)
1775 printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
1776 card->contrnr,
1777 c->parm.num, c->arg);
1778 bchan = &card->bchans[c->arg % card->nbchan];
1779 strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
1780 return 0;
1782 case ISDN_CMD_CLREAZ:
1783 if (debugmode)
1784 printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
1785 card->contrnr, c->arg);
1786 bchan = &card->bchans[c->arg % card->nbchan];
1787 bchan->msn[0] = 0;
1788 return 0;
1790 default:
1791 printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
1792 card->contrnr, c->command);
1793 return -EINVAL;
1795 return 0;
1798 static int if_command(isdn_ctrl * c)
1800 capidrv_contr *card = findcontrbydriverid(c->driver);
1802 if (card)
1803 return capidrv_command(c, card);
1805 printk(KERN_ERR
1806 "capidrv: if_command %d called with invalid driverId %d!\n",
1807 c->command, c->driver);
1808 return -ENODEV;
1811 static _cmsg sendcmsg;
1813 static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
1815 capidrv_contr *card = findcontrbydriverid(id);
1816 capidrv_bchan *bchan;
1817 capidrv_ncci *nccip;
1818 int len = skb->len;
1819 size_t msglen;
1820 u16 errcode;
1821 u16 datahandle;
1823 if (!card) {
1824 printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
1825 id);
1826 return 0;
1828 if (debugmode > 1)
1829 printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
1830 card->contrnr, len, skb, doack);
1831 bchan = &card->bchans[channel % card->nbchan];
1832 nccip = bchan->nccip;
1833 if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
1834 printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
1835 card->contrnr, card->name, channel);
1836 return 0;
1838 datahandle = nccip->datahandle;
1839 capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
1840 nccip->ncci, /* adr */
1841 (u32) skb->data, /* Data */
1842 skb->len, /* DataLength */
1843 datahandle, /* DataHandle */
1844 0 /* Flags */
1847 if (capidrv_add_ack(nccip, datahandle, doack ? skb->len : -1) < 0)
1848 return 0;
1850 capi_cmsg2message(&sendcmsg, sendcmsg.buf);
1851 msglen = CAPIMSG_LEN(sendcmsg.buf);
1852 if (skb_headroom(skb) < msglen) {
1853 struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
1854 if (!nskb) {
1855 printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
1856 card->contrnr);
1857 (void)capidrv_del_ack(nccip, datahandle);
1858 return 0;
1860 printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
1861 card->contrnr, skb_headroom(skb), msglen);
1862 memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
1863 errcode = capi20_put_message(&global.ap, nskb);
1864 if (errcode == CAPI_NOERROR) {
1865 dev_kfree_skb(skb);
1866 nccip->datahandle++;
1867 return len;
1869 (void)capidrv_del_ack(nccip, datahandle);
1870 dev_kfree_skb(nskb);
1871 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1872 } else {
1873 memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
1874 errcode = capi20_put_message(&global.ap, skb);
1875 if (errcode == CAPI_NOERROR) {
1876 nccip->datahandle++;
1877 return len;
1879 skb_pull(skb, msglen);
1880 (void)capidrv_del_ack(nccip, datahandle);
1881 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1885 static int if_readstat(u8 *buf, int len, int user, int id, int channel)
1887 capidrv_contr *card = findcontrbydriverid(id);
1888 int count;
1889 u8 *p;
1891 if (!card) {
1892 printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
1893 id);
1894 return -ENODEV;
1897 for (p=buf, count=0; count < len; p++, count++) {
1898 if (user)
1899 put_user(*card->q931_read++, p);
1900 else
1901 *p = *card->q931_read++;
1902 if (card->q931_read > card->q931_end)
1903 card->q931_read = card->q931_buf;
1905 return count;
1909 static void enable_dchannel_trace(capidrv_contr *card)
1911 u8 manufacturer[CAPI_MANUFACTURER_LEN];
1912 capi_version version;
1913 u16 contr = card->contrnr;
1914 u16 errcode;
1915 u16 avmversion[3];
1917 errcode = capi20_get_manufacturer(contr, manufacturer);
1918 if (errcode != CAPI_NOERROR) {
1919 printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
1920 card->name, errcode);
1921 return;
1923 if (strstr(manufacturer, "AVM") == 0) {
1924 printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
1925 card->name, manufacturer);
1926 return;
1928 errcode = capi20_get_version(contr, &version);
1929 if (errcode != CAPI_NOERROR) {
1930 printk(KERN_ERR "%s: can't get version (0x%x)\n",
1931 card->name, errcode);
1932 return;
1934 avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
1935 avmversion[1] = (version.majormanuversion << 4) & 0xf0;
1936 avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
1937 avmversion[2] |= version.minormanuversion & 0x0f;
1939 if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
1940 printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
1941 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1942 card->msgid++,
1943 contr,
1944 0x214D5641, /* ManuID */
1945 0, /* Class */
1946 1, /* Function */
1947 (_cstruct)"\004\200\014\000\000");
1948 } else {
1949 printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
1950 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1951 card->msgid++,
1952 contr,
1953 0x214D5641, /* ManuID */
1954 0, /* Class */
1955 1, /* Function */
1956 (_cstruct)"\004\002\003\000\000");
1958 send_message(card, &cmdcmsg);
1962 static void send_listen(capidrv_contr *card)
1964 capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
1965 card->msgid++,
1966 card->contrnr, /* controller */
1967 1 << 6, /* Infomask */
1968 card->cipmask,
1969 card->cipmask2,
1970 0, 0);
1971 send_message(card, &cmdcmsg);
1972 listen_change_state(card, EV_LISTEN_REQ);
1975 static void listentimerfunc(unsigned long x)
1977 capidrv_contr *card = (capidrv_contr *)x;
1978 if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
1979 printk(KERN_ERR "%s: controller dead ??\n", card->name);
1980 send_listen(card);
1981 mod_timer(&card->listentimer, jiffies + 60*HZ);
1985 static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
1987 capidrv_contr *card;
1988 unsigned long flags;
1989 isdn_ctrl cmd;
1990 char id[20];
1991 int i;
1993 MOD_INC_USE_COUNT;
1995 sprintf(id, "capidrv-%d", contr);
1996 if (!(card = (capidrv_contr *) kmalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
1997 printk(KERN_WARNING
1998 "capidrv: (%s) Could not allocate contr-struct.\n", id);
1999 MOD_DEC_USE_COUNT;
2000 return -1;
2002 memset(card, 0, sizeof(capidrv_contr));
2003 init_timer(&card->listentimer);
2004 strcpy(card->name, id);
2005 card->contrnr = contr;
2006 card->nbchan = profp->nbchannel;
2007 card->bchans = (capidrv_bchan *) kmalloc(sizeof(capidrv_bchan) * card->nbchan, GFP_ATOMIC);
2008 if (!card->bchans) {
2009 printk(KERN_WARNING
2010 "capidrv: (%s) Could not allocate bchan-structs.\n", id);
2011 kfree(card);
2012 MOD_DEC_USE_COUNT;
2013 return -1;
2015 card->interface.channels = profp->nbchannel;
2016 card->interface.maxbufsize = 2048;
2017 card->interface.command = if_command;
2018 card->interface.writebuf_skb = if_sendbuf;
2019 card->interface.writecmd = 0;
2020 card->interface.readstat = if_readstat;
2021 card->interface.features = ISDN_FEATURE_L2_HDLC |
2022 ISDN_FEATURE_L2_TRANS |
2023 ISDN_FEATURE_L3_TRANS |
2024 ISDN_FEATURE_P_UNKNOWN |
2025 ISDN_FEATURE_L2_X75I |
2026 ISDN_FEATURE_L2_X75UI |
2027 ISDN_FEATURE_L2_X75BUI;
2028 if (profp->support1 & (1<<2))
2029 card->interface.features |= ISDN_FEATURE_L2_V11096 |
2030 ISDN_FEATURE_L2_V11019 |
2031 ISDN_FEATURE_L2_V11038;
2032 if (profp->support1 & (1<<8))
2033 card->interface.features |= ISDN_FEATURE_L2_MODEM;
2034 card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
2035 strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
2038 card->q931_read = card->q931_buf;
2039 card->q931_write = card->q931_buf;
2040 card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
2042 if (!register_isdn(&card->interface)) {
2043 printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
2044 kfree(card->bchans);
2045 kfree(card);
2046 MOD_DEC_USE_COUNT;
2047 return -1;
2049 card->myid = card->interface.channels;
2051 spin_lock_irqsave(&global_lock, flags);
2052 card->next = global.contr_list;
2053 global.contr_list = card;
2054 global.ncontr++;
2055 spin_unlock_irqrestore(&global_lock, flags);
2057 memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
2058 for (i = 0; i < card->nbchan; i++) {
2059 card->bchans[i].contr = card;
2062 cmd.command = ISDN_STAT_RUN;
2063 cmd.driver = card->myid;
2064 card->interface.statcallb(&cmd);
2066 card->cipmask = 0x1FFF03FF; /* any */
2067 card->cipmask2 = 0;
2069 send_listen(card);
2071 card->listentimer.data = (unsigned long)card;
2072 card->listentimer.function = listentimerfunc;
2073 mod_timer(&card->listentimer, jiffies + 60*HZ);
2075 printk(KERN_INFO "%s: now up (%d B channels)\n",
2076 card->name, card->nbchan);
2078 enable_dchannel_trace(card);
2080 return 0;
2083 static int capidrv_delcontr(u16 contr)
2085 capidrv_contr **pp, *card;
2086 unsigned long flags;
2087 isdn_ctrl cmd;
2089 spin_lock_irqsave(&global_lock, flags);
2090 for (card = global.contr_list; card; card = card->next) {
2091 if (card->contrnr == contr)
2092 break;
2094 if (!card) {
2095 spin_unlock_irqrestore(&global_lock, flags);
2096 printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
2097 return -1;
2099 spin_unlock_irqrestore(&global_lock, flags);
2101 del_timer(&card->listentimer);
2103 if (debugmode)
2104 printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
2105 card->contrnr, card->myid);
2107 cmd.command = ISDN_STAT_STOP;
2108 cmd.driver = card->myid;
2109 card->interface.statcallb(&cmd);
2111 while (card->nbchan) {
2113 cmd.command = ISDN_STAT_DISCH;
2114 cmd.driver = card->myid;
2115 cmd.arg = card->nbchan-1;
2116 cmd.parm.num[0] = 0;
2117 if (debugmode)
2118 printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
2119 card->contrnr, card->myid, cmd.arg);
2120 card->interface.statcallb(&cmd);
2122 if (card->bchans[card->nbchan-1].nccip)
2123 free_ncci(card, card->bchans[card->nbchan-1].nccip);
2124 if (card->bchans[card->nbchan-1].plcip)
2125 free_plci(card, card->bchans[card->nbchan-1].plcip);
2126 if (card->plci_list)
2127 printk(KERN_ERR "capidrv: bug in free_plci()\n");
2128 card->nbchan--;
2130 kfree(card->bchans);
2131 card->bchans = 0;
2133 if (debugmode)
2134 printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
2135 card->contrnr, card->myid);
2137 cmd.command = ISDN_STAT_UNLOAD;
2138 cmd.driver = card->myid;
2139 card->interface.statcallb(&cmd);
2141 if (debugmode)
2142 printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
2143 card->contrnr, card->myid);
2145 spin_lock_irqsave(&global_lock, flags);
2146 for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
2147 if (*pp == card) {
2148 *pp = (*pp)->next;
2149 card->next = 0;
2150 global.ncontr--;
2151 break;
2154 spin_unlock_irqrestore(&global_lock, flags);
2156 printk(KERN_INFO "%s: now down.\n", card->name);
2158 kfree(card);
2160 MOD_DEC_USE_COUNT;
2162 return 0;
2166 static void lower_callback(unsigned int cmd, u32 contr, void *data)
2169 switch (cmd) {
2170 case KCI_CONTRUP:
2171 printk(KERN_INFO "capidrv: controller %hu up\n", contr);
2172 (void) capidrv_addcontr(contr, (capi_profile *) data);
2173 break;
2174 case KCI_CONTRDOWN:
2175 printk(KERN_INFO "capidrv: controller %hu down\n", contr);
2176 (void) capidrv_delcontr(contr);
2177 break;
2182 * /proc/capi/capidrv:
2183 * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
2185 static int proc_capidrv_read_proc(char *page, char **start, off_t off,
2186 int count, int *eof, void *data)
2188 int len = 0;
2190 len += sprintf(page+len, "%lu %lu %lu %lu\n",
2191 global.ap.nrecvctlpkt,
2192 global.ap.nrecvdatapkt,
2193 global.ap.nsentctlpkt,
2194 global.ap.nsentdatapkt);
2195 if (off+count >= len)
2196 *eof = 1;
2197 if (len < off)
2198 return 0;
2199 *start = page + off;
2200 return ((count < len-off) ? count : len-off);
2203 static struct procfsentries {
2204 char *name;
2205 mode_t mode;
2206 int (*read_proc)(char *page, char **start, off_t off,
2207 int count, int *eof, void *data);
2208 struct proc_dir_entry *procent;
2209 } procfsentries[] = {
2210 /* { "capi", S_IFDIR, 0 }, */
2211 { "capi/capidrv", 0 , proc_capidrv_read_proc },
2214 static void __init proc_init(void)
2216 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
2217 int i;
2219 for (i=0; i < nelem; i++) {
2220 struct procfsentries *p = procfsentries + i;
2221 p->procent = create_proc_entry(p->name, p->mode, 0);
2222 if (p->procent) p->procent->read_proc = p->read_proc;
2226 static void __exit proc_exit(void)
2228 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
2229 int i;
2231 for (i=nelem-1; i >= 0; i--) {
2232 struct procfsentries *p = procfsentries + i;
2233 if (p->procent) {
2234 remove_proc_entry(p->name, 0);
2235 p->procent = 0;
2240 static int __init capidrv_init(void)
2242 capi_profile profile;
2243 char rev[32];
2244 char *p;
2245 u32 ncontr, contr;
2246 u16 errcode;
2248 MOD_INC_USE_COUNT;
2250 if ((p = strchr(revision, ':')) != 0 && p[1]) {
2251 strncpy(rev, p + 2, sizeof(rev));
2252 rev[sizeof(rev)-1] = 0;
2253 if ((p = strchr(rev, '$')) != 0 && p > rev)
2254 *(p-1) = 0;
2255 } else
2256 strcpy(rev, "1.0");
2258 global.ap.rparam.level3cnt = -2; /* number of bchannels twice */
2259 global.ap.rparam.datablkcnt = 16;
2260 global.ap.rparam.datablklen = 2048;
2262 global.ap.recv_message = capidrv_recv_message;
2263 errcode = capi20_register(&global.ap);
2264 if (errcode) {
2265 MOD_DEC_USE_COUNT;
2266 return -EIO;
2269 capi20_set_callback(&global.ap, lower_callback);
2271 errcode = capi20_get_profile(0, &profile);
2272 if (errcode != CAPI_NOERROR) {
2273 capi20_release(&global.ap);
2274 MOD_DEC_USE_COUNT;
2275 return -EIO;
2278 ncontr = profile.ncontroller;
2279 for (contr = 1; contr <= ncontr; contr++) {
2280 errcode = capi20_get_profile(contr, &profile);
2281 if (errcode != CAPI_NOERROR)
2282 continue;
2283 (void) capidrv_addcontr(contr, &profile);
2285 proc_init();
2287 printk(KERN_NOTICE "capidrv: Rev %s: loaded\n", rev);
2288 MOD_DEC_USE_COUNT;
2290 return 0;
2293 static void __exit capidrv_exit(void)
2295 char rev[10];
2296 char *p;
2298 if ((p = strchr(revision, ':')) != 0) {
2299 strcpy(rev, p + 1);
2300 p = strchr(rev, '$');
2301 *p = 0;
2302 } else {
2303 strcpy(rev, " ??? ");
2306 capi20_release(&global.ap);
2308 proc_exit();
2310 printk(KERN_NOTICE "capidrv: Rev%s: unloaded\n", rev);
2313 module_init(capidrv_init);
2314 module_exit(capidrv_exit);