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