1 /* $Id: capidrv.c,v 1.1.2.2 2004/01/12 23:17:24 keil 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/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>
20 #include <linux/signal.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>
35 #include <linux/isdn/capiutil.h>
36 #include <linux/isdn/capicmd.h>
39 static int debugmode = 0;
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);
46 /* -------- type definitions ----------------------------------------- */
49 struct capidrv_contr {
51 struct capidrv_contr *next;
68 struct timer_list listentimer;
71 * ID of capi message sent
79 struct capidrv_bchan {
80 struct capidrv_contr *contr;
85 u8 mynum[ISDN_MSNLEN];
91 struct capidrv_plci *next;
93 u32 ncci; /* ncci for CONNECT_ACTIVE_IND */
94 u16 msgid; /* to identfy CONNECT_CONF */
99 struct capidrv_ncci *next;
100 struct capidrv_plci *plcip;
102 u16 msgid; /* to identfy CONNECT_B3_CONF */
108 struct ncci_datahandle_queue {
109 struct ncci_datahandle_queue *next;
115 struct capidrv_ncci *nccip;
118 struct capidrv_plci *plci_list;
128 struct capidrv_data {
129 struct capi20_appl ap;
131 struct capidrv_contr *contr_list;
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;
140 /* -------- data definitions ----------------------------------------- */
142 static capidrv_data global;
143 static DEFINE_SPINLOCK(global_lock);
145 static void handle_dtrace_data(capidrv_contr *card,
146 int send, int level2, u8 *data, u16 len);
148 /* -------- convert functions ---------------------------------------- */
150 static inline u32 b1prot(int l2, int l3)
153 case ISDN_PROTO_L2_X75I:
154 case ISDN_PROTO_L2_X75UI:
155 case ISDN_PROTO_L2_X75BUI:
157 case ISDN_PROTO_L2_HDLC:
160 case ISDN_PROTO_L2_TRANS:
162 case ISDN_PROTO_L2_V11096:
163 case ISDN_PROTO_L2_V11019:
164 case ISDN_PROTO_L2_V11038:
166 case ISDN_PROTO_L2_FAX:
168 case ISDN_PROTO_L2_MODEM:
173 static inline u32 b2prot(int l2, int l3)
176 case ISDN_PROTO_L2_X75I:
177 case ISDN_PROTO_L2_X75UI:
178 case ISDN_PROTO_L2_X75BUI:
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:
188 case ISDN_PROTO_L2_FAX:
193 static inline u32 b3prot(int l2, int l3)
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:
207 case ISDN_PROTO_L2_FAX:
212 static _cstruct b1config_async_v110(u16 rate)
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 */
225 static _cstruct b1config(int l2, int l3)
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:
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);
244 static inline u16 si2cip(u8 si1, u8 si2)
246 static const u8 cip[17][5] =
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 */
272 return (u16) cip[si1][si2];
275 static inline u8 cip2si1(u16 cipval)
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 */
284 cipval = 0; /* .... */
288 static inline u8 cip2si2(u16 cipval)
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 */
297 cipval = 0; /* .... */
302 /* -------- controller management ------------------------------------- */
304 static inline capidrv_contr *findcontrbydriverid(int driverid)
309 spin_lock_irqsave(&global_lock, flags);
310 for (p = global.contr_list; p; p = p->next)
311 if (p->myid == driverid)
313 spin_unlock_irqrestore(&global_lock, flags);
317 static capidrv_contr *findcontrbynumber(u32 contr)
320 capidrv_contr *p = global.contr_list;
322 spin_lock_irqsave(&global_lock, flags);
323 for (p = global.contr_list; p; p = p->next)
324 if (p->contrnr == contr)
326 spin_unlock_irqrestore(&global_lock, flags);
331 /* -------- plci management ------------------------------------------ */
333 static capidrv_plci *new_plci(capidrv_contr *card, int chan)
337 plcip = kzalloc(sizeof(capidrv_plci), GFP_ATOMIC);
342 plcip->state = ST_PLCI_NONE;
346 plcip->next = card->plci_list;
347 card->plci_list = plcip;
348 card->bchans[chan].plcip = plcip;
353 static capidrv_plci *find_plci_by_plci(capidrv_contr *card, u32 plci)
356 for (p = card->plci_list; p; p = p->next)
362 static capidrv_plci *find_plci_by_msgid(capidrv_contr *card, u16 msgid)
365 for (p = card->plci_list; p; p = p->next)
366 if (p->msgid == msgid)
371 static capidrv_plci *find_plci_by_ncci(capidrv_contr *card, u32 ncci)
374 for (p = card->plci_list; p; p = p->next)
375 if (p->plci == (ncci & 0xffff))
380 static void free_plci(capidrv_contr *card, capidrv_plci *plcip)
384 for (pp = &card->plci_list; *pp; pp = &(*pp)->next) {
387 card->bchans[plcip->chan].plcip = NULL;
388 card->bchans[plcip->chan].disconnecting = 0;
389 card->bchans[plcip->chan].incoming = 0;
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,
406 nccip = kzalloc(sizeof(capidrv_ncci), GFP_ATOMIC);
412 nccip->state = ST_NCCI_NONE;
413 nccip->plcip = plcip;
414 nccip->chan = plcip->chan;
415 nccip->datahandle = 0;
417 nccip->next = plcip->ncci_list;
418 plcip->ncci_list = nccip;
420 card->bchans[plcip->chan].nccip = nccip;
425 static inline capidrv_ncci *find_ncci(capidrv_contr *card, u32 ncci)
430 if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
433 for (p = plcip->ncci_list; p; p = p->next)
439 static inline capidrv_ncci *find_ncci_by_msgid(capidrv_contr *card,
445 if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
448 for (p = plcip->ncci_list; p; p = p->next)
449 if (p->msgid == msgid)
454 static void free_ncci(capidrv_contr *card, struct capidrv_ncci *nccip)
456 struct capidrv_ncci **pp;
458 for (pp = &(nccip->plcip->ncci_list); *pp; pp = &(*pp)->next) {
464 card->bchans[nccip->chan].nccip = NULL;
468 static int capidrv_add_ack(struct capidrv_ncci *nccip,
469 u16 datahandle, int len)
471 struct ncci_datahandle_queue *n, **pp;
473 n = kmalloc(sizeof(struct ncci_datahandle_queue), GFP_ATOMIC);
475 printk(KERN_ERR "capidrv: kmalloc ncci_datahandle failed\n");
479 n->datahandle = datahandle;
481 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next);
486 static int capidrv_del_ack(struct capidrv_ncci *nccip, u16 datahandle)
488 struct ncci_datahandle_queue **pp, *p;
491 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) {
492 if ((*pp)->datahandle == datahandle) {
503 /* -------- convert and send capi message ---------------------------- */
505 static void send_message(capidrv_contr *card, _cmsg *cmsg)
510 if (capi_cmsg2message(cmsg, cmsg->buf)) {
511 printk(KERN_ERR "capidrv::send_message: parser failure\n");
514 len = CAPIMSG_LEN(cmsg->buf);
515 skb = alloc_skb(len, GFP_ATOMIC);
517 printk(KERN_ERR "capidrv::send_message: can't allocate mem\n");
520 skb_put_data(skb, cmsg->buf, len);
521 if (capi20_put_message(&global.ap, skb) != CAPI_NOERROR)
525 /* -------- state machine -------------------------------------------- */
527 struct listenstatechange {
533 static struct listenstatechange listentable[] =
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},
546 static void listen_change_state(capidrv_contr *card, int event)
548 struct listenstatechange *p = listentable;
550 if (card->state == p->actstate && p->event == event) {
552 printk(KERN_DEBUG "capidrv-%d: listen_change_state %d -> %d\n",
553 card->contrnr, card->state, p->nextstate);
554 card->state = p->nextstate;
559 printk(KERN_ERR "capidrv-%d: listen_change_state state=%d event=%d ????\n",
560 card->contrnr, card->state, event);
564 /* ------------------------------------------------------------------ */
566 static void p0(capidrv_contr *card, capidrv_plci *plci)
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);
578 /* ------------------------------------------------------------------ */
580 struct plcistatechange {
584 void (*changefunc)(capidrv_contr *card, capidrv_plci *plci);
587 static struct plcistatechange plcitable[] =
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},
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},
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},
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},
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},
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},
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},
628 {ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
630 {ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0},
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},
635 {ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, NULL},
637 {ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, NULL},
641 static void plci_change_state(capidrv_contr *card, capidrv_plci *plci, int event)
643 struct plcistatechange *p = plcitable;
645 if (plci->state == p->actstate && p->event == event) {
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;
651 p->changefunc(card, plci);
656 printk(KERN_ERR "capidrv-%d: plci_change_state:0x%x state=%d event=%d ????\n",
657 card->contrnr, plci->plci, plci->state, event);
660 /* ------------------------------------------------------------------ */
664 static void n0(capidrv_contr *card, capidrv_ncci *ncci)
668 capi_fill_DISCONNECT_REQ(&cmsg,
672 NULL, /* BChannelinformation */
673 NULL, /* Keypadfacility */
674 NULL, /* Useruserdata */ /* $$$$ */
675 NULL /* Facilitydataarray */
677 plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ);
678 send_message(card, &cmsg);
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);
687 /* ------------------------------------------------------------------ */
689 struct nccistatechange {
693 void (*changefunc)(capidrv_contr *card, capidrv_ncci *ncci);
696 static struct nccistatechange nccitable[] =
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},
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},
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},
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},
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},
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},
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},
726 {ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0},
730 static void ncci_change_state(capidrv_contr *card, capidrv_ncci *ncci, int event)
732 struct nccistatechange *p = nccitable;
734 if (ncci->state == p->actstate && p->event == event) {
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;
742 ncci->oldstate = p->actstate;
743 ncci->state = p->nextstate;
746 p->changefunc(card, ncci);
751 printk(KERN_ERR "capidrv-%d: ncci_change_state:0x%x state=%d event=%d ????\n",
752 card->contrnr, ncci->ncci, ncci->state, event);
755 /* ------------------------------------------------------------------- */
757 static inline int new_bchan(capidrv_contr *card)
760 for (i = 0; i < card->nbchan; i++) {
761 if (card->bchans[i].plcip == NULL) {
762 card->bchans[i].disconnecting = 0;
769 /* ------------------------------------------------------------------- */
770 static char *capi_info2str(u16 reason)
772 #ifndef CONFIG_ISDN_CAPI_CAPIDRV_VERBOSE
777 /*-- informative values (corresponding message was processed) -----*/
779 return "NCPI not supported by current protocol, NCPI ignored";
781 return "Flags not supported by current protocol, flags ignored";
783 return "Alert already sent by another application";
785 /*-- error information concerning CAPI_REGISTER -----*/
787 return "Too many applications";
789 return "Logical block size too small, must be at least 128 Bytes";
791 return "Buffer exceeds 64 kByte";
793 return "Message buffer size too small, must be at least 1024 Bytes";
795 return "Max. number of logical connections not supported";
799 return "The message could not be accepted because of an internal busy condition";
801 return "OS resource error (no memory ?)";
803 return "CAPI not installed";
805 return "Controller does not support external equipment";
807 return "Controller does only support external equipment";
809 /*-- error information concerning message exchange functions -----*/
811 return "Illegal application number";
813 return "Illegal command or subcommand or message length less than 12 bytes";
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";
817 return "Queue is empty";
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";
821 return "Unknown notification parameter";
823 return "The Message could not be accepted because of an internal busy condition";
825 return "OS Resource error (no memory ?)";
827 return "CAPI not installed";
829 return "Controller does not support external equipment";
831 return "Controller does only support external equipment";
833 /*-- error information concerning resource / coding problems -----*/
835 return "Message not supported in current state";
837 return "Illegal Controller / PLCI / NCCI";
839 return "Out of PLCI";
841 return "Out of NCCI";
843 return "Out of LISTEN";
845 return "Out of FAX resources (protocol T.30)";
847 return "Illegal message parameter coding";
849 /*-- error information concerning requested services -----*/
851 return "B1 protocol not supported";
853 return "B2 protocol not supported";
855 return "B3 protocol not supported";
857 return "B1 protocol parameter not supported";
859 return "B2 protocol parameter not supported";
861 return "B3 protocol parameter not supported";
863 return "B protocol combination not supported";
865 return "NCPI not supported";
867 return "CIP Value unknown";
869 return "Flags not supported (reserved bits)";
871 return "Facility not supported";
873 return "Data length not supported by current protocol";
875 return "Reset procedure not supported by current protocol";
877 /*-- informations about the clearing of a physical connection -----*/
879 return "Protocol error layer 1 (broken line or B-channel removed by signalling protocol)";
881 return "Protocol error layer 2";
883 return "Protocol error layer 3";
885 return "Another application got that call";
886 /*-- T.30 specific reasons -----*/
888 return "Connecting not successful (remote station is no FAX G3 machine)";
890 return "Connecting not successful (training error)";
892 return "Disconnected before transfer (remote station does not support transfer mode, e.g. resolution)";
894 return "Disconnected during transfer (remote abort)";
896 return "Disconnected during transfer (remote procedure error, e.g. unsuccessful repetition of T.30 commands)";
898 return "Disconnected during transfer (local tx data underrun)";
900 return "Disconnected during transfer (local rx data overflow)";
902 return "Disconnected during transfer (local abort)";
904 return "Illegal parameter coding (e.g. SFF coding error)";
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";
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";
961 default: return "No additional information";
966 static void handle_controller(_cmsg *cmsg)
968 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
971 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
972 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
973 cmsg->adr.adrController & 0x7f);
976 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
978 case CAPI_LISTEN_CONF: /* Controller */
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);
983 listen_change_state(card, EV_LISTEN_CONF_ERROR);
984 } else if (card->cipmask == 0) {
985 listen_change_state(card, EV_LISTEN_CONF_EMPTY);
987 listen_change_state(card, EV_LISTEN_CONF_OK);
991 case CAPI_MANUFACTURER_IND: /* Controller */
992 if (cmsg->ManuID == 0x214D5641
994 && cmsg->Function == 1) {
995 u8 *data = cmsg->ManuData + 3;
996 u16 len = cmsg->ManuData[0];
1000 len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
1004 layer = ((*(data - 1)) << 8) | *(data - 2);
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);
1013 } else if ((layer & 0xff) < 0x80) {
1014 handle_dtrace_data(card, direction, 0, data, len);
1017 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
1019 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1020 cmsg->adr.adrController, layer);
1024 case CAPI_MANUFACTURER_CONF: /* Controller */
1025 if (cmsg->ManuID == 0x214D5641) {
1027 switch (cmsg->Class) {
1029 case 1: s = "unknown class"; break;
1030 case 2: s = "unknown function"; break;
1031 default: s = "unknown error"; break;
1034 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
1036 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1037 cmsg->adr.adrController,
1042 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1044 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1046 case CAPI_INFO_IND: /* Controller/plci */
1048 case CAPI_INFO_CONF: /* Controller/plci */
1052 printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
1054 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1055 cmsg->adr.adrController);
1060 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
1062 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1063 cmsg->adr.adrController);
1066 static void handle_incoming_call(capidrv_contr *card, _cmsg *cmsg)
1068 capidrv_plci *plcip;
1069 capidrv_bchan *bchan;
1073 if ((chan = new_bchan(card)) == -1) {
1074 printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
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);
1082 bchan->incoming = 1;
1083 plcip->plci = cmsg->adr.adrPLCI;
1084 plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
1086 cmd.command = ISDN_STAT_ICALL;
1087 cmd.driver = card->myid;
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];
1101 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n",
1103 cmd.parm.setup.phone,
1106 cmd.parm.setup.eazmsn);
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",
1111 cmd.parm.setup.si2);
1112 cmd.parm.setup.si2 = 0;
1115 switch (card->interface.statcallb(&cmd)) {
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
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",
1129 cmd.parm.setup.phone,
1132 cmd.parm.setup.eazmsn);
1135 /* At least one device matching this call (RING on ttyI)
1136 * HL-driver may send ALERTING on the D-channel in this
1138 * really means: RING on ttyI or a net interface
1139 * accepted this call already.
1141 * If the call was accepted, state has already changed,
1142 * and CONNECT_RESP already sent.
1144 if (plcip->state == ST_PLCI_INCOMING) {
1145 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
1147 cmd.parm.setup.phone,
1150 cmd.parm.setup.eazmsn);
1151 capi_fill_ALERT_REQ(cmsg,
1154 plcip->plci, /* adr */
1155 NULL,/* BChannelinformation */
1156 NULL,/* Keypadfacility */
1157 NULL,/* Useruserdata */
1158 NULL /* Facilitydataarray */
1160 plcip->msgid = cmsg->Messagenumber;
1161 send_message(card, cmsg);
1163 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
1165 cmd.parm.setup.phone,
1168 cmd.parm.setup.eazmsn);
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);
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);
1191 static void handle_plci(_cmsg *cmsg)
1193 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1194 capidrv_plci *plcip;
1199 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1200 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1201 cmsg->adr.adrController & 0x7f);
1204 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1206 case CAPI_DISCONNECT_IND: /* plci */
1208 printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
1210 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1211 cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
1213 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
1214 capi_cmsg_answer(cmsg);
1215 send_message(card, cmsg);
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);
1225 case CAPI_DISCONNECT_CONF: /* plci */
1227 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1229 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1230 cmsg->Info, capi_info2str(cmsg->Info),
1233 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1236 card->bchans[plcip->chan].disconnecting = 1;
1239 case CAPI_ALERT_CONF: /* plci */
1241 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1243 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1244 cmsg->Info, capi_info2str(cmsg->Info),
1249 case CAPI_CONNECT_IND: /* plci */
1250 handle_incoming_call(card, cmsg);
1253 case CAPI_CONNECT_CONF: /* plci */
1255 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1257 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1258 cmsg->Info, capi_info2str(cmsg->Info),
1261 if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
1264 plcip->plci = cmsg->adr.adrPLCI;
1266 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
1268 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
1272 case CAPI_CONNECT_ACTIVE_IND: /* plci */
1274 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
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);
1282 capidrv_ncci *nccip;
1283 capi_cmsg_answer(cmsg);
1284 send_message(card, cmsg);
1286 nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
1289 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1292 capi_fill_CONNECT_B3_REQ(cmsg,
1295 plcip->plci, /* adr */
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);
1310 case CAPI_INFO_IND: /* Controller/plci */
1312 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
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",
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);
1330 cdb = capi_cmsg2str(cmsg);
1332 printk(KERN_WARNING "capidrv-%d: %s\n",
1333 card->contrnr, cdb->buf);
1336 printk(KERN_WARNING "capidrv-%d: CAPI_INFO_IND InfoNumber %x not handled\n",
1337 card->contrnr, cmsg->InfoNumber);
1341 case CAPI_CONNECT_ACTIVE_CONF: /* plci */
1343 case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */
1345 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1347 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1350 case CAPI_INFO_CONF: /* Controller/plci */
1354 printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
1356 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1361 printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
1363 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1367 printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
1369 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1374 static void handle_ncci(_cmsg *cmsg)
1376 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1377 capidrv_plci *plcip;
1378 capidrv_ncci *nccip;
1383 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1384 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1385 cmsg->adr.adrController & 0x7f);
1388 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1390 case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */
1391 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1394 capi_cmsg_answer(cmsg);
1395 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
1396 send_message(card, cmsg);
1398 cmd.command = ISDN_STAT_BCONN;
1399 cmd.driver = card->myid;
1400 cmd.arg = nccip->chan;
1401 card->interface.statcallb(&cmd);
1403 printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
1404 card->contrnr, nccip->chan, nccip->ncci);
1407 case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */
1410 case CAPI_CONNECT_B3_IND: /* ncci */
1412 plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
1414 nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
1416 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
1417 capi_fill_CONNECT_B3_RESP(cmsg,
1420 nccip->ncci, /* adr */
1424 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
1425 send_message(card, cmsg);
1428 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1430 printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
1432 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1435 capi_fill_CONNECT_B3_RESP(cmsg,
1442 send_message(card, cmsg);
1445 case CAPI_CONNECT_B3_CONF: /* ncci */
1447 if (!(nccip = find_ncci_by_msgid(card,
1449 cmsg->Messagenumber)))
1452 nccip->ncci = cmsg->adr.adrNCCI;
1454 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1456 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1457 cmsg->Info, capi_info2str(cmsg->Info),
1462 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
1464 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
1467 case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */
1468 capi_cmsg_answer(cmsg);
1469 send_message(card, cmsg);
1472 case CAPI_DATA_B3_IND: /* ncci */
1473 /* handled in handle_data() */
1476 case CAPI_DATA_B3_CONF: /* ncci */
1478 printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
1479 cmsg->Info, capi_info2str(cmsg->Info));
1481 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1484 len = capidrv_del_ack(nccip, cmsg->DataHandle);
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);
1494 case CAPI_DISCONNECT_B3_IND: /* ncci */
1495 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
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);
1505 case CAPI_DISCONNECT_B3_CONF: /* ncci */
1506 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1509 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1511 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1512 cmsg->Info, capi_info2str(cmsg->Info),
1514 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
1518 case CAPI_RESET_B3_IND: /* ncci */
1519 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1521 ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
1522 capi_cmsg_answer(cmsg);
1523 send_message(card, cmsg);
1526 case CAPI_RESET_B3_CONF: /* ncci */
1527 goto ignored; /* $$$$ */
1529 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1531 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1535 printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
1537 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1542 printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
1544 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1548 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1550 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1555 static void handle_data(_cmsg *cmsg, struct sk_buff *skb)
1557 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1558 capidrv_ncci *nccip;
1561 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1562 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1563 cmsg->adr.adrController & 0x7f);
1567 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
1568 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1570 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
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);
1581 static _cmsg s_cmsg;
1583 static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
1585 if (capi_message2cmsg(&s_cmsg, skb->data)) {
1586 printk(KERN_ERR "capidrv: applid=%d: received invalid message\n",
1591 if (debugmode > 3) {
1592 _cdebbuf *cdb = capi_cmsg2str(&s_cmsg);
1595 printk(KERN_DEBUG "%s: applid=%d %s\n", __func__,
1596 ap->applid, cdb->buf);
1599 printk(KERN_DEBUG "%s: applid=%d %s not traced\n",
1600 __func__, ap->applid,
1601 capi_cmd2str(s_cmsg.Command, s_cmsg.Subcommand));
1603 if (s_cmsg.Command == CAPI_DATA_B3
1604 && s_cmsg.Subcommand == CAPI_IND) {
1605 handle_data(&s_cmsg, skb);
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);
1613 handle_ncci(&s_cmsg);
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>
1622 /* ------------------------------------------------------------------- */
1624 #define PUTBYTE_TO_STATUS(card, byte) \
1626 *(card)->q931_write++ = (byte); \
1627 if ((card)->q931_write > (card)->q931_end) \
1628 (card)->q931_write = (card)->q931_buf; \
1631 static void handle_dtrace_data(capidrv_contr *card,
1632 int send, int level2, u8 *data, u16 len)
1638 printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
1639 card->contrnr, len);
1644 PUTBYTE_TO_STATUS(card, 'D');
1645 PUTBYTE_TO_STATUS(card, '2');
1646 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1647 PUTBYTE_TO_STATUS(card, ':');
1649 PUTBYTE_TO_STATUS(card, 'D');
1650 PUTBYTE_TO_STATUS(card, '3');
1651 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1652 PUTBYTE_TO_STATUS(card, ':');
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));
1660 PUTBYTE_TO_STATUS(card, '\n');
1662 cmd.command = ISDN_STAT_STAVAIL;
1663 cmd.driver = card->myid;
1664 cmd.arg = len * 3 + 5;
1665 card->interface.statcallb(&cmd);
1668 /* ------------------------------------------------------------------- */
1670 static _cmsg cmdcmsg;
1672 static int capidrv_ioctl(isdn_ctrl *c, capidrv_contr *card)
1676 debugmode = (int)(*((unsigned int *)c->parm.num));
1677 printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
1678 card->contrnr, debugmode);
1681 printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
1682 card->contrnr, c->arg);
1689 * Handle leased lines (CAPI-Bundling)
1692 struct internal_bchannelinfo {
1693 unsigned short channelalloc;
1694 unsigned short operation;
1695 unsigned char cmask[31];
1698 static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
1700 unsigned long bmask = 0;
1705 if (strncmp(teln, "FV:", 3) != 0)
1708 while (*s && *s == ' ') s++;
1710 if (*s == 'p' || *s == 'P') {
1714 if (*s == 'a' || *s == 'A') {
1723 digit1 = simple_strtoul(s, &endp, 10);
1728 if (digit1 <= 0 || digit1 > 30) return -4;
1729 if (*s == 0 || *s == ',' || *s == ' ') {
1730 bmask |= (1 << digit1);
1735 if (*s != '-') return -5;
1738 digit2 = simple_strtoul(s, &endp, 10);
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++)
1749 for (i = digit1; i <= digit2; i++)
1751 digit1 = digit2 = 0;
1757 if (activep) *activep = active;
1758 if (bmaskp) *bmaskp = bmask;
1762 static int FVteln2capi20(char *teln, u8 AdditionalInfo[1 + 2 + 2 + 31])
1764 unsigned long bmask;
1768 rc = decodeFVteln(teln, &bmask, &active);
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 */
1776 AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
1778 AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
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;
1787 static int capidrv_command(isdn_ctrl *c, capidrv_contr *card)
1790 struct capidrv_bchan *bchan;
1791 struct capidrv_plci *plcip;
1792 u8 AdditionalInfo[1 + 2 + 2 + 31];
1793 int rc, isleasedline = 0;
1795 if (c->command == ISDN_CMD_IOCTL)
1796 return capidrv_ioctl(c, card);
1798 switch (c->command) {
1799 case ISDN_CMD_DIAL: {
1800 u8 calling[ISDN_MSNLEN + 3];
1801 u8 called[ISDN_MSNLEN + 2];
1804 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
1807 c->parm.setup.phone,
1810 c->parm.setup.eazmsn);
1812 bchan = &card->bchans[c->arg % card->nbchan];
1815 printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
1818 c->parm.setup.phone,
1821 c->parm.setup.eazmsn,
1822 bchan->plcip->plci);
1825 bchan->si1 = c->parm.setup.si1;
1826 bchan->si2 = c->parm.setup.si2;
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);
1833 printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
1839 printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
1841 calling[0] = strlen(bchan->mynum) + 2;
1844 strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
1845 called[0] = strlen(bchan->num) + 1;
1847 strncpy(called + 2, bchan->num, ISDN_MSNLEN);
1850 capi_fill_CONNECT_REQ(&cmdcmsg,
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 */
1868 /* BChannelinformation */
1869 isleasedline ? AdditionalInfo : NULL,
1870 NULL, /* Keypadfacility */
1871 NULL, /* Useruserdata */
1872 NULL /* Facilitydataarray */
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);
1881 plcip->msgid = cmdcmsg.Messagenumber;
1882 plcip->leasedline = isleasedline;
1883 plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
1884 send_message(card, &cmdcmsg);
1888 case ISDN_CMD_ACCEPTD:
1890 bchan = &card->bchans[c->arg % card->nbchan];
1892 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
1894 c->arg, bchan->l2, bchan->l3);
1896 capi_fill_CONNECT_RESP(&cmdcmsg,
1899 bchan->plcip->plci, /* adr */
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 */
1910 NULL, /* BChannelinformation */
1911 NULL, /* Keypadfacility */
1912 NULL, /* Useruserdata */
1913 NULL /* Facilitydataarray */
1915 if (capi_cmsg2message(&cmdcmsg, cmdcmsg.buf)) {
1916 printk(KERN_ERR "capidrv-%d: capidrv_command: parser failure\n",
1920 plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
1921 send_message(card, &cmdcmsg);
1924 case ISDN_CMD_ACCEPTB:
1926 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
1931 case ISDN_CMD_HANGUP:
1933 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
1936 bchan = &card->bchans[c->arg % card->nbchan];
1938 if (bchan->disconnecting) {
1940 printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
1946 bchan->disconnecting = 1;
1947 capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
1953 ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
1954 send_message(card, &cmdcmsg);
1956 } else if (bchan->plcip) {
1957 if (bchan->plcip->state == ST_PLCI_INCOMING) {
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
1964 bchan->disconnecting = 1;
1966 } else if (bchan->plcip->plci) {
1967 bchan->disconnecting = 1;
1968 capi_fill_DISCONNECT_REQ(&cmdcmsg,
1972 NULL, /* BChannelinformation */
1973 NULL, /* Keypadfacility */
1974 NULL, /* Useruserdata */
1975 NULL /* Facilitydataarray */
1977 plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
1978 send_message(card, &cmdcmsg);
1981 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
1987 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
1993 case ISDN_CMD_SETL2:
1995 printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
1997 (c->arg & 0xff), (c->arg >> 8));
1998 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1999 bchan->l2 = (c->arg >> 8);
2002 case ISDN_CMD_SETL3:
2004 printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
2006 (c->arg & 0xff), (c->arg >> 8));
2007 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
2008 bchan->l3 = (c->arg >> 8);
2011 case ISDN_CMD_SETEAZ:
2013 printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
2015 c->parm.num, c->arg);
2016 bchan = &card->bchans[c->arg % card->nbchan];
2017 strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
2020 case ISDN_CMD_CLREAZ:
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];
2029 printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
2030 card->contrnr, c->command);
2036 static int if_command(isdn_ctrl *c)
2038 capidrv_contr *card = findcontrbydriverid(c->driver);
2041 return capidrv_command(c, card);
2044 "capidrv: if_command %d called with invalid driverId %d!\n",
2045 c->command, c->driver);
2049 static _cmsg sendcmsg;
2051 static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
2053 capidrv_contr *card = findcontrbydriverid(id);
2054 capidrv_bchan *bchan;
2055 capidrv_ncci *nccip;
2063 printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
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);
2077 datahandle = nccip->datahandle;
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.
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.
2092 data = (unsigned long) skb->data;
2095 capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
2096 nccip->ncci, /* adr */
2098 skb->len, /* DataLength */
2099 datahandle, /* DataHandle */
2103 if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
2106 if (capi_cmsg2message(&sendcmsg, sendcmsg.buf)) {
2107 printk(KERN_ERR "capidrv-%d: if_sendbuf: parser failure\n",
2111 msglen = CAPIMSG_LEN(sendcmsg.buf);
2112 if (skb_headroom(skb) < msglen) {
2113 struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
2115 printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
2117 (void)capidrv_del_ack(nccip, datahandle);
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) {
2126 nccip->datahandle++;
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;
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++;
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;
2151 static int if_readstat(u8 __user *buf, int len, int id, int channel)
2153 capidrv_contr *card = findcontrbydriverid(id);
2158 printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
2163 for (p = buf, count = 0; count < len; p++, count++) {
2164 if (put_user(*card->q931_read++, p))
2166 if (card->q931_read > card->q931_end)
2167 card->q931_read = card->q931_buf;
2173 static void enable_dchannel_trace(capidrv_contr *card)
2175 u8 manufacturer[CAPI_MANUFACTURER_LEN];
2176 capi_version version;
2177 u16 contr = card->contrnr;
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);
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);
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);
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;
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,
2208 0x214D5641, /* ManuID */
2211 (_cstruct)"\004\200\014\000\000");
2213 printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
2214 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
2217 0x214D5641, /* ManuID */
2220 (_cstruct)"\004\002\003\000\000");
2222 send_message(card, &cmdcmsg);
2226 static void send_listen(capidrv_contr *card)
2228 capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
2230 card->contrnr, /* controller */
2231 1 << 6, /* Infomask */
2235 listen_change_state(card, EV_LISTEN_REQ);
2236 send_message(card, &cmdcmsg);
2239 static void listentimerfunc(struct timer_list *t)
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);
2245 mod_timer(&card->listentimer, jiffies + 60 * HZ);
2249 static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
2251 capidrv_contr *card;
2252 unsigned long flags;
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);
2262 if (!(card = kzalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
2264 "capidrv: (%s) Could not allocate contr-struct.\n", id);
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),
2274 if (!card->bchans) {
2276 "capidrv: (%s) Could not allocate bchan-structs.\n", id);
2277 module_put(card->owner);
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);
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;
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);
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;
2323 spin_lock_irqsave(&global_lock, flags);
2324 card->next = global.contr_list;
2325 global.contr_list = card;
2327 spin_unlock_irqrestore(&global_lock, flags);
2329 cmd.command = ISDN_STAT_RUN;
2330 cmd.driver = card->myid;
2331 card->interface.statcallb(&cmd);
2333 card->cipmask = 0x1FFF03FF; /* any */
2337 mod_timer(&card->listentimer, jiffies + 60 * HZ);
2339 printk(KERN_INFO "%s: now up (%d B channels)\n",
2340 card->name, card->nbchan);
2342 enable_dchannel_trace(card);
2347 static int capidrv_delcontr(u16 contr)
2349 capidrv_contr **pp, *card;
2350 unsigned long flags;
2353 spin_lock_irqsave(&global_lock, flags);
2354 for (card = global.contr_list; card; card = card->next) {
2355 if (card->contrnr == contr)
2359 spin_unlock_irqrestore(&global_lock, flags);
2360 printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
2364 /* FIXME: maybe a race condition the card should be removed
2365 * here from global list /kkeil
2367 spin_unlock_irqrestore(&global_lock, flags);
2369 del_timer(&card->listentimer);
2372 printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
2373 card->contrnr, card->myid);
2375 cmd.command = ISDN_STAT_STOP;
2376 cmd.driver = card->myid;
2377 card->interface.statcallb(&cmd);
2379 while (card->nbchan) {
2381 cmd.command = ISDN_STAT_DISCH;
2382 cmd.driver = card->myid;
2383 cmd.arg = card->nbchan - 1;
2384 cmd.parm.num[0] = 0;
2386 printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
2387 card->contrnr, card->myid, cmd.arg);
2388 card->interface.statcallb(&cmd);
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");
2398 kfree(card->bchans);
2399 card->bchans = NULL;
2402 printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
2403 card->contrnr, card->myid);
2405 cmd.command = ISDN_STAT_UNLOAD;
2406 cmd.driver = card->myid;
2407 card->interface.statcallb(&cmd);
2410 printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
2411 card->contrnr, card->myid);
2413 spin_lock_irqsave(&global_lock, flags);
2414 for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
2422 spin_unlock_irqrestore(&global_lock, flags);
2424 module_put(card->owner);
2425 printk(KERN_INFO "%s: now down.\n", card->name);
2432 lower_callback(struct notifier_block *nb, unsigned long val, void *v)
2434 capi_profile profile;
2435 u32 contr = (long)v;
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);
2444 printk(KERN_INFO "capidrv: controller %hu down\n", contr);
2445 (void) capidrv_delcontr(contr);
2452 * /proc/capi/capidrv:
2453 * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
2455 static int __maybe_unused capidrv_proc_show(struct seq_file *m, void *v)
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);
2465 static void __init proc_init(void)
2467 proc_create_single("capi/capidrv", 0, NULL, capidrv_proc_show);
2470 static void __exit proc_exit(void)
2472 remove_proc_entry("capi/capidrv", NULL);
2475 static struct notifier_block capictr_nb = {
2476 .notifier_call = lower_callback,
2479 static int __init capidrv_init(void)
2481 capi_profile profile;
2485 global.ap.rparam.level3cnt = -2; /* number of bchannels twice */
2486 global.ap.rparam.datablkcnt = 16;
2487 global.ap.rparam.datablklen = 2048;
2489 global.ap.recv_message = capidrv_recv_message;
2490 errcode = capi20_register(&global.ap);
2495 register_capictr_notifier(&capictr_nb);
2497 errcode = capi20_get_profile(0, &profile);
2498 if (errcode != CAPI_NOERROR) {
2499 unregister_capictr_notifier(&capictr_nb);
2500 capi20_release(&global.ap);
2504 ncontr = profile.ncontroller;
2505 for (contr = 1; contr <= ncontr; contr++) {
2506 errcode = capi20_get_profile(contr, &profile);
2507 if (errcode != CAPI_NOERROR)
2509 (void) capidrv_addcontr(contr, &profile);
2516 static void __exit capidrv_exit(void)
2518 unregister_capictr_notifier(&capictr_nb);
2519 capi20_release(&global.ap);
2524 module_init(capidrv_init);
2525 module_exit(capidrv_exit);