blob: 23b6f7bc16b7e53ebac2a6d73e76608bcaa67ecf [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;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800993 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (!card) {
996 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
997 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
998 cmsg->adr.adrController & 0x7f);
999 return;
1000 }
1001 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1002
1003 case CAPI_DISCONNECT_IND: /* plci */
1004 if (cmsg->Reason) {
1005 printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
1006 card->contrnr,
1007 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1008 cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
1009 }
1010 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
1011 capi_cmsg_answer(cmsg);
1012 send_message(card, cmsg);
1013 goto notfound;
1014 }
1015 card->bchans[plcip->chan].disconnecting = 1;
1016 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
1017 capi_cmsg_answer(cmsg);
1018 send_message(card, cmsg);
1019 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
1020 break;
1021
1022 case CAPI_DISCONNECT_CONF: /* plci */
1023 if (cmsg->Info) {
1024 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1025 card->contrnr,
1026 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1027 cmsg->Info, capi_info2str(cmsg->Info),
1028 cmsg->adr.adrPLCI);
1029 }
1030 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1031 goto notfound;
1032
1033 card->bchans[plcip->chan].disconnecting = 1;
1034 break;
1035
1036 case CAPI_ALERT_CONF: /* plci */
1037 if (cmsg->Info) {
1038 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1039 card->contrnr,
1040 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1041 cmsg->Info, capi_info2str(cmsg->Info),
1042 cmsg->adr.adrPLCI);
1043 }
1044 break;
1045
1046 case CAPI_CONNECT_IND: /* plci */
1047 handle_incoming_call(card, cmsg);
1048 break;
1049
1050 case CAPI_CONNECT_CONF: /* plci */
1051 if (cmsg->Info) {
1052 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1053 card->contrnr,
1054 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1055 cmsg->Info, capi_info2str(cmsg->Info),
1056 cmsg->adr.adrPLCI);
1057 }
1058 if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
1059 goto notfound;
1060
1061 plcip->plci = cmsg->adr.adrPLCI;
1062 if (cmsg->Info) {
1063 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
1064 } else {
1065 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
1066 }
1067 break;
1068
1069 case CAPI_CONNECT_ACTIVE_IND: /* plci */
1070
1071 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1072 goto notfound;
1073
1074 if (card->bchans[plcip->chan].incoming) {
1075 capi_cmsg_answer(cmsg);
1076 send_message(card, cmsg);
1077 plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1078 } else {
1079 capidrv_ncci *nccip;
1080 capi_cmsg_answer(cmsg);
1081 send_message(card, cmsg);
1082
1083 nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
1084
1085 if (!nccip) {
1086 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1087 break; /* $$$$ */
1088 }
1089 capi_fill_CONNECT_B3_REQ(cmsg,
1090 global.ap.applid,
1091 card->msgid++,
1092 plcip->plci, /* adr */
1093 NULL /* NCPI */
1094 );
1095 nccip->msgid = cmsg->Messagenumber;
1096 send_message(card, cmsg);
1097 cmd.command = ISDN_STAT_DCONN;
1098 cmd.driver = card->myid;
1099 cmd.arg = plcip->chan;
1100 card->interface.statcallb(&cmd);
1101 plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1102 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
1103 }
1104 break;
1105
1106 case CAPI_INFO_IND: /* Controller/plci */
1107
1108 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1109 goto notfound;
1110
1111 if (cmsg->InfoNumber == 0x4000) {
1112 if (cmsg->InfoElement[0] == 4) {
1113 cmd.command = ISDN_STAT_CINF;
1114 cmd.driver = card->myid;
1115 cmd.arg = plcip->chan;
1116 sprintf(cmd.parm.num, "%lu",
1117 (unsigned long)
1118 ((u32) cmsg->InfoElement[1]
1119 | ((u32) (cmsg->InfoElement[2]) << 8)
1120 | ((u32) (cmsg->InfoElement[3]) << 16)
1121 | ((u32) (cmsg->InfoElement[4]) << 24)));
1122 card->interface.statcallb(&cmd);
1123 break;
1124 }
1125 }
Karsten Keil17f0cd22007-02-28 20:13:50 -08001126 cdb = capi_cmsg2str(cmsg);
1127 if (cdb) {
1128 printk(KERN_WARNING "capidrv-%d: %s\n",
1129 card->contrnr, cdb->buf);
1130 cdebbuf_free(cdb);
1131 } else
1132 printk(KERN_WARNING "capidrv-%d: CAPI_INFO_IND InfoNumber %x not handled\n",
1133 card->contrnr, cmsg->InfoNumber);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136
1137 case CAPI_CONNECT_ACTIVE_CONF: /* plci */
1138 goto ignored;
1139 case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */
1140 goto ignored;
1141 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1142 goto ignored;
1143 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1144 goto ignored;
1145
1146 case CAPI_INFO_CONF: /* Controller/plci */
1147 goto ignored;
1148
1149 default:
1150 printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
1151 card->contrnr,
1152 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1153 cmsg->adr.adrPLCI);
1154 }
1155 return;
1156 ignored:
1157 printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
1158 card->contrnr,
1159 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1160 cmsg->adr.adrPLCI);
1161 return;
1162 notfound:
1163 printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
1164 card->contrnr,
1165 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1166 cmsg->adr.adrPLCI);
1167 return;
1168}
1169
1170static void handle_ncci(_cmsg * cmsg)
1171{
1172 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1173 capidrv_plci *plcip;
1174 capidrv_ncci *nccip;
1175 isdn_ctrl cmd;
1176 int len;
1177
1178 if (!card) {
1179 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1180 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1181 cmsg->adr.adrController & 0x7f);
1182 return;
1183 }
1184 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1185
1186 case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */
1187 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1188 goto notfound;
1189
1190 capi_cmsg_answer(cmsg);
1191 send_message(card, cmsg);
1192 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
1193
1194 cmd.command = ISDN_STAT_BCONN;
1195 cmd.driver = card->myid;
1196 cmd.arg = nccip->chan;
1197 card->interface.statcallb(&cmd);
1198
1199 printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
1200 card->contrnr, nccip->chan, nccip->ncci);
1201 break;
1202
1203 case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */
1204 goto ignored;
1205
1206 case CAPI_CONNECT_B3_IND: /* ncci */
1207
1208 plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
1209 if (plcip) {
1210 nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
1211 if (nccip) {
1212 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
1213 capi_fill_CONNECT_B3_RESP(cmsg,
1214 global.ap.applid,
1215 card->msgid++,
1216 nccip->ncci, /* adr */
1217 0, /* Reject */
1218 NULL /* NCPI */
1219 );
1220 send_message(card, cmsg);
1221 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
1222 break;
1223 }
1224 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1225 } else {
1226 printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
1227 card->contrnr,
1228 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1229 cmsg->adr.adrNCCI);
1230 }
1231 capi_fill_CONNECT_B3_RESP(cmsg,
1232 global.ap.applid,
1233 card->msgid++,
1234 cmsg->adr.adrNCCI,
1235 2, /* Reject */
1236 NULL /* NCPI */
1237 );
1238 send_message(card, cmsg);
1239 break;
1240
1241 case CAPI_CONNECT_B3_CONF: /* ncci */
1242
1243 if (!(nccip = find_ncci_by_msgid(card,
1244 cmsg->adr.adrNCCI,
1245 cmsg->Messagenumber)))
1246 goto notfound;
1247
1248 nccip->ncci = cmsg->adr.adrNCCI;
1249 if (cmsg->Info) {
1250 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1251 card->contrnr,
1252 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1253 cmsg->Info, capi_info2str(cmsg->Info),
1254 cmsg->adr.adrNCCI);
1255 }
1256
1257 if (cmsg->Info)
1258 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
1259 else
1260 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
1261 break;
1262
1263 case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */
1264 capi_cmsg_answer(cmsg);
1265 send_message(card, cmsg);
1266 break;
1267
1268 case CAPI_DATA_B3_IND: /* ncci */
1269 /* handled in handle_data() */
1270 goto ignored;
1271
1272 case CAPI_DATA_B3_CONF: /* ncci */
1273 if (cmsg->Info) {
1274 printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
1275 cmsg->Info, capi_info2str(cmsg->Info));
1276 }
1277 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1278 goto notfound;
1279
1280 len = capidrv_del_ack(nccip, cmsg->DataHandle);
1281 if (len < 0)
1282 break;
1283 cmd.command = ISDN_STAT_BSENT;
1284 cmd.driver = card->myid;
1285 cmd.arg = nccip->chan;
1286 cmd.parm.length = len;
1287 card->interface.statcallb(&cmd);
1288 break;
1289
1290 case CAPI_DISCONNECT_B3_IND: /* ncci */
1291 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1292 goto notfound;
1293
1294 card->bchans[nccip->chan].disconnecting = 1;
1295 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
1296 capi_cmsg_answer(cmsg);
1297 send_message(card, cmsg);
1298 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
1299 break;
1300
1301 case CAPI_DISCONNECT_B3_CONF: /* ncci */
1302 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1303 goto notfound;
1304 if (cmsg->Info) {
1305 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1306 card->contrnr,
1307 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1308 cmsg->Info, capi_info2str(cmsg->Info),
1309 cmsg->adr.adrNCCI);
1310 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
1311 }
1312 break;
1313
1314 case CAPI_RESET_B3_IND: /* ncci */
1315 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1316 goto notfound;
1317 ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
1318 capi_cmsg_answer(cmsg);
1319 send_message(card, cmsg);
1320 break;
1321
1322 case CAPI_RESET_B3_CONF: /* ncci */
1323 goto ignored; /* $$$$ */
1324
1325 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1326 goto ignored;
1327 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1328 goto ignored;
1329
1330 default:
1331 printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
1332 card->contrnr,
1333 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1334 cmsg->adr.adrNCCI);
1335 }
1336 return;
1337 ignored:
1338 printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
1339 card->contrnr,
1340 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1341 cmsg->adr.adrNCCI);
1342 return;
1343 notfound:
1344 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1345 card->contrnr,
1346 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1347 cmsg->adr.adrNCCI);
1348}
1349
1350
1351static void handle_data(_cmsg * cmsg, struct sk_buff *skb)
1352{
1353 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1354 capidrv_ncci *nccip;
1355
1356 if (!card) {
1357 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1358 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1359 cmsg->adr.adrController & 0x7f);
1360 kfree_skb(skb);
1361 return;
1362 }
1363 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
1364 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1365 card->contrnr,
1366 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1367 cmsg->adr.adrNCCI);
1368 kfree_skb(skb);
1369 return;
1370 }
1371 (void) skb_pull(skb, CAPIMSG_LEN(skb->data));
1372 card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
1373 capi_cmsg_answer(cmsg);
1374 send_message(card, cmsg);
1375}
1376
1377static _cmsg s_cmsg;
1378
1379static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
1380{
1381 capi_message2cmsg(&s_cmsg, skb->data);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001382 if (debugmode > 3) {
1383 _cdebbuf *cdb = capi_cmsg2str(&s_cmsg);
1384
1385 if (cdb) {
1386 printk(KERN_DEBUG "%s: applid=%d %s\n", __FUNCTION__,
1387 ap->applid, cdb->buf);
1388 cdebbuf_free(cdb);
1389 } else
1390 printk(KERN_DEBUG "%s: applid=%d %s not traced\n",
1391 __FUNCTION__, ap->applid,
1392 capi_cmd2str(s_cmsg.Command, s_cmsg.Subcommand));
1393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (s_cmsg.Command == CAPI_DATA_B3
1395 && s_cmsg.Subcommand == CAPI_IND) {
1396 handle_data(&s_cmsg, skb);
1397 return;
1398 }
1399 if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
1400 handle_controller(&s_cmsg);
1401 else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
1402 handle_plci(&s_cmsg);
1403 else
1404 handle_ncci(&s_cmsg);
1405 /*
1406 * data of skb used in s_cmsg,
1407 * free data when s_cmsg is not used again
1408 * thanks to Lars Heete <hel@admin.de>
1409 */
1410 kfree_skb(skb);
1411}
1412
1413/* ------------------------------------------------------------------- */
1414
1415#define PUTBYTE_TO_STATUS(card, byte) \
1416 do { \
1417 *(card)->q931_write++ = (byte); \
1418 if ((card)->q931_write > (card)->q931_end) \
1419 (card)->q931_write = (card)->q931_buf; \
1420 } while (0)
1421
1422static void handle_dtrace_data(capidrv_contr *card,
1423 int send, int level2, u8 *data, u16 len)
1424{
1425 u8 *p, *end;
1426 isdn_ctrl cmd;
1427
1428 if (!len) {
1429 printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
1430 card->contrnr, len);
1431 return;
1432 }
1433
1434 if (level2) {
1435 PUTBYTE_TO_STATUS(card, 'D');
1436 PUTBYTE_TO_STATUS(card, '2');
1437 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1438 PUTBYTE_TO_STATUS(card, ':');
1439 } else {
1440 PUTBYTE_TO_STATUS(card, 'D');
1441 PUTBYTE_TO_STATUS(card, '3');
1442 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1443 PUTBYTE_TO_STATUS(card, ':');
1444 }
1445
1446 for (p = data, end = data+len; p < end; p++) {
1447 u8 w;
1448 PUTBYTE_TO_STATUS(card, ' ');
1449 w = (*p >> 4) & 0xf;
1450 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1451 w = *p & 0xf;
1452 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1453 }
1454 PUTBYTE_TO_STATUS(card, '\n');
1455
1456 cmd.command = ISDN_STAT_STAVAIL;
1457 cmd.driver = card->myid;
1458 cmd.arg = len*3+5;
1459 card->interface.statcallb(&cmd);
1460}
1461
1462/* ------------------------------------------------------------------- */
1463
1464static _cmsg cmdcmsg;
1465
1466static int capidrv_ioctl(isdn_ctrl * c, capidrv_contr * card)
1467{
1468 switch (c->arg) {
1469 case 1:
1470 debugmode = (int)(*((unsigned int *)c->parm.num));
1471 printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
1472 card->contrnr, debugmode);
1473 return 0;
1474 default:
1475 printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
1476 card->contrnr, c->arg);
1477 return -EINVAL;
1478 }
1479 return -EINVAL;
1480}
1481
1482/*
1483 * Handle leased lines (CAPI-Bundling)
1484 */
1485
1486struct internal_bchannelinfo {
1487 unsigned short channelalloc;
1488 unsigned short operation;
1489 unsigned char cmask[31];
1490};
1491
1492static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
1493{
1494 unsigned long bmask = 0;
1495 int active = !0;
1496 char *s;
1497 int i;
1498
1499 if (strncmp(teln, "FV:", 3) != 0)
1500 return 1;
1501 s = teln + 3;
1502 while (*s && *s == ' ') s++;
1503 if (!*s) return -2;
1504 if (*s == 'p' || *s == 'P') {
1505 active = 0;
1506 s++;
1507 }
1508 if (*s == 'a' || *s == 'A') {
1509 active = !0;
1510 s++;
1511 }
1512 while (*s) {
1513 int digit1 = 0;
1514 int digit2 = 0;
1515 if (!isdigit(*s)) return -3;
1516 while (isdigit(*s)) { digit1 = digit1*10 + (*s - '0'); s++; }
1517 if (digit1 <= 0 && digit1 > 30) return -4;
1518 if (*s == 0 || *s == ',' || *s == ' ') {
1519 bmask |= (1 << digit1);
1520 digit1 = 0;
1521 if (*s) s++;
1522 continue;
1523 }
1524 if (*s != '-') return -5;
1525 s++;
1526 if (!isdigit(*s)) return -3;
1527 while (isdigit(*s)) { digit2 = digit2*10 + (*s - '0'); s++; }
1528 if (digit2 <= 0 && digit2 > 30) return -4;
1529 if (*s == 0 || *s == ',' || *s == ' ') {
1530 if (digit1 > digit2)
1531 for (i = digit2; i <= digit1 ; i++)
1532 bmask |= (1 << i);
1533 else
1534 for (i = digit1; i <= digit2 ; i++)
1535 bmask |= (1 << i);
1536 digit1 = digit2 = 0;
1537 if (*s) s++;
1538 continue;
1539 }
1540 return -6;
1541 }
1542 if (activep) *activep = active;
1543 if (bmaskp) *bmaskp = bmask;
1544 return 0;
1545}
1546
1547static int FVteln2capi20(char *teln, u8 AdditionalInfo[1+2+2+31])
1548{
1549 unsigned long bmask;
1550 int active;
1551 int rc, i;
1552
1553 rc = decodeFVteln(teln, &bmask, &active);
1554 if (rc) return rc;
1555 /* Length */
1556 AdditionalInfo[0] = 2+2+31;
1557 /* Channel: 3 => use channel allocation */
1558 AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
1559 /* Operation: 0 => DTE mode, 1 => DCE mode */
1560 if (active) {
1561 AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
1562 } else {
1563 AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
1564 }
1565 /* Channel mask array */
1566 AdditionalInfo[5] = 0; /* no D-Channel */
1567 for (i=1; i <= 30; i++)
1568 AdditionalInfo[5+i] = (bmask & (1 << i)) ? 0xff : 0;
1569 return 0;
1570}
1571
1572static int capidrv_command(isdn_ctrl * c, capidrv_contr * card)
1573{
1574 isdn_ctrl cmd;
1575 struct capidrv_bchan *bchan;
1576 struct capidrv_plci *plcip;
1577 u8 AdditionalInfo[1+2+2+31];
1578 int rc, isleasedline = 0;
1579
1580 if (c->command == ISDN_CMD_IOCTL)
1581 return capidrv_ioctl(c, card);
1582
1583 switch (c->command) {
1584 case ISDN_CMD_DIAL:{
1585 u8 calling[ISDN_MSNLEN + 3];
1586 u8 called[ISDN_MSNLEN + 2];
1587
1588 if (debugmode)
1589 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
1590 card->contrnr,
1591 c->arg,
1592 c->parm.setup.phone,
1593 c->parm.setup.si1,
1594 c->parm.setup.si2,
1595 c->parm.setup.eazmsn);
1596
1597 bchan = &card->bchans[c->arg % card->nbchan];
1598
1599 if (bchan->plcip) {
1600 printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
1601 card->contrnr,
1602 c->arg,
1603 c->parm.setup.phone,
1604 c->parm.setup.si1,
1605 c->parm.setup.si2,
1606 c->parm.setup.eazmsn,
1607 bchan->plcip->plci);
1608 return 0;
1609 }
1610 bchan->si1 = c->parm.setup.si1;
1611 bchan->si2 = c->parm.setup.si2;
1612
1613 strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
1614 strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
1615 rc = FVteln2capi20(bchan->num, AdditionalInfo);
1616 isleasedline = (rc == 0);
1617 if (rc < 0)
1618 printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
1619
1620 if (isleasedline) {
1621 calling[0] = 0;
1622 called[0] = 0;
1623 if (debugmode)
1624 printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
1625 } else {
1626 calling[0] = strlen(bchan->mynum) + 2;
1627 calling[1] = 0;
1628 calling[2] = 0x80;
1629 strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
1630 called[0] = strlen(bchan->num) + 1;
1631 called[1] = 0x80;
1632 strncpy(called + 2, bchan->num, ISDN_MSNLEN);
1633 }
1634
1635 capi_fill_CONNECT_REQ(&cmdcmsg,
1636 global.ap.applid,
1637 card->msgid++,
1638 card->contrnr, /* adr */
1639 si2cip(bchan->si1, bchan->si2), /* cipvalue */
1640 called, /* CalledPartyNumber */
1641 calling, /* CallingPartyNumber */
1642 NULL, /* CalledPartySubaddress */
1643 NULL, /* CallingPartySubaddress */
1644 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1645 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1646 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1647 b1config(bchan->l2, bchan->l3), /* B1configuration */
1648 NULL, /* B2configuration */
1649 NULL, /* B3configuration */
1650 NULL, /* BC */
1651 NULL, /* LLC */
1652 NULL, /* HLC */
1653 /* BChannelinformation */
1654 isleasedline ? AdditionalInfo : NULL,
1655 NULL, /* Keypadfacility */
1656 NULL, /* Useruserdata */
1657 NULL /* Facilitydataarray */
1658 );
1659 if ((plcip = new_plci(card, (c->arg % card->nbchan))) == 0) {
1660 cmd.command = ISDN_STAT_DHUP;
1661 cmd.driver = card->myid;
1662 cmd.arg = (c->arg % card->nbchan);
1663 card->interface.statcallb(&cmd);
1664 return -1;
1665 }
1666 plcip->msgid = cmdcmsg.Messagenumber;
1667 plcip->leasedline = isleasedline;
1668 plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
1669 send_message(card, &cmdcmsg);
1670 return 0;
1671 }
1672
1673 case ISDN_CMD_ACCEPTD:
1674
1675 bchan = &card->bchans[c->arg % card->nbchan];
1676 if (debugmode)
1677 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
1678 card->contrnr,
1679 c->arg, bchan->l2, bchan->l3);
1680
1681 capi_fill_CONNECT_RESP(&cmdcmsg,
1682 global.ap.applid,
1683 card->msgid++,
1684 bchan->plcip->plci, /* adr */
1685 0, /* Reject */
1686 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1687 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1688 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1689 b1config(bchan->l2, bchan->l3), /* B1configuration */
1690 NULL, /* B2configuration */
1691 NULL, /* B3configuration */
1692 NULL, /* ConnectedNumber */
1693 NULL, /* ConnectedSubaddress */
1694 NULL, /* LLC */
1695 NULL, /* BChannelinformation */
1696 NULL, /* Keypadfacility */
1697 NULL, /* Useruserdata */
1698 NULL /* Facilitydataarray */
1699 );
1700 capi_cmsg2message(&cmdcmsg, cmdcmsg.buf);
1701 plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
1702 send_message(card, &cmdcmsg);
1703 return 0;
1704
1705 case ISDN_CMD_ACCEPTB:
1706 if (debugmode)
1707 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
1708 card->contrnr,
1709 c->arg);
1710 return -ENOSYS;
1711
1712 case ISDN_CMD_HANGUP:
1713 if (debugmode)
1714 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
1715 card->contrnr,
1716 c->arg);
1717 bchan = &card->bchans[c->arg % card->nbchan];
1718
1719 if (bchan->disconnecting) {
1720 if (debugmode)
1721 printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
1722 card->contrnr,
1723 c->arg);
1724 return 0;
1725 }
1726 if (bchan->nccip) {
1727 bchan->disconnecting = 1;
1728 capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
1729 global.ap.applid,
1730 card->msgid++,
1731 bchan->nccip->ncci,
1732 NULL /* NCPI */
1733 );
1734 ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
1735 send_message(card, &cmdcmsg);
1736 return 0;
1737 } else if (bchan->plcip) {
1738 if (bchan->plcip->state == ST_PLCI_INCOMING) {
1739 /*
1740 * just ignore, we a called from
1741 * isdn_status_callback(),
1742 * which will return 0 or 2, this is handled
1743 * by the CONNECT_IND handler
1744 */
1745 bchan->disconnecting = 1;
1746 return 0;
1747 } else if (bchan->plcip->plci) {
1748 bchan->disconnecting = 1;
1749 capi_fill_DISCONNECT_REQ(&cmdcmsg,
1750 global.ap.applid,
1751 card->msgid++,
1752 bchan->plcip->plci,
1753 NULL, /* BChannelinformation */
1754 NULL, /* Keypadfacility */
1755 NULL, /* Useruserdata */
1756 NULL /* Facilitydataarray */
1757 );
1758 plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
1759 send_message(card, &cmdcmsg);
1760 return 0;
1761 } else {
1762 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
1763 card->contrnr,
1764 c->arg);
1765 return -EINVAL;
1766 }
1767 }
1768 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
1769 card->contrnr,
1770 c->arg);
1771 return -EINVAL;
1772/* ready */
1773
1774 case ISDN_CMD_SETL2:
1775 if (debugmode)
1776 printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
1777 card->contrnr,
1778 (c->arg & 0xff), (c->arg >> 8));
1779 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1780 bchan->l2 = (c->arg >> 8);
1781 return 0;
1782
1783 case ISDN_CMD_SETL3:
1784 if (debugmode)
1785 printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
1786 card->contrnr,
1787 (c->arg & 0xff), (c->arg >> 8));
1788 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1789 bchan->l3 = (c->arg >> 8);
1790 return 0;
1791
1792 case ISDN_CMD_SETEAZ:
1793 if (debugmode)
1794 printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
1795 card->contrnr,
1796 c->parm.num, c->arg);
1797 bchan = &card->bchans[c->arg % card->nbchan];
1798 strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
1799 return 0;
1800
1801 case ISDN_CMD_CLREAZ:
1802 if (debugmode)
1803 printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
1804 card->contrnr, c->arg);
1805 bchan = &card->bchans[c->arg % card->nbchan];
1806 bchan->msn[0] = 0;
1807 return 0;
1808
1809 default:
1810 printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
1811 card->contrnr, c->command);
1812 return -EINVAL;
1813 }
1814 return 0;
1815}
1816
1817static int if_command(isdn_ctrl * c)
1818{
1819 capidrv_contr *card = findcontrbydriverid(c->driver);
1820
1821 if (card)
1822 return capidrv_command(c, card);
1823
1824 printk(KERN_ERR
1825 "capidrv: if_command %d called with invalid driverId %d!\n",
1826 c->command, c->driver);
1827 return -ENODEV;
1828}
1829
1830static _cmsg sendcmsg;
1831
1832static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
1833{
1834 capidrv_contr *card = findcontrbydriverid(id);
1835 capidrv_bchan *bchan;
1836 capidrv_ncci *nccip;
1837 int len = skb->len;
1838 int msglen;
1839 u16 errcode;
1840 u16 datahandle;
1841
1842 if (!card) {
1843 printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
1844 id);
1845 return 0;
1846 }
1847 if (debugmode > 4)
1848 printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
1849 card->contrnr, len, skb, doack);
1850 bchan = &card->bchans[channel % card->nbchan];
1851 nccip = bchan->nccip;
1852 if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
1853 printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
1854 card->contrnr, card->name, channel);
1855 return 0;
1856 }
1857 datahandle = nccip->datahandle;
1858 capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
1859 nccip->ncci, /* adr */
1860 (u32) skb->data, /* Data */
1861 skb->len, /* DataLength */
1862 datahandle, /* DataHandle */
1863 0 /* Flags */
1864 );
1865
1866 if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
1867 return 0;
1868
1869 capi_cmsg2message(&sendcmsg, sendcmsg.buf);
1870 msglen = CAPIMSG_LEN(sendcmsg.buf);
1871 if (skb_headroom(skb) < msglen) {
1872 struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
1873 if (!nskb) {
1874 printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
1875 card->contrnr);
1876 (void)capidrv_del_ack(nccip, datahandle);
1877 return 0;
1878 }
1879 printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
1880 card->contrnr, skb_headroom(skb), msglen);
1881 memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
1882 errcode = capi20_put_message(&global.ap, nskb);
1883 if (errcode == CAPI_NOERROR) {
1884 dev_kfree_skb(skb);
1885 nccip->datahandle++;
1886 return len;
1887 }
1888 if (debugmode > 3)
1889 printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
1890 card->contrnr, errcode, capi_info2str(errcode));
1891 (void)capidrv_del_ack(nccip, datahandle);
1892 dev_kfree_skb(nskb);
1893 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1894 } else {
1895 memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
1896 errcode = capi20_put_message(&global.ap, skb);
1897 if (errcode == CAPI_NOERROR) {
1898 nccip->datahandle++;
1899 return len;
1900 }
1901 if (debugmode > 3)
1902 printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
1903 card->contrnr, errcode, capi_info2str(errcode));
1904 skb_pull(skb, msglen);
1905 (void)capidrv_del_ack(nccip, datahandle);
1906 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1907 }
1908}
1909
1910static int if_readstat(u8 __user *buf, int len, int id, int channel)
1911{
1912 capidrv_contr *card = findcontrbydriverid(id);
1913 int count;
1914 u8 __user *p;
1915
1916 if (!card) {
1917 printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
1918 id);
1919 return -ENODEV;
1920 }
1921
1922 for (p=buf, count=0; count < len; p++, count++) {
Jeff Garzik7786ce12006-10-17 00:10:40 -07001923 if (put_user(*card->q931_read++, p))
1924 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (card->q931_read > card->q931_end)
1926 card->q931_read = card->q931_buf;
1927 }
1928 return count;
1929
1930}
1931
1932static void enable_dchannel_trace(capidrv_contr *card)
1933{
1934 u8 manufacturer[CAPI_MANUFACTURER_LEN];
1935 capi_version version;
1936 u16 contr = card->contrnr;
1937 u16 errcode;
1938 u16 avmversion[3];
1939
1940 errcode = capi20_get_manufacturer(contr, manufacturer);
1941 if (errcode != CAPI_NOERROR) {
1942 printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
1943 card->name, errcode);
1944 return;
1945 }
1946 if (strstr(manufacturer, "AVM") == 0) {
1947 printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
1948 card->name, manufacturer);
1949 return;
1950 }
1951 errcode = capi20_get_version(contr, &version);
1952 if (errcode != CAPI_NOERROR) {
1953 printk(KERN_ERR "%s: can't get version (0x%x)\n",
1954 card->name, errcode);
1955 return;
1956 }
1957 avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
1958 avmversion[1] = (version.majormanuversion << 4) & 0xf0;
1959 avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
1960 avmversion[2] |= version.minormanuversion & 0x0f;
1961
1962 if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
1963 printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
1964 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1965 card->msgid++,
1966 contr,
1967 0x214D5641, /* ManuID */
1968 0, /* Class */
1969 1, /* Function */
1970 (_cstruct)"\004\200\014\000\000");
1971 } else {
1972 printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
1973 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1974 card->msgid++,
1975 contr,
1976 0x214D5641, /* ManuID */
1977 0, /* Class */
1978 1, /* Function */
1979 (_cstruct)"\004\002\003\000\000");
1980 }
1981 send_message(card, &cmdcmsg);
1982}
1983
1984
1985static void send_listen(capidrv_contr *card)
1986{
1987 capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
1988 card->msgid++,
1989 card->contrnr, /* controller */
1990 1 << 6, /* Infomask */
1991 card->cipmask,
1992 card->cipmask2,
1993 NULL, NULL);
1994 send_message(card, &cmdcmsg);
1995 listen_change_state(card, EV_LISTEN_REQ);
1996}
1997
1998static void listentimerfunc(unsigned long x)
1999{
2000 capidrv_contr *card = (capidrv_contr *)x;
2001 if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
2002 printk(KERN_ERR "%s: controller dead ??\n", card->name);
2003 send_listen(card);
2004 mod_timer(&card->listentimer, jiffies + 60*HZ);
2005}
2006
2007
2008static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
2009{
2010 capidrv_contr *card;
2011 unsigned long flags;
2012 isdn_ctrl cmd;
2013 char id[20];
2014 int i;
2015
2016 sprintf(id, "capidrv-%d", contr);
2017 if (!try_module_get(THIS_MODULE)) {
2018 printk(KERN_WARNING "capidrv: (%s) Could not reserve module\n", id);
2019 return -1;
2020 }
Burman Yan41f96932006-12-08 02:39:35 -08002021 if (!(card = kzalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 printk(KERN_WARNING
2023 "capidrv: (%s) Could not allocate contr-struct.\n", id);
2024 return -1;
2025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 card->owner = THIS_MODULE;
2027 init_timer(&card->listentimer);
2028 strcpy(card->name, id);
2029 card->contrnr = contr;
2030 card->nbchan = profp->nbchannel;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002031 card->bchans = kmalloc(sizeof(capidrv_bchan) * card->nbchan, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!card->bchans) {
2033 printk(KERN_WARNING
2034 "capidrv: (%s) Could not allocate bchan-structs.\n", id);
2035 module_put(card->owner);
2036 kfree(card);
2037 return -1;
2038 }
2039 card->interface.channels = profp->nbchannel;
2040 card->interface.maxbufsize = 2048;
2041 card->interface.command = if_command;
2042 card->interface.writebuf_skb = if_sendbuf;
2043 card->interface.writecmd = NULL;
2044 card->interface.readstat = if_readstat;
2045 card->interface.features = ISDN_FEATURE_L2_HDLC |
2046 ISDN_FEATURE_L2_TRANS |
2047 ISDN_FEATURE_L3_TRANS |
2048 ISDN_FEATURE_P_UNKNOWN |
2049 ISDN_FEATURE_L2_X75I |
2050 ISDN_FEATURE_L2_X75UI |
2051 ISDN_FEATURE_L2_X75BUI;
2052 if (profp->support1 & (1<<2))
2053 card->interface.features |= ISDN_FEATURE_L2_V11096 |
2054 ISDN_FEATURE_L2_V11019 |
2055 ISDN_FEATURE_L2_V11038;
2056 if (profp->support1 & (1<<8))
2057 card->interface.features |= ISDN_FEATURE_L2_MODEM;
2058 card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
2059 strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
2060
2061
2062 card->q931_read = card->q931_buf;
2063 card->q931_write = card->q931_buf;
2064 card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
2065
2066 if (!register_isdn(&card->interface)) {
2067 printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
2068 kfree(card->bchans);
2069 module_put(card->owner);
2070 kfree(card);
2071 return -1;
2072 }
2073 card->myid = card->interface.channels;
2074 memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
2075 for (i = 0; i < card->nbchan; i++) {
2076 card->bchans[i].contr = card;
2077 }
2078
2079 spin_lock_irqsave(&global_lock, flags);
2080 card->next = global.contr_list;
2081 global.contr_list = card;
2082 global.ncontr++;
2083 spin_unlock_irqrestore(&global_lock, flags);
2084
2085 cmd.command = ISDN_STAT_RUN;
2086 cmd.driver = card->myid;
2087 card->interface.statcallb(&cmd);
2088
2089 card->cipmask = 0x1FFF03FF; /* any */
2090 card->cipmask2 = 0;
2091
2092 card->listentimer.data = (unsigned long)card;
2093 card->listentimer.function = listentimerfunc;
2094 send_listen(card);
2095 mod_timer(&card->listentimer, jiffies + 60*HZ);
2096
2097 printk(KERN_INFO "%s: now up (%d B channels)\n",
2098 card->name, card->nbchan);
2099
2100 enable_dchannel_trace(card);
2101
2102 return 0;
2103}
2104
2105static int capidrv_delcontr(u16 contr)
2106{
2107 capidrv_contr **pp, *card;
2108 unsigned long flags;
2109 isdn_ctrl cmd;
2110
2111 spin_lock_irqsave(&global_lock, flags);
2112 for (card = global.contr_list; card; card = card->next) {
2113 if (card->contrnr == contr)
2114 break;
2115 }
2116 if (!card) {
2117 spin_unlock_irqrestore(&global_lock, flags);
2118 printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
2119 return -1;
2120 }
2121 #warning FIXME: maybe a race condition the card should be removed here from global list /kkeil
2122 spin_unlock_irqrestore(&global_lock, flags);
2123
2124 del_timer(&card->listentimer);
2125
2126 if (debugmode)
2127 printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
2128 card->contrnr, card->myid);
2129
2130 cmd.command = ISDN_STAT_STOP;
2131 cmd.driver = card->myid;
2132 card->interface.statcallb(&cmd);
2133
2134 while (card->nbchan) {
2135
2136 cmd.command = ISDN_STAT_DISCH;
2137 cmd.driver = card->myid;
2138 cmd.arg = card->nbchan-1;
2139 cmd.parm.num[0] = 0;
2140 if (debugmode)
2141 printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
2142 card->contrnr, card->myid, cmd.arg);
2143 card->interface.statcallb(&cmd);
2144
2145 if (card->bchans[card->nbchan-1].nccip)
2146 free_ncci(card, card->bchans[card->nbchan-1].nccip);
2147 if (card->bchans[card->nbchan-1].plcip)
2148 free_plci(card, card->bchans[card->nbchan-1].plcip);
2149 if (card->plci_list)
2150 printk(KERN_ERR "capidrv: bug in free_plci()\n");
2151 card->nbchan--;
2152 }
2153 kfree(card->bchans);
2154 card->bchans = NULL;
2155
2156 if (debugmode)
2157 printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
2158 card->contrnr, card->myid);
2159
2160 cmd.command = ISDN_STAT_UNLOAD;
2161 cmd.driver = card->myid;
2162 card->interface.statcallb(&cmd);
2163
2164 if (debugmode)
2165 printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
2166 card->contrnr, card->myid);
2167
2168 spin_lock_irqsave(&global_lock, flags);
2169 for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
2170 if (*pp == card) {
2171 *pp = (*pp)->next;
2172 card->next = NULL;
2173 global.ncontr--;
2174 break;
2175 }
2176 }
2177 spin_unlock_irqrestore(&global_lock, flags);
2178
2179 module_put(card->owner);
2180 printk(KERN_INFO "%s: now down.\n", card->name);
2181 kfree(card);
2182 return 0;
2183}
2184
2185
2186static void lower_callback(unsigned int cmd, u32 contr, void *data)
2187{
2188
2189 switch (cmd) {
2190 case KCI_CONTRUP:
2191 printk(KERN_INFO "capidrv: controller %hu up\n", contr);
2192 (void) capidrv_addcontr(contr, (capi_profile *) data);
2193 break;
2194 case KCI_CONTRDOWN:
2195 printk(KERN_INFO "capidrv: controller %hu down\n", contr);
2196 (void) capidrv_delcontr(contr);
2197 break;
2198 }
2199}
2200
2201/*
2202 * /proc/capi/capidrv:
2203 * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
2204 */
2205static int proc_capidrv_read_proc(char *page, char **start, off_t off,
2206 int count, int *eof, void *data)
2207{
2208 int len = 0;
2209
2210 len += sprintf(page+len, "%lu %lu %lu %lu\n",
2211 global.ap.nrecvctlpkt,
2212 global.ap.nrecvdatapkt,
2213 global.ap.nsentctlpkt,
2214 global.ap.nsentdatapkt);
2215 if (off+count >= len)
2216 *eof = 1;
2217 if (len < off)
2218 return 0;
2219 *start = page + off;
2220 return ((count < len-off) ? count : len-off);
2221}
2222
2223static struct procfsentries {
2224 char *name;
2225 mode_t mode;
2226 int (*read_proc)(char *page, char **start, off_t off,
2227 int count, int *eof, void *data);
2228 struct proc_dir_entry *procent;
2229} procfsentries[] = {
2230 /* { "capi", S_IFDIR, 0 }, */
2231 { "capi/capidrv", 0 , proc_capidrv_read_proc },
2232};
2233
2234static void __init proc_init(void)
2235{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08002236 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 int i;
2238
2239 for (i=0; i < nelem; i++) {
2240 struct procfsentries *p = procfsentries + i;
2241 p->procent = create_proc_entry(p->name, p->mode, NULL);
2242 if (p->procent) p->procent->read_proc = p->read_proc;
2243 }
2244}
2245
2246static void __exit proc_exit(void)
2247{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08002248 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 int i;
2250
2251 for (i=nelem-1; i >= 0; i--) {
2252 struct procfsentries *p = procfsentries + i;
2253 if (p->procent) {
2254 remove_proc_entry(p->name, NULL);
2255 p->procent = NULL;
2256 }
2257 }
2258}
2259
2260static int __init capidrv_init(void)
2261{
2262 capi_profile profile;
2263 char rev[32];
2264 char *p;
2265 u32 ncontr, contr;
2266 u16 errcode;
2267
2268 if ((p = strchr(revision, ':')) != 0 && p[1]) {
2269 strncpy(rev, p + 2, sizeof(rev));
2270 rev[sizeof(rev)-1] = 0;
2271 if ((p = strchr(rev, '$')) != 0 && p > rev)
2272 *(p-1) = 0;
2273 } else
2274 strcpy(rev, "1.0");
2275
2276 global.ap.rparam.level3cnt = -2; /* number of bchannels twice */
2277 global.ap.rparam.datablkcnt = 16;
2278 global.ap.rparam.datablklen = 2048;
2279
2280 global.ap.recv_message = capidrv_recv_message;
2281 errcode = capi20_register(&global.ap);
2282 if (errcode) {
2283 return -EIO;
2284 }
2285
2286 capi20_set_callback(&global.ap, lower_callback);
2287
2288 errcode = capi20_get_profile(0, &profile);
2289 if (errcode != CAPI_NOERROR) {
2290 capi20_release(&global.ap);
2291 return -EIO;
2292 }
2293
2294 ncontr = profile.ncontroller;
2295 for (contr = 1; contr <= ncontr; contr++) {
2296 errcode = capi20_get_profile(contr, &profile);
2297 if (errcode != CAPI_NOERROR)
2298 continue;
2299 (void) capidrv_addcontr(contr, &profile);
2300 }
2301 proc_init();
2302
2303 printk(KERN_NOTICE "capidrv: Rev %s: loaded\n", rev);
2304 return 0;
2305}
2306
2307static void __exit capidrv_exit(void)
2308{
2309 char rev[10];
2310 char *p;
2311
2312 if ((p = strchr(revision, ':')) != 0) {
2313 strcpy(rev, p + 1);
2314 p = strchr(rev, '$');
2315 *p = 0;
2316 } else {
2317 strcpy(rev, " ??? ");
2318 }
2319
2320 capi20_release(&global.ap);
2321
2322 proc_exit();
2323
2324 printk(KERN_NOTICE "capidrv: Rev%s: unloaded\n", rev);
2325}
2326
2327module_init(capidrv_init);
2328module_exit(capidrv_exit);