blob: 2a49cea0a223e19e41cdeee3e4f900886521949f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: capidrv.c,v 1.1.2.2 2004/01/12 23:17:24 keil Exp $
2 *
3 * ISDN4Linux Driver, using capi20 interface (kernelcapi)
4 *
5 * Copyright 1997 by Carsten Paeth <calle@calle.de>
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
12#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/fcntl.h>
18#include <linux/fs.h>
19#include <linux/signal.h>
20#include <linux/mm.h>
21#include <linux/timer.h>
22#include <linux/wait.h>
23#include <linux/skbuff.h>
24#include <linux/isdn.h>
25#include <linux/isdnif.h>
26#include <linux/proc_fs.h>
27#include <linux/capi.h>
28#include <linux/kernelcapi.h>
29#include <linux/ctype.h>
30#include <linux/init.h>
31#include <linux/moduleparam.h>
32
33#include <linux/isdn/capiutil.h>
34#include <linux/isdn/capicmd.h>
35#include "capidrv.h"
36
37static char *revision = "$Revision: 1.1.2.2 $";
38static int debugmode = 0;
39
40MODULE_DESCRIPTION("CAPI4Linux: Interface to ISDN4Linux");
41MODULE_AUTHOR("Carsten Paeth");
42MODULE_LICENSE("GPL");
43module_param(debugmode, uint, 0);
44
45/* -------- type definitions ----------------------------------------- */
46
47
48struct capidrv_contr {
49
50 struct capidrv_contr *next;
51 struct module *owner;
52 u32 contrnr;
53 char name[20];
54
55 /*
56 * for isdn4linux
57 */
58 isdn_if interface;
59 int myid;
60
61 /*
62 * LISTEN state
63 */
64 int state;
65 u32 cipmask;
66 u32 cipmask2;
67 struct timer_list listentimer;
68
69 /*
70 * ID of capi message sent
71 */
72 u16 msgid;
73
74 /*
75 * B-Channels
76 */
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;
116
117 struct capidrv_plci *plci_list;
118
119 /* for q931 data */
120 u8 q931_buf[4096];
121 u8 *q931_read;
122 u8 *q931_write;
123 u8 *q931_end;
124};
125
126
127struct capidrv_data {
128 struct capi20_appl ap;
129 int ncontr;
130 struct capidrv_contr *contr_list;
131};
132
133typedef struct capidrv_plci capidrv_plci;
134typedef struct capidrv_ncci capidrv_ncci;
135typedef struct capidrv_contr capidrv_contr;
136typedef struct capidrv_data capidrv_data;
137typedef struct capidrv_bchan capidrv_bchan;
138
139/* -------- data definitions ----------------------------------------- */
140
141static capidrv_data global;
142static DEFINE_SPINLOCK(global_lock);
143
144static void handle_dtrace_data(capidrv_contr *card,
145 int send, int level2, u8 *data, u16 len);
146
147/* -------- convert functions ---------------------------------------- */
148
149static inline u32 b1prot(int l2, int l3)
150{
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;
169 }
170}
171
172static inline u32 b2prot(int l2, int l3)
173{
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;
189 }
190}
191
192static inline u32 b3prot(int l2, int l3)
193{
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;
208 }
209}
210
211static _cstruct b1config_async_v110(u16 rate)
212{
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;
222}
223
224static _cstruct b1config(int l2, int l3)
225{
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 NULL;
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);
240 }
241}
242
243static inline u16 si2cip(u8 si1, u8 si2)
244{
245 static const u8 cip[17][5] =
246 {
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 */
265 };
266 if (si1 > 16)
267 si1 = 0;
268 if (si2 > 4)
269 si2 = 0;
270
271 return (u16) cip[si1][si2];
272}
273
274static inline u8 cip2si1(u16 cipval)
275{
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 */
281
282 if (cipval > 31)
283 cipval = 0; /* .... */
284 return si[cipval];
285}
286
287static inline u8 cip2si2(u16 cipval)
288{
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 */
294
295 if (cipval > 31)
296 cipval = 0; /* .... */
297 return si[cipval];
298}
299
300
301/* -------- controller management ------------------------------------- */
302
303static inline capidrv_contr *findcontrbydriverid(int driverid)
304{
305 unsigned long flags;
306 capidrv_contr *p;
307
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;
314}
315
316static capidrv_contr *findcontrbynumber(u32 contr)
317{
318 unsigned long flags;
319 capidrv_contr *p = global.contr_list;
320
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;
327}
328
329
330/* -------- plci management ------------------------------------------ */
331
332static capidrv_plci *new_plci(capidrv_contr * card, int chan)
333{
334 capidrv_plci *plcip;
335
Burman Yan41f96932006-12-08 02:39:35 -0800336 plcip = kzalloc(sizeof(capidrv_plci), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (plcip == 0)
339 return NULL;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 plcip->state = ST_PLCI_NONE;
342 plcip->plci = 0;
343 plcip->msgid = 0;
344 plcip->chan = chan;
345 plcip->next = card->plci_list;
346 card->plci_list = plcip;
347 card->bchans[chan].plcip = plcip;
348
349 return plcip;
350}
351
352static capidrv_plci *find_plci_by_plci(capidrv_contr * card, u32 plci)
353{
354 capidrv_plci *p;
355 for (p = card->plci_list; p; p = p->next)
356 if (p->plci == plci)
357 return p;
358 return NULL;
359}
360
361static capidrv_plci *find_plci_by_msgid(capidrv_contr * card, u16 msgid)
362{
363 capidrv_plci *p;
364 for (p = card->plci_list; p; p = p->next)
365 if (p->msgid == msgid)
366 return p;
367 return NULL;
368}
369
370static capidrv_plci *find_plci_by_ncci(capidrv_contr * card, u32 ncci)
371{
372 capidrv_plci *p;
373 for (p = card->plci_list; p; p = p->next)
374 if (p->plci == (ncci & 0xffff))
375 return p;
376 return NULL;
377}
378
379static void free_plci(capidrv_contr * card, capidrv_plci * plcip)
380{
381 capidrv_plci **pp;
382
383 for (pp = &card->plci_list; *pp; pp = &(*pp)->next) {
384 if (*pp == plcip) {
385 *pp = (*pp)->next;
386 card->bchans[plcip->chan].plcip = NULL;
387 card->bchans[plcip->chan].disconnecting = 0;
388 card->bchans[plcip->chan].incoming = 0;
389 kfree(plcip);
390 return;
391 }
392 }
393 printk(KERN_ERR "capidrv-%d: free_plci %p (0x%x) not found, Huh?\n",
394 card->contrnr, plcip, plcip->plci);
395}
396
397/* -------- ncci management ------------------------------------------ */
398
399static inline capidrv_ncci *new_ncci(capidrv_contr * card,
400 capidrv_plci * plcip,
401 u32 ncci)
402{
403 capidrv_ncci *nccip;
404
Burman Yan41f96932006-12-08 02:39:35 -0800405 nccip = kzalloc(sizeof(capidrv_ncci), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (nccip == 0)
408 return NULL;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 nccip->ncci = ncci;
411 nccip->state = ST_NCCI_NONE;
412 nccip->plcip = plcip;
413 nccip->chan = plcip->chan;
414 nccip->datahandle = 0;
415
416 nccip->next = plcip->ncci_list;
417 plcip->ncci_list = nccip;
418
419 card->bchans[plcip->chan].nccip = nccip;
420
421 return nccip;
422}
423
424static inline capidrv_ncci *find_ncci(capidrv_contr * card, u32 ncci)
425{
426 capidrv_plci *plcip;
427 capidrv_ncci *p;
428
429 if ((plcip = find_plci_by_ncci(card, ncci)) == 0)
430 return NULL;
431
432 for (p = plcip->ncci_list; p; p = p->next)
433 if (p->ncci == ncci)
434 return p;
435 return NULL;
436}
437
438static inline capidrv_ncci *find_ncci_by_msgid(capidrv_contr * card,
439 u32 ncci, u16 msgid)
440{
441 capidrv_plci *plcip;
442 capidrv_ncci *p;
443
444 if ((plcip = find_plci_by_ncci(card, ncci)) == 0)
445 return NULL;
446
447 for (p = plcip->ncci_list; p; p = p->next)
448 if (p->msgid == msgid)
449 return p;
450 return NULL;
451}
452
453static void free_ncci(capidrv_contr * card, struct capidrv_ncci *nccip)
454{
455 struct capidrv_ncci **pp;
456
457 for (pp = &(nccip->plcip->ncci_list); *pp; pp = &(*pp)->next) {
458 if (*pp == nccip) {
459 *pp = (*pp)->next;
460 break;
461 }
462 }
463 card->bchans[nccip->chan].nccip = NULL;
464 kfree(nccip);
465}
466
467static int capidrv_add_ack(struct capidrv_ncci *nccip,
468 u16 datahandle, int len)
469{
470 struct ncci_datahandle_queue *n, **pp;
471
472 n = (struct ncci_datahandle_queue *)
473 kmalloc(sizeof(struct ncci_datahandle_queue), GFP_ATOMIC);
474 if (!n) {
475 printk(KERN_ERR "capidrv: kmalloc ncci_datahandle failed\n");
476 return -1;
477 }
478 n->next = NULL;
479 n->datahandle = datahandle;
480 n->len = len;
481 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) ;
482 *pp = n;
483 return 0;
484}
485
486static int capidrv_del_ack(struct capidrv_ncci *nccip, u16 datahandle)
487{
488 struct ncci_datahandle_queue **pp, *p;
489 int len;
490
491 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) {
492 if ((*pp)->datahandle == datahandle) {
493 p = *pp;
494 len = p->len;
495 *pp = (*pp)->next;
496 kfree(p);
497 return len;
498 }
499 }
500 return -1;
501}
502
503/* -------- convert and send capi message ---------------------------- */
504
505static void send_message(capidrv_contr * card, _cmsg * cmsg)
506{
507 struct sk_buff *skb;
508 size_t len;
509 capi_cmsg2message(cmsg, cmsg->buf);
510 len = CAPIMSG_LEN(cmsg->buf);
511 skb = alloc_skb(len, GFP_ATOMIC);
512 memcpy(skb_put(skb, len), cmsg->buf, len);
513 if (capi20_put_message(&global.ap, skb) != CAPI_NOERROR)
514 kfree_skb(skb);
515}
516
517/* -------- state machine -------------------------------------------- */
518
519struct listenstatechange {
520 int actstate;
521 int nextstate;
522 int event;
523};
524
525static struct listenstatechange listentable[] =
526{
527 {ST_LISTEN_NONE, ST_LISTEN_WAIT_CONF, EV_LISTEN_REQ},
528 {ST_LISTEN_ACTIVE, ST_LISTEN_ACTIVE_WAIT_CONF, EV_LISTEN_REQ},
529 {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_ERROR},
530 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_ERROR},
531 {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
532 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
533 {ST_LISTEN_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
534 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
535 {},
536};
537
538static void listen_change_state(capidrv_contr * card, int event)
539{
540 struct listenstatechange *p = listentable;
541 while (p->event) {
542 if (card->state == p->actstate && p->event == event) {
543 if (debugmode)
544 printk(KERN_DEBUG "capidrv-%d: listen_change_state %d -> %d\n",
545 card->contrnr, card->state, p->nextstate);
546 card->state = p->nextstate;
547 return;
548 }
549 p++;
550 }
551 printk(KERN_ERR "capidrv-%d: listen_change_state state=%d event=%d ????\n",
552 card->contrnr, card->state, event);
553
554}
555
556/* ------------------------------------------------------------------ */
557
558static void p0(capidrv_contr * card, capidrv_plci * plci)
559{
560 isdn_ctrl cmd;
561
562 card->bchans[plci->chan].contr = NULL;
563 cmd.command = ISDN_STAT_DHUP;
564 cmd.driver = card->myid;
565 cmd.arg = plci->chan;
566 card->interface.statcallb(&cmd);
567 free_plci(card, plci);
568}
569
570/* ------------------------------------------------------------------ */
571
572struct plcistatechange {
573 int actstate;
574 int nextstate;
575 int event;
576 void (*changefunc) (capidrv_contr * card, capidrv_plci * plci);
577};
578
579static struct plcistatechange plcitable[] =
580{
581 /* P-0 */
582 {ST_PLCI_NONE, ST_PLCI_OUTGOING, EV_PLCI_CONNECT_REQ, NULL},
583 {ST_PLCI_NONE, ST_PLCI_ALLOCATED, EV_PLCI_FACILITY_IND_UP, NULL},
584 {ST_PLCI_NONE, ST_PLCI_INCOMING, EV_PLCI_CONNECT_IND, NULL},
585 {ST_PLCI_NONE, ST_PLCI_RESUMEING, EV_PLCI_RESUME_REQ, NULL},
586 /* P-0.1 */
587 {ST_PLCI_OUTGOING, ST_PLCI_NONE, EV_PLCI_CONNECT_CONF_ERROR, p0},
588 {ST_PLCI_OUTGOING, ST_PLCI_ALLOCATED, EV_PLCI_CONNECT_CONF_OK, NULL},
589 /* P-1 */
590 {ST_PLCI_ALLOCATED, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
591 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
592 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
593 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
594 /* P-ACT */
595 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
596 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
597 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
598 {ST_PLCI_ACTIVE, ST_PLCI_HELD, EV_PLCI_HOLD_IND, NULL},
599 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_SUSPEND_IND, NULL},
600 /* P-2 */
601 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
602 {ST_PLCI_INCOMING, ST_PLCI_FACILITY_IND, EV_PLCI_FACILITY_IND_UP, NULL},
603 {ST_PLCI_INCOMING, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_RESP, NULL},
604 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
605 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
606 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
607 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CD_IND, NULL},
608 /* P-3 */
609 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
610 {ST_PLCI_FACILITY_IND, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
611 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
612 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
613 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
614 /* P-4 */
615 {ST_PLCI_ACCEPTING, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
616 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
617 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
618 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
619 /* P-5 */
620 {ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
621 /* P-6 */
622 {ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0},
623 /* P-0.Res */
624 {ST_PLCI_RESUMEING, ST_PLCI_NONE, EV_PLCI_RESUME_CONF_ERROR, p0},
625 {ST_PLCI_RESUMEING, ST_PLCI_RESUME, EV_PLCI_RESUME_CONF_OK, NULL},
626 /* P-RES */
627 {ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, NULL},
628 /* P-HELD */
629 {ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, NULL},
630 {},
631};
632
633static void plci_change_state(capidrv_contr * card, capidrv_plci * plci, int event)
634{
635 struct plcistatechange *p = plcitable;
636 while (p->event) {
637 if (plci->state == p->actstate && p->event == event) {
638 if (debugmode)
639 printk(KERN_DEBUG "capidrv-%d: plci_change_state:0x%x %d -> %d\n",
640 card->contrnr, plci->plci, plci->state, p->nextstate);
641 plci->state = p->nextstate;
642 if (p->changefunc)
643 p->changefunc(card, plci);
644 return;
645 }
646 p++;
647 }
648 printk(KERN_ERR "capidrv-%d: plci_change_state:0x%x state=%d event=%d ????\n",
649 card->contrnr, plci->plci, plci->state, event);
650}
651
652/* ------------------------------------------------------------------ */
653
654static _cmsg cmsg;
655
656static void n0(capidrv_contr * card, capidrv_ncci * ncci)
657{
658 isdn_ctrl cmd;
659
660 capi_fill_DISCONNECT_REQ(&cmsg,
661 global.ap.applid,
662 card->msgid++,
663 ncci->plcip->plci,
664 NULL, /* BChannelinformation */
665 NULL, /* Keypadfacility */
666 NULL, /* Useruserdata */ /* $$$$ */
667 NULL /* Facilitydataarray */
668 );
669 send_message(card, &cmsg);
670 plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ);
671
672 cmd.command = ISDN_STAT_BHUP;
673 cmd.driver = card->myid;
674 cmd.arg = ncci->chan;
675 card->interface.statcallb(&cmd);
676 free_ncci(card, ncci);
677}
678
679/* ------------------------------------------------------------------ */
680
681struct nccistatechange {
682 int actstate;
683 int nextstate;
684 int event;
685 void (*changefunc) (capidrv_contr * card, capidrv_ncci * ncci);
686};
687
688static struct nccistatechange nccitable[] =
689{
690 /* N-0 */
691 {ST_NCCI_NONE, ST_NCCI_OUTGOING, EV_NCCI_CONNECT_B3_REQ, NULL},
692 {ST_NCCI_NONE, ST_NCCI_INCOMING, EV_NCCI_CONNECT_B3_IND, NULL},
693 /* N-0.1 */
694 {ST_NCCI_OUTGOING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_CONF_OK, NULL},
695 {ST_NCCI_OUTGOING, ST_NCCI_NONE, EV_NCCI_CONNECT_B3_CONF_ERROR, n0},
696 /* N-1 */
697 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_CONNECT_B3_REJECT, NULL},
698 {ST_NCCI_INCOMING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_RESP, NULL},
699 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
700 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
701 /* N-2 */
702 {ST_NCCI_ALLOCATED, ST_NCCI_ACTIVE, EV_NCCI_CONNECT_B3_ACTIVE_IND, NULL},
703 {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
704 {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
705 /* N-ACT */
706 {ST_NCCI_ACTIVE, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
707 {ST_NCCI_ACTIVE, ST_NCCI_RESETING, EV_NCCI_RESET_B3_REQ, NULL},
708 {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
709 {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
710 /* N-3 */
711 {ST_NCCI_RESETING, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
712 {ST_NCCI_RESETING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
713 {ST_NCCI_RESETING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
714 /* N-4 */
715 {ST_NCCI_DISCONNECTING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
716 {ST_NCCI_DISCONNECTING, ST_NCCI_PREVIOUS, EV_NCCI_DISCONNECT_B3_CONF_ERROR,NULL},
717 /* N-5 */
718 {ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0},
719 {},
720};
721
722static void ncci_change_state(capidrv_contr * card, capidrv_ncci * ncci, int event)
723{
724 struct nccistatechange *p = nccitable;
725 while (p->event) {
726 if (ncci->state == p->actstate && p->event == event) {
727 if (debugmode)
728 printk(KERN_DEBUG "capidrv-%d: ncci_change_state:0x%x %d -> %d\n",
729 card->contrnr, ncci->ncci, ncci->state, p->nextstate);
730 if (p->nextstate == ST_NCCI_PREVIOUS) {
731 ncci->state = ncci->oldstate;
732 ncci->oldstate = p->actstate;
733 } else {
734 ncci->oldstate = p->actstate;
735 ncci->state = p->nextstate;
736 }
737 if (p->changefunc)
738 p->changefunc(card, ncci);
739 return;
740 }
741 p++;
742 }
743 printk(KERN_ERR "capidrv-%d: ncci_change_state:0x%x state=%d event=%d ????\n",
744 card->contrnr, ncci->ncci, ncci->state, event);
745}
746
747/* ------------------------------------------------------------------- */
748
749static inline int new_bchan(capidrv_contr * card)
750{
751 int i;
752 for (i = 0; i < card->nbchan; i++) {
753 if (card->bchans[i].plcip == 0) {
754 card->bchans[i].disconnecting = 0;
755 return i;
756 }
757 }
758 return -1;
759}
760
761/* ------------------------------------------------------------------- */
762
763static void handle_controller(_cmsg * cmsg)
764{
765 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
766
767 if (!card) {
768 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
769 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
770 cmsg->adr.adrController & 0x7f);
771 return;
772 }
773 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
774
775 case CAPI_LISTEN_CONF: /* Controller */
776 if (debugmode)
777 printk(KERN_DEBUG "capidrv-%d: listenconf Info=0x%4x (%s) cipmask=0x%x\n",
778 card->contrnr, cmsg->Info, capi_info2str(cmsg->Info), card->cipmask);
779 if (cmsg->Info) {
780 listen_change_state(card, EV_LISTEN_CONF_ERROR);
781 } else if (card->cipmask == 0) {
782 listen_change_state(card, EV_LISTEN_CONF_EMPTY);
783 } else {
784 listen_change_state(card, EV_LISTEN_CONF_OK);
785 }
786 break;
787
788 case CAPI_MANUFACTURER_IND: /* Controller */
789 if ( cmsg->ManuID == 0x214D5641
790 && cmsg->Class == 0
791 && cmsg->Function == 1) {
792 u8 *data = cmsg->ManuData+3;
793 u16 len = cmsg->ManuData[0];
794 u16 layer;
795 int direction;
796 if (len == 255) {
797 len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
798 data += 2;
799 }
800 len -= 2;
801 layer = ((*(data-1)) << 8) | *(data-2);
802 if (layer & 0x300)
803 direction = (layer & 0x200) ? 0 : 1;
804 else direction = (layer & 0x800) ? 0 : 1;
805 if (layer & 0x0C00) {
806 if ((layer & 0xff) == 0x80) {
807 handle_dtrace_data(card, direction, 1, data, len);
808 break;
809 }
810 } else if ((layer & 0xff) < 0x80) {
811 handle_dtrace_data(card, direction, 0, data, len);
812 break;
813 }
814 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
815 card->contrnr,
816 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
817 cmsg->adr.adrController, layer);
818 break;
819 }
820 goto ignored;
821 case CAPI_MANUFACTURER_CONF: /* Controller */
822 if (cmsg->ManuID == 0x214D5641) {
823 char *s = NULL;
824 switch (cmsg->Class) {
825 case 0: break;
826 case 1: s = "unknown class"; break;
827 case 2: s = "unknown function"; break;
828 default: s = "unkown error"; break;
829 }
830 if (s)
831 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
832 card->contrnr,
833 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
834 cmsg->adr.adrController,
835 cmsg->Function, s);
836 break;
837 }
838 goto ignored;
839 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
840 goto ignored;
841 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
842 goto ignored;
843 case CAPI_INFO_IND: /* Controller/plci */
844 goto ignored;
845 case CAPI_INFO_CONF: /* Controller/plci */
846 goto ignored;
847
848 default:
849 printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
850 card->contrnr,
851 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
852 cmsg->adr.adrController);
853 }
854 return;
855
856 ignored:
857 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
858 card->contrnr,
859 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
860 cmsg->adr.adrController);
861}
862
863static void handle_incoming_call(capidrv_contr * card, _cmsg * cmsg)
864{
865 capidrv_plci *plcip;
866 capidrv_bchan *bchan;
867 isdn_ctrl cmd;
868 int chan;
869
870 if ((chan = new_bchan(card)) == -1) {
871 printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
872 return;
873 }
874 bchan = &card->bchans[chan];
875 if ((plcip = new_plci(card, chan)) == 0) {
876 printk(KERN_ERR "capidrv-%d: incoming call: no memory, sorry.\n", card->contrnr);
877 return;
878 }
879 bchan->incoming = 1;
880 plcip->plci = cmsg->adr.adrPLCI;
881 plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
882
883 cmd.command = ISDN_STAT_ICALL;
884 cmd.driver = card->myid;
885 cmd.arg = chan;
886 memset(&cmd.parm.setup, 0, sizeof(cmd.parm.setup));
887 strncpy(cmd.parm.setup.phone,
888 cmsg->CallingPartyNumber + 3,
889 cmsg->CallingPartyNumber[0] - 2);
890 strncpy(cmd.parm.setup.eazmsn,
891 cmsg->CalledPartyNumber + 2,
892 cmsg->CalledPartyNumber[0] - 1);
893 cmd.parm.setup.si1 = cip2si1(cmsg->CIPValue);
894 cmd.parm.setup.si2 = cip2si2(cmsg->CIPValue);
895 cmd.parm.setup.plan = cmsg->CallingPartyNumber[1];
896 cmd.parm.setup.screen = cmsg->CallingPartyNumber[2];
897
898 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n",
899 card->contrnr,
900 cmd.parm.setup.phone,
901 cmd.parm.setup.si1,
902 cmd.parm.setup.si2,
903 cmd.parm.setup.eazmsn);
904
905 if (cmd.parm.setup.si1 == 1 && cmd.parm.setup.si2 != 0) {
906 printk(KERN_INFO "capidrv-%d: patching si2=%d to 0 for VBOX\n",
907 card->contrnr,
908 cmd.parm.setup.si2);
909 cmd.parm.setup.si2 = 0;
910 }
911
912 switch (card->interface.statcallb(&cmd)) {
913 case 0:
914 case 3:
915 /* No device matching this call.
916 * and isdn_common.c has send a HANGUP command
917 * which is ignored in state ST_PLCI_INCOMING,
918 * so we send RESP to ignore the call
919 */
920 capi_cmsg_answer(cmsg);
921 cmsg->Reject = 1; /* ignore */
922 send_message(card, cmsg);
923 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
924 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s ignored\n",
925 card->contrnr,
926 cmd.parm.setup.phone,
927 cmd.parm.setup.si1,
928 cmd.parm.setup.si2,
929 cmd.parm.setup.eazmsn);
930 break;
931 case 1:
932 /* At least one device matching this call (RING on ttyI)
933 * HL-driver may send ALERTING on the D-channel in this
934 * case.
935 * really means: RING on ttyI or a net interface
936 * accepted this call already.
937 *
938 * If the call was accepted, state has already changed,
939 * and CONNECT_RESP already sent.
940 */
941 if (plcip->state == ST_PLCI_INCOMING) {
942 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
943 card->contrnr,
944 cmd.parm.setup.phone,
945 cmd.parm.setup.si1,
946 cmd.parm.setup.si2,
947 cmd.parm.setup.eazmsn);
948 capi_fill_ALERT_REQ(cmsg,
949 global.ap.applid,
950 card->msgid++,
951 plcip->plci, /* adr */
952 NULL,/* BChannelinformation */
953 NULL,/* Keypadfacility */
954 NULL,/* Useruserdata */
955 NULL /* Facilitydataarray */
956 );
957 plcip->msgid = cmsg->Messagenumber;
958 send_message(card, cmsg);
959 } else {
960 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
961 card->contrnr,
962 cmd.parm.setup.phone,
963 cmd.parm.setup.si1,
964 cmd.parm.setup.si2,
965 cmd.parm.setup.eazmsn);
966 }
967 break;
968
969 case 2: /* Call will be rejected. */
970 capi_cmsg_answer(cmsg);
971 cmsg->Reject = 2; /* reject call, normal call clearing */
972 send_message(card, cmsg);
973 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
974 break;
975
976 default:
977 /* An error happened. (Invalid parameters for example.) */
978 capi_cmsg_answer(cmsg);
979 cmsg->Reject = 8; /* reject call,
980 destination out of order */
981 send_message(card, cmsg);
982 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
983 break;
984 }
985 return;
986}
987
988static void handle_plci(_cmsg * cmsg)
989{
990 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
991 capidrv_plci *plcip;
992 isdn_ctrl cmd;
993
994 if (!card) {
995 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
996 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
997 cmsg->adr.adrController & 0x7f);
998 return;
999 }
1000 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1001
1002 case CAPI_DISCONNECT_IND: /* plci */
1003 if (cmsg->Reason) {
1004 printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
1005 card->contrnr,
1006 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1007 cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
1008 }
1009 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
1010 capi_cmsg_answer(cmsg);
1011 send_message(card, cmsg);
1012 goto notfound;
1013 }
1014 card->bchans[plcip->chan].disconnecting = 1;
1015 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
1016 capi_cmsg_answer(cmsg);
1017 send_message(card, cmsg);
1018 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
1019 break;
1020
1021 case CAPI_DISCONNECT_CONF: /* plci */
1022 if (cmsg->Info) {
1023 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1024 card->contrnr,
1025 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1026 cmsg->Info, capi_info2str(cmsg->Info),
1027 cmsg->adr.adrPLCI);
1028 }
1029 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1030 goto notfound;
1031
1032 card->bchans[plcip->chan].disconnecting = 1;
1033 break;
1034
1035 case CAPI_ALERT_CONF: /* plci */
1036 if (cmsg->Info) {
1037 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1038 card->contrnr,
1039 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1040 cmsg->Info, capi_info2str(cmsg->Info),
1041 cmsg->adr.adrPLCI);
1042 }
1043 break;
1044
1045 case CAPI_CONNECT_IND: /* plci */
1046 handle_incoming_call(card, cmsg);
1047 break;
1048
1049 case CAPI_CONNECT_CONF: /* plci */
1050 if (cmsg->Info) {
1051 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1052 card->contrnr,
1053 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1054 cmsg->Info, capi_info2str(cmsg->Info),
1055 cmsg->adr.adrPLCI);
1056 }
1057 if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
1058 goto notfound;
1059
1060 plcip->plci = cmsg->adr.adrPLCI;
1061 if (cmsg->Info) {
1062 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
1063 } else {
1064 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
1065 }
1066 break;
1067
1068 case CAPI_CONNECT_ACTIVE_IND: /* plci */
1069
1070 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1071 goto notfound;
1072
1073 if (card->bchans[plcip->chan].incoming) {
1074 capi_cmsg_answer(cmsg);
1075 send_message(card, cmsg);
1076 plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1077 } else {
1078 capidrv_ncci *nccip;
1079 capi_cmsg_answer(cmsg);
1080 send_message(card, cmsg);
1081
1082 nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
1083
1084 if (!nccip) {
1085 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1086 break; /* $$$$ */
1087 }
1088 capi_fill_CONNECT_B3_REQ(cmsg,
1089 global.ap.applid,
1090 card->msgid++,
1091 plcip->plci, /* adr */
1092 NULL /* NCPI */
1093 );
1094 nccip->msgid = cmsg->Messagenumber;
1095 send_message(card, cmsg);
1096 cmd.command = ISDN_STAT_DCONN;
1097 cmd.driver = card->myid;
1098 cmd.arg = plcip->chan;
1099 card->interface.statcallb(&cmd);
1100 plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1101 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
1102 }
1103 break;
1104
1105 case CAPI_INFO_IND: /* Controller/plci */
1106
1107 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1108 goto notfound;
1109
1110 if (cmsg->InfoNumber == 0x4000) {
1111 if (cmsg->InfoElement[0] == 4) {
1112 cmd.command = ISDN_STAT_CINF;
1113 cmd.driver = card->myid;
1114 cmd.arg = plcip->chan;
1115 sprintf(cmd.parm.num, "%lu",
1116 (unsigned long)
1117 ((u32) cmsg->InfoElement[1]
1118 | ((u32) (cmsg->InfoElement[2]) << 8)
1119 | ((u32) (cmsg->InfoElement[3]) << 16)
1120 | ((u32) (cmsg->InfoElement[4]) << 24)));
1121 card->interface.statcallb(&cmd);
1122 break;
1123 }
1124 }
1125 printk(KERN_ERR "capidrv-%d: %s\n",
1126 card->contrnr, capi_cmsg2str(cmsg));
1127 break;
1128
1129 case CAPI_CONNECT_ACTIVE_CONF: /* plci */
1130 goto ignored;
1131 case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */
1132 goto ignored;
1133 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1134 goto ignored;
1135 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1136 goto ignored;
1137
1138 case CAPI_INFO_CONF: /* Controller/plci */
1139 goto ignored;
1140
1141 default:
1142 printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
1143 card->contrnr,
1144 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1145 cmsg->adr.adrPLCI);
1146 }
1147 return;
1148 ignored:
1149 printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
1150 card->contrnr,
1151 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1152 cmsg->adr.adrPLCI);
1153 return;
1154 notfound:
1155 printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
1156 card->contrnr,
1157 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1158 cmsg->adr.adrPLCI);
1159 return;
1160}
1161
1162static void handle_ncci(_cmsg * cmsg)
1163{
1164 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1165 capidrv_plci *plcip;
1166 capidrv_ncci *nccip;
1167 isdn_ctrl cmd;
1168 int len;
1169
1170 if (!card) {
1171 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1172 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1173 cmsg->adr.adrController & 0x7f);
1174 return;
1175 }
1176 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1177
1178 case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */
1179 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1180 goto notfound;
1181
1182 capi_cmsg_answer(cmsg);
1183 send_message(card, cmsg);
1184 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
1185
1186 cmd.command = ISDN_STAT_BCONN;
1187 cmd.driver = card->myid;
1188 cmd.arg = nccip->chan;
1189 card->interface.statcallb(&cmd);
1190
1191 printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
1192 card->contrnr, nccip->chan, nccip->ncci);
1193 break;
1194
1195 case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */
1196 goto ignored;
1197
1198 case CAPI_CONNECT_B3_IND: /* ncci */
1199
1200 plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
1201 if (plcip) {
1202 nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
1203 if (nccip) {
1204 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
1205 capi_fill_CONNECT_B3_RESP(cmsg,
1206 global.ap.applid,
1207 card->msgid++,
1208 nccip->ncci, /* adr */
1209 0, /* Reject */
1210 NULL /* NCPI */
1211 );
1212 send_message(card, cmsg);
1213 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
1214 break;
1215 }
1216 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1217 } else {
1218 printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
1219 card->contrnr,
1220 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1221 cmsg->adr.adrNCCI);
1222 }
1223 capi_fill_CONNECT_B3_RESP(cmsg,
1224 global.ap.applid,
1225 card->msgid++,
1226 cmsg->adr.adrNCCI,
1227 2, /* Reject */
1228 NULL /* NCPI */
1229 );
1230 send_message(card, cmsg);
1231 break;
1232
1233 case CAPI_CONNECT_B3_CONF: /* ncci */
1234
1235 if (!(nccip = find_ncci_by_msgid(card,
1236 cmsg->adr.adrNCCI,
1237 cmsg->Messagenumber)))
1238 goto notfound;
1239
1240 nccip->ncci = cmsg->adr.adrNCCI;
1241 if (cmsg->Info) {
1242 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1243 card->contrnr,
1244 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1245 cmsg->Info, capi_info2str(cmsg->Info),
1246 cmsg->adr.adrNCCI);
1247 }
1248
1249 if (cmsg->Info)
1250 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
1251 else
1252 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
1253 break;
1254
1255 case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */
1256 capi_cmsg_answer(cmsg);
1257 send_message(card, cmsg);
1258 break;
1259
1260 case CAPI_DATA_B3_IND: /* ncci */
1261 /* handled in handle_data() */
1262 goto ignored;
1263
1264 case CAPI_DATA_B3_CONF: /* ncci */
1265 if (cmsg->Info) {
1266 printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
1267 cmsg->Info, capi_info2str(cmsg->Info));
1268 }
1269 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1270 goto notfound;
1271
1272 len = capidrv_del_ack(nccip, cmsg->DataHandle);
1273 if (len < 0)
1274 break;
1275 cmd.command = ISDN_STAT_BSENT;
1276 cmd.driver = card->myid;
1277 cmd.arg = nccip->chan;
1278 cmd.parm.length = len;
1279 card->interface.statcallb(&cmd);
1280 break;
1281
1282 case CAPI_DISCONNECT_B3_IND: /* ncci */
1283 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1284 goto notfound;
1285
1286 card->bchans[nccip->chan].disconnecting = 1;
1287 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
1288 capi_cmsg_answer(cmsg);
1289 send_message(card, cmsg);
1290 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
1291 break;
1292
1293 case CAPI_DISCONNECT_B3_CONF: /* ncci */
1294 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1295 goto notfound;
1296 if (cmsg->Info) {
1297 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1298 card->contrnr,
1299 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1300 cmsg->Info, capi_info2str(cmsg->Info),
1301 cmsg->adr.adrNCCI);
1302 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
1303 }
1304 break;
1305
1306 case CAPI_RESET_B3_IND: /* ncci */
1307 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1308 goto notfound;
1309 ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
1310 capi_cmsg_answer(cmsg);
1311 send_message(card, cmsg);
1312 break;
1313
1314 case CAPI_RESET_B3_CONF: /* ncci */
1315 goto ignored; /* $$$$ */
1316
1317 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1318 goto ignored;
1319 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1320 goto ignored;
1321
1322 default:
1323 printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
1324 card->contrnr,
1325 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1326 cmsg->adr.adrNCCI);
1327 }
1328 return;
1329 ignored:
1330 printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
1331 card->contrnr,
1332 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1333 cmsg->adr.adrNCCI);
1334 return;
1335 notfound:
1336 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1337 card->contrnr,
1338 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1339 cmsg->adr.adrNCCI);
1340}
1341
1342
1343static void handle_data(_cmsg * cmsg, struct sk_buff *skb)
1344{
1345 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1346 capidrv_ncci *nccip;
1347
1348 if (!card) {
1349 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1350 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1351 cmsg->adr.adrController & 0x7f);
1352 kfree_skb(skb);
1353 return;
1354 }
1355 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
1356 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1357 card->contrnr,
1358 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1359 cmsg->adr.adrNCCI);
1360 kfree_skb(skb);
1361 return;
1362 }
1363 (void) skb_pull(skb, CAPIMSG_LEN(skb->data));
1364 card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
1365 capi_cmsg_answer(cmsg);
1366 send_message(card, cmsg);
1367}
1368
1369static _cmsg s_cmsg;
1370
1371static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
1372{
1373 capi_message2cmsg(&s_cmsg, skb->data);
1374 if (debugmode > 3)
1375 printk(KERN_DEBUG "capidrv_signal: applid=%d %s\n",
1376 ap->applid, capi_cmsg2str(&s_cmsg));
1377
1378 if (s_cmsg.Command == CAPI_DATA_B3
1379 && s_cmsg.Subcommand == CAPI_IND) {
1380 handle_data(&s_cmsg, skb);
1381 return;
1382 }
1383 if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
1384 handle_controller(&s_cmsg);
1385 else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
1386 handle_plci(&s_cmsg);
1387 else
1388 handle_ncci(&s_cmsg);
1389 /*
1390 * data of skb used in s_cmsg,
1391 * free data when s_cmsg is not used again
1392 * thanks to Lars Heete <hel@admin.de>
1393 */
1394 kfree_skb(skb);
1395}
1396
1397/* ------------------------------------------------------------------- */
1398
1399#define PUTBYTE_TO_STATUS(card, byte) \
1400 do { \
1401 *(card)->q931_write++ = (byte); \
1402 if ((card)->q931_write > (card)->q931_end) \
1403 (card)->q931_write = (card)->q931_buf; \
1404 } while (0)
1405
1406static void handle_dtrace_data(capidrv_contr *card,
1407 int send, int level2, u8 *data, u16 len)
1408{
1409 u8 *p, *end;
1410 isdn_ctrl cmd;
1411
1412 if (!len) {
1413 printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
1414 card->contrnr, len);
1415 return;
1416 }
1417
1418 if (level2) {
1419 PUTBYTE_TO_STATUS(card, 'D');
1420 PUTBYTE_TO_STATUS(card, '2');
1421 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1422 PUTBYTE_TO_STATUS(card, ':');
1423 } else {
1424 PUTBYTE_TO_STATUS(card, 'D');
1425 PUTBYTE_TO_STATUS(card, '3');
1426 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1427 PUTBYTE_TO_STATUS(card, ':');
1428 }
1429
1430 for (p = data, end = data+len; p < end; p++) {
1431 u8 w;
1432 PUTBYTE_TO_STATUS(card, ' ');
1433 w = (*p >> 4) & 0xf;
1434 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1435 w = *p & 0xf;
1436 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1437 }
1438 PUTBYTE_TO_STATUS(card, '\n');
1439
1440 cmd.command = ISDN_STAT_STAVAIL;
1441 cmd.driver = card->myid;
1442 cmd.arg = len*3+5;
1443 card->interface.statcallb(&cmd);
1444}
1445
1446/* ------------------------------------------------------------------- */
1447
1448static _cmsg cmdcmsg;
1449
1450static int capidrv_ioctl(isdn_ctrl * c, capidrv_contr * card)
1451{
1452 switch (c->arg) {
1453 case 1:
1454 debugmode = (int)(*((unsigned int *)c->parm.num));
1455 printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
1456 card->contrnr, debugmode);
1457 return 0;
1458 default:
1459 printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
1460 card->contrnr, c->arg);
1461 return -EINVAL;
1462 }
1463 return -EINVAL;
1464}
1465
1466/*
1467 * Handle leased lines (CAPI-Bundling)
1468 */
1469
1470struct internal_bchannelinfo {
1471 unsigned short channelalloc;
1472 unsigned short operation;
1473 unsigned char cmask[31];
1474};
1475
1476static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
1477{
1478 unsigned long bmask = 0;
1479 int active = !0;
1480 char *s;
1481 int i;
1482
1483 if (strncmp(teln, "FV:", 3) != 0)
1484 return 1;
1485 s = teln + 3;
1486 while (*s && *s == ' ') s++;
1487 if (!*s) return -2;
1488 if (*s == 'p' || *s == 'P') {
1489 active = 0;
1490 s++;
1491 }
1492 if (*s == 'a' || *s == 'A') {
1493 active = !0;
1494 s++;
1495 }
1496 while (*s) {
1497 int digit1 = 0;
1498 int digit2 = 0;
1499 if (!isdigit(*s)) return -3;
1500 while (isdigit(*s)) { digit1 = digit1*10 + (*s - '0'); s++; }
1501 if (digit1 <= 0 && digit1 > 30) return -4;
1502 if (*s == 0 || *s == ',' || *s == ' ') {
1503 bmask |= (1 << digit1);
1504 digit1 = 0;
1505 if (*s) s++;
1506 continue;
1507 }
1508 if (*s != '-') return -5;
1509 s++;
1510 if (!isdigit(*s)) return -3;
1511 while (isdigit(*s)) { digit2 = digit2*10 + (*s - '0'); s++; }
1512 if (digit2 <= 0 && digit2 > 30) return -4;
1513 if (*s == 0 || *s == ',' || *s == ' ') {
1514 if (digit1 > digit2)
1515 for (i = digit2; i <= digit1 ; i++)
1516 bmask |= (1 << i);
1517 else
1518 for (i = digit1; i <= digit2 ; i++)
1519 bmask |= (1 << i);
1520 digit1 = digit2 = 0;
1521 if (*s) s++;
1522 continue;
1523 }
1524 return -6;
1525 }
1526 if (activep) *activep = active;
1527 if (bmaskp) *bmaskp = bmask;
1528 return 0;
1529}
1530
1531static int FVteln2capi20(char *teln, u8 AdditionalInfo[1+2+2+31])
1532{
1533 unsigned long bmask;
1534 int active;
1535 int rc, i;
1536
1537 rc = decodeFVteln(teln, &bmask, &active);
1538 if (rc) return rc;
1539 /* Length */
1540 AdditionalInfo[0] = 2+2+31;
1541 /* Channel: 3 => use channel allocation */
1542 AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
1543 /* Operation: 0 => DTE mode, 1 => DCE mode */
1544 if (active) {
1545 AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
1546 } else {
1547 AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
1548 }
1549 /* Channel mask array */
1550 AdditionalInfo[5] = 0; /* no D-Channel */
1551 for (i=1; i <= 30; i++)
1552 AdditionalInfo[5+i] = (bmask & (1 << i)) ? 0xff : 0;
1553 return 0;
1554}
1555
1556static int capidrv_command(isdn_ctrl * c, capidrv_contr * card)
1557{
1558 isdn_ctrl cmd;
1559 struct capidrv_bchan *bchan;
1560 struct capidrv_plci *plcip;
1561 u8 AdditionalInfo[1+2+2+31];
1562 int rc, isleasedline = 0;
1563
1564 if (c->command == ISDN_CMD_IOCTL)
1565 return capidrv_ioctl(c, card);
1566
1567 switch (c->command) {
1568 case ISDN_CMD_DIAL:{
1569 u8 calling[ISDN_MSNLEN + 3];
1570 u8 called[ISDN_MSNLEN + 2];
1571
1572 if (debugmode)
1573 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
1574 card->contrnr,
1575 c->arg,
1576 c->parm.setup.phone,
1577 c->parm.setup.si1,
1578 c->parm.setup.si2,
1579 c->parm.setup.eazmsn);
1580
1581 bchan = &card->bchans[c->arg % card->nbchan];
1582
1583 if (bchan->plcip) {
1584 printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
1585 card->contrnr,
1586 c->arg,
1587 c->parm.setup.phone,
1588 c->parm.setup.si1,
1589 c->parm.setup.si2,
1590 c->parm.setup.eazmsn,
1591 bchan->plcip->plci);
1592 return 0;
1593 }
1594 bchan->si1 = c->parm.setup.si1;
1595 bchan->si2 = c->parm.setup.si2;
1596
1597 strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
1598 strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
1599 rc = FVteln2capi20(bchan->num, AdditionalInfo);
1600 isleasedline = (rc == 0);
1601 if (rc < 0)
1602 printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
1603
1604 if (isleasedline) {
1605 calling[0] = 0;
1606 called[0] = 0;
1607 if (debugmode)
1608 printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
1609 } else {
1610 calling[0] = strlen(bchan->mynum) + 2;
1611 calling[1] = 0;
1612 calling[2] = 0x80;
1613 strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
1614 called[0] = strlen(bchan->num) + 1;
1615 called[1] = 0x80;
1616 strncpy(called + 2, bchan->num, ISDN_MSNLEN);
1617 }
1618
1619 capi_fill_CONNECT_REQ(&cmdcmsg,
1620 global.ap.applid,
1621 card->msgid++,
1622 card->contrnr, /* adr */
1623 si2cip(bchan->si1, bchan->si2), /* cipvalue */
1624 called, /* CalledPartyNumber */
1625 calling, /* CallingPartyNumber */
1626 NULL, /* CalledPartySubaddress */
1627 NULL, /* CallingPartySubaddress */
1628 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1629 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1630 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1631 b1config(bchan->l2, bchan->l3), /* B1configuration */
1632 NULL, /* B2configuration */
1633 NULL, /* B3configuration */
1634 NULL, /* BC */
1635 NULL, /* LLC */
1636 NULL, /* HLC */
1637 /* BChannelinformation */
1638 isleasedline ? AdditionalInfo : NULL,
1639 NULL, /* Keypadfacility */
1640 NULL, /* Useruserdata */
1641 NULL /* Facilitydataarray */
1642 );
1643 if ((plcip = new_plci(card, (c->arg % card->nbchan))) == 0) {
1644 cmd.command = ISDN_STAT_DHUP;
1645 cmd.driver = card->myid;
1646 cmd.arg = (c->arg % card->nbchan);
1647 card->interface.statcallb(&cmd);
1648 return -1;
1649 }
1650 plcip->msgid = cmdcmsg.Messagenumber;
1651 plcip->leasedline = isleasedline;
1652 plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
1653 send_message(card, &cmdcmsg);
1654 return 0;
1655 }
1656
1657 case ISDN_CMD_ACCEPTD:
1658
1659 bchan = &card->bchans[c->arg % card->nbchan];
1660 if (debugmode)
1661 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
1662 card->contrnr,
1663 c->arg, bchan->l2, bchan->l3);
1664
1665 capi_fill_CONNECT_RESP(&cmdcmsg,
1666 global.ap.applid,
1667 card->msgid++,
1668 bchan->plcip->plci, /* adr */
1669 0, /* Reject */
1670 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1671 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1672 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1673 b1config(bchan->l2, bchan->l3), /* B1configuration */
1674 NULL, /* B2configuration */
1675 NULL, /* B3configuration */
1676 NULL, /* ConnectedNumber */
1677 NULL, /* ConnectedSubaddress */
1678 NULL, /* LLC */
1679 NULL, /* BChannelinformation */
1680 NULL, /* Keypadfacility */
1681 NULL, /* Useruserdata */
1682 NULL /* Facilitydataarray */
1683 );
1684 capi_cmsg2message(&cmdcmsg, cmdcmsg.buf);
1685 plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
1686 send_message(card, &cmdcmsg);
1687 return 0;
1688
1689 case ISDN_CMD_ACCEPTB:
1690 if (debugmode)
1691 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
1692 card->contrnr,
1693 c->arg);
1694 return -ENOSYS;
1695
1696 case ISDN_CMD_HANGUP:
1697 if (debugmode)
1698 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
1699 card->contrnr,
1700 c->arg);
1701 bchan = &card->bchans[c->arg % card->nbchan];
1702
1703 if (bchan->disconnecting) {
1704 if (debugmode)
1705 printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
1706 card->contrnr,
1707 c->arg);
1708 return 0;
1709 }
1710 if (bchan->nccip) {
1711 bchan->disconnecting = 1;
1712 capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
1713 global.ap.applid,
1714 card->msgid++,
1715 bchan->nccip->ncci,
1716 NULL /* NCPI */
1717 );
1718 ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
1719 send_message(card, &cmdcmsg);
1720 return 0;
1721 } else if (bchan->plcip) {
1722 if (bchan->plcip->state == ST_PLCI_INCOMING) {
1723 /*
1724 * just ignore, we a called from
1725 * isdn_status_callback(),
1726 * which will return 0 or 2, this is handled
1727 * by the CONNECT_IND handler
1728 */
1729 bchan->disconnecting = 1;
1730 return 0;
1731 } else if (bchan->plcip->plci) {
1732 bchan->disconnecting = 1;
1733 capi_fill_DISCONNECT_REQ(&cmdcmsg,
1734 global.ap.applid,
1735 card->msgid++,
1736 bchan->plcip->plci,
1737 NULL, /* BChannelinformation */
1738 NULL, /* Keypadfacility */
1739 NULL, /* Useruserdata */
1740 NULL /* Facilitydataarray */
1741 );
1742 plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
1743 send_message(card, &cmdcmsg);
1744 return 0;
1745 } else {
1746 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
1747 card->contrnr,
1748 c->arg);
1749 return -EINVAL;
1750 }
1751 }
1752 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
1753 card->contrnr,
1754 c->arg);
1755 return -EINVAL;
1756/* ready */
1757
1758 case ISDN_CMD_SETL2:
1759 if (debugmode)
1760 printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
1761 card->contrnr,
1762 (c->arg & 0xff), (c->arg >> 8));
1763 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1764 bchan->l2 = (c->arg >> 8);
1765 return 0;
1766
1767 case ISDN_CMD_SETL3:
1768 if (debugmode)
1769 printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
1770 card->contrnr,
1771 (c->arg & 0xff), (c->arg >> 8));
1772 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1773 bchan->l3 = (c->arg >> 8);
1774 return 0;
1775
1776 case ISDN_CMD_SETEAZ:
1777 if (debugmode)
1778 printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
1779 card->contrnr,
1780 c->parm.num, c->arg);
1781 bchan = &card->bchans[c->arg % card->nbchan];
1782 strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
1783 return 0;
1784
1785 case ISDN_CMD_CLREAZ:
1786 if (debugmode)
1787 printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
1788 card->contrnr, c->arg);
1789 bchan = &card->bchans[c->arg % card->nbchan];
1790 bchan->msn[0] = 0;
1791 return 0;
1792
1793 default:
1794 printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
1795 card->contrnr, c->command);
1796 return -EINVAL;
1797 }
1798 return 0;
1799}
1800
1801static int if_command(isdn_ctrl * c)
1802{
1803 capidrv_contr *card = findcontrbydriverid(c->driver);
1804
1805 if (card)
1806 return capidrv_command(c, card);
1807
1808 printk(KERN_ERR
1809 "capidrv: if_command %d called with invalid driverId %d!\n",
1810 c->command, c->driver);
1811 return -ENODEV;
1812}
1813
1814static _cmsg sendcmsg;
1815
1816static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
1817{
1818 capidrv_contr *card = findcontrbydriverid(id);
1819 capidrv_bchan *bchan;
1820 capidrv_ncci *nccip;
1821 int len = skb->len;
1822 int msglen;
1823 u16 errcode;
1824 u16 datahandle;
1825
1826 if (!card) {
1827 printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
1828 id);
1829 return 0;
1830 }
1831 if (debugmode > 4)
1832 printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
1833 card->contrnr, len, skb, doack);
1834 bchan = &card->bchans[channel % card->nbchan];
1835 nccip = bchan->nccip;
1836 if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
1837 printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
1838 card->contrnr, card->name, channel);
1839 return 0;
1840 }
1841 datahandle = nccip->datahandle;
1842 capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
1843 nccip->ncci, /* adr */
1844 (u32) skb->data, /* Data */
1845 skb->len, /* DataLength */
1846 datahandle, /* DataHandle */
1847 0 /* Flags */
1848 );
1849
1850 if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
1851 return 0;
1852
1853 capi_cmsg2message(&sendcmsg, sendcmsg.buf);
1854 msglen = CAPIMSG_LEN(sendcmsg.buf);
1855 if (skb_headroom(skb) < msglen) {
1856 struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
1857 if (!nskb) {
1858 printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
1859 card->contrnr);
1860 (void)capidrv_del_ack(nccip, datahandle);
1861 return 0;
1862 }
1863 printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
1864 card->contrnr, skb_headroom(skb), msglen);
1865 memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
1866 errcode = capi20_put_message(&global.ap, nskb);
1867 if (errcode == CAPI_NOERROR) {
1868 dev_kfree_skb(skb);
1869 nccip->datahandle++;
1870 return len;
1871 }
1872 if (debugmode > 3)
1873 printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
1874 card->contrnr, errcode, capi_info2str(errcode));
1875 (void)capidrv_del_ack(nccip, datahandle);
1876 dev_kfree_skb(nskb);
1877 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1878 } else {
1879 memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
1880 errcode = capi20_put_message(&global.ap, skb);
1881 if (errcode == CAPI_NOERROR) {
1882 nccip->datahandle++;
1883 return len;
1884 }
1885 if (debugmode > 3)
1886 printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
1887 card->contrnr, errcode, capi_info2str(errcode));
1888 skb_pull(skb, msglen);
1889 (void)capidrv_del_ack(nccip, datahandle);
1890 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1891 }
1892}
1893
1894static int if_readstat(u8 __user *buf, int len, int id, int channel)
1895{
1896 capidrv_contr *card = findcontrbydriverid(id);
1897 int count;
1898 u8 __user *p;
1899
1900 if (!card) {
1901 printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
1902 id);
1903 return -ENODEV;
1904 }
1905
1906 for (p=buf, count=0; count < len; p++, count++) {
Jeff Garzik7786ce12006-10-17 00:10:40 -07001907 if (put_user(*card->q931_read++, p))
1908 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (card->q931_read > card->q931_end)
1910 card->q931_read = card->q931_buf;
1911 }
1912 return count;
1913
1914}
1915
1916static void enable_dchannel_trace(capidrv_contr *card)
1917{
1918 u8 manufacturer[CAPI_MANUFACTURER_LEN];
1919 capi_version version;
1920 u16 contr = card->contrnr;
1921 u16 errcode;
1922 u16 avmversion[3];
1923
1924 errcode = capi20_get_manufacturer(contr, manufacturer);
1925 if (errcode != CAPI_NOERROR) {
1926 printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
1927 card->name, errcode);
1928 return;
1929 }
1930 if (strstr(manufacturer, "AVM") == 0) {
1931 printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
1932 card->name, manufacturer);
1933 return;
1934 }
1935 errcode = capi20_get_version(contr, &version);
1936 if (errcode != CAPI_NOERROR) {
1937 printk(KERN_ERR "%s: can't get version (0x%x)\n",
1938 card->name, errcode);
1939 return;
1940 }
1941 avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
1942 avmversion[1] = (version.majormanuversion << 4) & 0xf0;
1943 avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
1944 avmversion[2] |= version.minormanuversion & 0x0f;
1945
1946 if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
1947 printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
1948 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1949 card->msgid++,
1950 contr,
1951 0x214D5641, /* ManuID */
1952 0, /* Class */
1953 1, /* Function */
1954 (_cstruct)"\004\200\014\000\000");
1955 } else {
1956 printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
1957 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1958 card->msgid++,
1959 contr,
1960 0x214D5641, /* ManuID */
1961 0, /* Class */
1962 1, /* Function */
1963 (_cstruct)"\004\002\003\000\000");
1964 }
1965 send_message(card, &cmdcmsg);
1966}
1967
1968
1969static void send_listen(capidrv_contr *card)
1970{
1971 capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
1972 card->msgid++,
1973 card->contrnr, /* controller */
1974 1 << 6, /* Infomask */
1975 card->cipmask,
1976 card->cipmask2,
1977 NULL, NULL);
1978 send_message(card, &cmdcmsg);
1979 listen_change_state(card, EV_LISTEN_REQ);
1980}
1981
1982static void listentimerfunc(unsigned long x)
1983{
1984 capidrv_contr *card = (capidrv_contr *)x;
1985 if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
1986 printk(KERN_ERR "%s: controller dead ??\n", card->name);
1987 send_listen(card);
1988 mod_timer(&card->listentimer, jiffies + 60*HZ);
1989}
1990
1991
1992static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
1993{
1994 capidrv_contr *card;
1995 unsigned long flags;
1996 isdn_ctrl cmd;
1997 char id[20];
1998 int i;
1999
2000 sprintf(id, "capidrv-%d", contr);
2001 if (!try_module_get(THIS_MODULE)) {
2002 printk(KERN_WARNING "capidrv: (%s) Could not reserve module\n", id);
2003 return -1;
2004 }
Burman Yan41f96932006-12-08 02:39:35 -08002005 if (!(card = kzalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 printk(KERN_WARNING
2007 "capidrv: (%s) Could not allocate contr-struct.\n", id);
2008 return -1;
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 card->owner = THIS_MODULE;
2011 init_timer(&card->listentimer);
2012 strcpy(card->name, id);
2013 card->contrnr = contr;
2014 card->nbchan = profp->nbchannel;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002015 card->bchans = kmalloc(sizeof(capidrv_bchan) * card->nbchan, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (!card->bchans) {
2017 printk(KERN_WARNING
2018 "capidrv: (%s) Could not allocate bchan-structs.\n", id);
2019 module_put(card->owner);
2020 kfree(card);
2021 return -1;
2022 }
2023 card->interface.channels = profp->nbchannel;
2024 card->interface.maxbufsize = 2048;
2025 card->interface.command = if_command;
2026 card->interface.writebuf_skb = if_sendbuf;
2027 card->interface.writecmd = NULL;
2028 card->interface.readstat = if_readstat;
2029 card->interface.features = ISDN_FEATURE_L2_HDLC |
2030 ISDN_FEATURE_L2_TRANS |
2031 ISDN_FEATURE_L3_TRANS |
2032 ISDN_FEATURE_P_UNKNOWN |
2033 ISDN_FEATURE_L2_X75I |
2034 ISDN_FEATURE_L2_X75UI |
2035 ISDN_FEATURE_L2_X75BUI;
2036 if (profp->support1 & (1<<2))
2037 card->interface.features |= ISDN_FEATURE_L2_V11096 |
2038 ISDN_FEATURE_L2_V11019 |
2039 ISDN_FEATURE_L2_V11038;
2040 if (profp->support1 & (1<<8))
2041 card->interface.features |= ISDN_FEATURE_L2_MODEM;
2042 card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
2043 strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
2044
2045
2046 card->q931_read = card->q931_buf;
2047 card->q931_write = card->q931_buf;
2048 card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
2049
2050 if (!register_isdn(&card->interface)) {
2051 printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
2052 kfree(card->bchans);
2053 module_put(card->owner);
2054 kfree(card);
2055 return -1;
2056 }
2057 card->myid = card->interface.channels;
2058 memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
2059 for (i = 0; i < card->nbchan; i++) {
2060 card->bchans[i].contr = card;
2061 }
2062
2063 spin_lock_irqsave(&global_lock, flags);
2064 card->next = global.contr_list;
2065 global.contr_list = card;
2066 global.ncontr++;
2067 spin_unlock_irqrestore(&global_lock, flags);
2068
2069 cmd.command = ISDN_STAT_RUN;
2070 cmd.driver = card->myid;
2071 card->interface.statcallb(&cmd);
2072
2073 card->cipmask = 0x1FFF03FF; /* any */
2074 card->cipmask2 = 0;
2075
2076 card->listentimer.data = (unsigned long)card;
2077 card->listentimer.function = listentimerfunc;
2078 send_listen(card);
2079 mod_timer(&card->listentimer, jiffies + 60*HZ);
2080
2081 printk(KERN_INFO "%s: now up (%d B channels)\n",
2082 card->name, card->nbchan);
2083
2084 enable_dchannel_trace(card);
2085
2086 return 0;
2087}
2088
2089static int capidrv_delcontr(u16 contr)
2090{
2091 capidrv_contr **pp, *card;
2092 unsigned long flags;
2093 isdn_ctrl cmd;
2094
2095 spin_lock_irqsave(&global_lock, flags);
2096 for (card = global.contr_list; card; card = card->next) {
2097 if (card->contrnr == contr)
2098 break;
2099 }
2100 if (!card) {
2101 spin_unlock_irqrestore(&global_lock, flags);
2102 printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
2103 return -1;
2104 }
2105 #warning FIXME: maybe a race condition the card should be removed here from global list /kkeil
2106 spin_unlock_irqrestore(&global_lock, flags);
2107
2108 del_timer(&card->listentimer);
2109
2110 if (debugmode)
2111 printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
2112 card->contrnr, card->myid);
2113
2114 cmd.command = ISDN_STAT_STOP;
2115 cmd.driver = card->myid;
2116 card->interface.statcallb(&cmd);
2117
2118 while (card->nbchan) {
2119
2120 cmd.command = ISDN_STAT_DISCH;
2121 cmd.driver = card->myid;
2122 cmd.arg = card->nbchan-1;
2123 cmd.parm.num[0] = 0;
2124 if (debugmode)
2125 printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
2126 card->contrnr, card->myid, cmd.arg);
2127 card->interface.statcallb(&cmd);
2128
2129 if (card->bchans[card->nbchan-1].nccip)
2130 free_ncci(card, card->bchans[card->nbchan-1].nccip);
2131 if (card->bchans[card->nbchan-1].plcip)
2132 free_plci(card, card->bchans[card->nbchan-1].plcip);
2133 if (card->plci_list)
2134 printk(KERN_ERR "capidrv: bug in free_plci()\n");
2135 card->nbchan--;
2136 }
2137 kfree(card->bchans);
2138 card->bchans = NULL;
2139
2140 if (debugmode)
2141 printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
2142 card->contrnr, card->myid);
2143
2144 cmd.command = ISDN_STAT_UNLOAD;
2145 cmd.driver = card->myid;
2146 card->interface.statcallb(&cmd);
2147
2148 if (debugmode)
2149 printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
2150 card->contrnr, card->myid);
2151
2152 spin_lock_irqsave(&global_lock, flags);
2153 for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
2154 if (*pp == card) {
2155 *pp = (*pp)->next;
2156 card->next = NULL;
2157 global.ncontr--;
2158 break;
2159 }
2160 }
2161 spin_unlock_irqrestore(&global_lock, flags);
2162
2163 module_put(card->owner);
2164 printk(KERN_INFO "%s: now down.\n", card->name);
2165 kfree(card);
2166 return 0;
2167}
2168
2169
2170static void lower_callback(unsigned int cmd, u32 contr, void *data)
2171{
2172
2173 switch (cmd) {
2174 case KCI_CONTRUP:
2175 printk(KERN_INFO "capidrv: controller %hu up\n", contr);
2176 (void) capidrv_addcontr(contr, (capi_profile *) data);
2177 break;
2178 case KCI_CONTRDOWN:
2179 printk(KERN_INFO "capidrv: controller %hu down\n", contr);
2180 (void) capidrv_delcontr(contr);
2181 break;
2182 }
2183}
2184
2185/*
2186 * /proc/capi/capidrv:
2187 * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
2188 */
2189static int proc_capidrv_read_proc(char *page, char **start, off_t off,
2190 int count, int *eof, void *data)
2191{
2192 int len = 0;
2193
2194 len += sprintf(page+len, "%lu %lu %lu %lu\n",
2195 global.ap.nrecvctlpkt,
2196 global.ap.nrecvdatapkt,
2197 global.ap.nsentctlpkt,
2198 global.ap.nsentdatapkt);
2199 if (off+count >= len)
2200 *eof = 1;
2201 if (len < off)
2202 return 0;
2203 *start = page + off;
2204 return ((count < len-off) ? count : len-off);
2205}
2206
2207static struct procfsentries {
2208 char *name;
2209 mode_t mode;
2210 int (*read_proc)(char *page, char **start, off_t off,
2211 int count, int *eof, void *data);
2212 struct proc_dir_entry *procent;
2213} procfsentries[] = {
2214 /* { "capi", S_IFDIR, 0 }, */
2215 { "capi/capidrv", 0 , proc_capidrv_read_proc },
2216};
2217
2218static void __init proc_init(void)
2219{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08002220 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 int i;
2222
2223 for (i=0; i < nelem; i++) {
2224 struct procfsentries *p = procfsentries + i;
2225 p->procent = create_proc_entry(p->name, p->mode, NULL);
2226 if (p->procent) p->procent->read_proc = p->read_proc;
2227 }
2228}
2229
2230static void __exit proc_exit(void)
2231{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08002232 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int i;
2234
2235 for (i=nelem-1; i >= 0; i--) {
2236 struct procfsentries *p = procfsentries + i;
2237 if (p->procent) {
2238 remove_proc_entry(p->name, NULL);
2239 p->procent = NULL;
2240 }
2241 }
2242}
2243
2244static int __init capidrv_init(void)
2245{
2246 capi_profile profile;
2247 char rev[32];
2248 char *p;
2249 u32 ncontr, contr;
2250 u16 errcode;
2251
2252 if ((p = strchr(revision, ':')) != 0 && p[1]) {
2253 strncpy(rev, p + 2, sizeof(rev));
2254 rev[sizeof(rev)-1] = 0;
2255 if ((p = strchr(rev, '$')) != 0 && p > rev)
2256 *(p-1) = 0;
2257 } else
2258 strcpy(rev, "1.0");
2259
2260 global.ap.rparam.level3cnt = -2; /* number of bchannels twice */
2261 global.ap.rparam.datablkcnt = 16;
2262 global.ap.rparam.datablklen = 2048;
2263
2264 global.ap.recv_message = capidrv_recv_message;
2265 errcode = capi20_register(&global.ap);
2266 if (errcode) {
2267 return -EIO;
2268 }
2269
2270 capi20_set_callback(&global.ap, lower_callback);
2271
2272 errcode = capi20_get_profile(0, &profile);
2273 if (errcode != CAPI_NOERROR) {
2274 capi20_release(&global.ap);
2275 return -EIO;
2276 }
2277
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);
2284 }
2285 proc_init();
2286
2287 printk(KERN_NOTICE "capidrv: Rev %s: loaded\n", rev);
2288 return 0;
2289}
2290
2291static void __exit capidrv_exit(void)
2292{
2293 char rev[10];
2294 char *p;
2295
2296 if ((p = strchr(revision, ':')) != 0) {
2297 strcpy(rev, p + 1);
2298 p = strchr(rev, '$');
2299 *p = 0;
2300 } else {
2301 strcpy(rev, " ??? ");
2302 }
2303
2304 capi20_release(&global.ap);
2305
2306 proc_exit();
2307
2308 printk(KERN_NOTICE "capidrv: Rev%s: unloaded\n", rev);
2309}
2310
2311module_init(capidrv_init);
2312module_exit(capidrv_exit);