blob: fd6d28f3fc36e9223e759a1c3aaffa8b2de84641 [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>
Alexey Dobriyan9a58a802010-01-14 03:10:54 -080027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/capi.h>
29#include <linux/kernelcapi.h>
30#include <linux/ctype.h>
31#include <linux/init.h>
32#include <linux/moduleparam.h>
33
34#include <linux/isdn/capiutil.h>
35#include <linux/isdn/capicmd.h>
36#include "capidrv.h"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int debugmode = 0;
39
40MODULE_DESCRIPTION("CAPI4Linux: Interface to ISDN4Linux");
41MODULE_AUTHOR("Carsten Paeth");
42MODULE_LICENSE("GPL");
Joe Perches475be4d2012-02-19 19:52:38 -080043module_param(debugmode, uint, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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;
Joe Perches475be4d2012-02-19 19:52:38 -080067 struct timer_list listentimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
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 {
Joe Perches475be4d2012-02-19 19:52:38 -0800108 struct ncci_datahandle_queue *next;
109 u16 datahandle;
110 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 } *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,
Joe Perches475be4d2012-02-19 19:52:38 -0800145 int send, int level2, u8 *data, u16 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
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;
Joe Perches475be4d2012-02-19 19:52:38 -0800161 case ISDN_PROTO_L2_V11096:
162 case ISDN_PROTO_L2_V11019:
163 case ISDN_PROTO_L2_V11038:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 2;
Joe Perches475be4d2012-02-19 19:52:38 -0800165 case ISDN_PROTO_L2_FAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 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:
Joe Perches475be4d2012-02-19 19:52:38 -0800182 case ISDN_PROTO_L2_V11096:
183 case ISDN_PROTO_L2_V11019:
184 case ISDN_PROTO_L2_V11038:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 case ISDN_PROTO_L2_MODEM:
186 return 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800187 case ISDN_PROTO_L2_FAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 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:
Joe Perches475be4d2012-02-19 19:52:38 -0800200 case ISDN_PROTO_L2_V11096:
201 case ISDN_PROTO_L2_V11019:
202 case ISDN_PROTO_L2_V11038:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 case ISDN_PROTO_L2_MODEM:
204 default:
205 return 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800206 case ISDN_PROTO_L2_FAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 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;
Joe Perches475be4d2012-02-19 19:52:38 -0800234 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241}
242
243static inline u16 si2cip(u8 si1, u8 si2)
244{
245 static const u8 cip[17][5] =
Joe Perches475be4d2012-02-19 19:52:38 -0800246 {
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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 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] =
Joe Perches475be4d2012-02-19 19:52:38 -0800277 {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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
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] =
Joe Perches475be4d2012-02-19 19:52:38 -0800290 {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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
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{
Joe Perches475be4d2012-02-19 19:52:38 -0800305 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 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
Joe Perches475be4d2012-02-19 19:52:38 -0800332static capidrv_plci *new_plci(capidrv_contr *card, int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
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
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700338 if (plcip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 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
Joe Perches475be4d2012-02-19 19:52:38 -0800352static capidrv_plci *find_plci_by_plci(capidrv_contr *card, u32 plci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
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
Joe Perches475be4d2012-02-19 19:52:38 -0800361static capidrv_plci *find_plci_by_msgid(capidrv_contr *card, u16 msgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
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
Joe Perches475be4d2012-02-19 19:52:38 -0800370static capidrv_plci *find_plci_by_ncci(capidrv_contr *card, u32 ncci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
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
Joe Perches475be4d2012-02-19 19:52:38 -0800379static void free_plci(capidrv_contr *card, capidrv_plci *plcip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
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
Joe Perches475be4d2012-02-19 19:52:38 -0800399static inline capidrv_ncci *new_ncci(capidrv_contr *card,
400 capidrv_plci *plcip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 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
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700407 if (nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 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
Joe Perches475be4d2012-02-19 19:52:38 -0800424static inline capidrv_ncci *find_ncci(capidrv_contr *card, u32 ncci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 capidrv_plci *plcip;
427 capidrv_ncci *p;
428
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700429 if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 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
Joe Perches475be4d2012-02-19 19:52:38 -0800438static inline capidrv_ncci *find_ncci_by_msgid(capidrv_contr *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 u32 ncci, u16 msgid)
440{
441 capidrv_plci *plcip;
442 capidrv_ncci *p;
443
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700444 if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 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
Joe Perches475be4d2012-02-19 19:52:38 -0800453static void free_ncci(capidrv_contr *card, struct capidrv_ncci *nccip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800468 u16 datahandle, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct ncci_datahandle_queue *n, **pp;
471
Zhang Yanfeic3f14cf2013-03-11 19:13:47 +0000472 n = kmalloc(sizeof(struct ncci_datahandle_queue), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!n) {
Joe Perches475be4d2012-02-19 19:52:38 -0800474 printk(KERN_ERR "capidrv: kmalloc ncci_datahandle failed\n");
475 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 n->next = NULL;
478 n->datahandle = datahandle;
479 n->len = len;
Joe Perches475be4d2012-02-19 19:52:38 -0800480 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *pp = n;
482 return 0;
483}
484
485static int capidrv_del_ack(struct capidrv_ncci *nccip, u16 datahandle)
486{
487 struct ncci_datahandle_queue **pp, *p;
488 int len;
489
490 for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) {
Joe Perches475be4d2012-02-19 19:52:38 -0800491 if ((*pp)->datahandle == datahandle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 p = *pp;
493 len = p->len;
494 *pp = (*pp)->next;
Joe Perches475be4d2012-02-19 19:52:38 -0800495 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return len;
497 }
498 }
499 return -1;
500}
501
502/* -------- convert and send capi message ---------------------------- */
503
Joe Perches475be4d2012-02-19 19:52:38 -0800504static void send_message(capidrv_contr *card, _cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct sk_buff *skb;
507 size_t len;
Jesper Juhlb1b2e7c2007-10-16 01:27:51 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 capi_cmsg2message(cmsg, cmsg->buf);
510 len = CAPIMSG_LEN(cmsg->buf);
511 skb = alloc_skb(len, GFP_ATOMIC);
Jesper Juhlb1b2e7c2007-10-16 01:27:51 -0700512 if (!skb) {
513 printk(KERN_ERR "capidrv::send_message: can't allocate mem\n");
514 return;
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 memcpy(skb_put(skb, len), cmsg->buf, len);
517 if (capi20_put_message(&global.ap, skb) != CAPI_NOERROR)
518 kfree_skb(skb);
519}
520
521/* -------- state machine -------------------------------------------- */
522
523struct listenstatechange {
524 int actstate;
525 int nextstate;
526 int event;
527};
528
529static struct listenstatechange listentable[] =
530{
Joe Perches475be4d2012-02-19 19:52:38 -0800531 {ST_LISTEN_NONE, ST_LISTEN_WAIT_CONF, EV_LISTEN_REQ},
532 {ST_LISTEN_ACTIVE, ST_LISTEN_ACTIVE_WAIT_CONF, EV_LISTEN_REQ},
533 {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_ERROR},
534 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_ERROR},
535 {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
536 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
537 {ST_LISTEN_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
538 {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
539 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540};
541
Joe Perches475be4d2012-02-19 19:52:38 -0800542static void listen_change_state(capidrv_contr *card, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct listenstatechange *p = listentable;
545 while (p->event) {
546 if (card->state == p->actstate && p->event == event) {
547 if (debugmode)
548 printk(KERN_DEBUG "capidrv-%d: listen_change_state %d -> %d\n",
549 card->contrnr, card->state, p->nextstate);
550 card->state = p->nextstate;
551 return;
552 }
553 p++;
554 }
555 printk(KERN_ERR "capidrv-%d: listen_change_state state=%d event=%d ????\n",
556 card->contrnr, card->state, event);
557
558}
559
560/* ------------------------------------------------------------------ */
561
Joe Perches475be4d2012-02-19 19:52:38 -0800562static void p0(capidrv_contr *card, capidrv_plci *plci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 isdn_ctrl cmd;
565
566 card->bchans[plci->chan].contr = NULL;
567 cmd.command = ISDN_STAT_DHUP;
568 cmd.driver = card->myid;
569 cmd.arg = plci->chan;
570 card->interface.statcallb(&cmd);
571 free_plci(card, plci);
572}
573
574/* ------------------------------------------------------------------ */
575
576struct plcistatechange {
577 int actstate;
578 int nextstate;
579 int event;
Joe Perches475be4d2012-02-19 19:52:38 -0800580 void (*changefunc)(capidrv_contr *card, capidrv_plci *plci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581};
582
583static struct plcistatechange plcitable[] =
584{
Joe Perches475be4d2012-02-19 19:52:38 -0800585 /* P-0 */
586 {ST_PLCI_NONE, ST_PLCI_OUTGOING, EV_PLCI_CONNECT_REQ, NULL},
587 {ST_PLCI_NONE, ST_PLCI_ALLOCATED, EV_PLCI_FACILITY_IND_UP, NULL},
588 {ST_PLCI_NONE, ST_PLCI_INCOMING, EV_PLCI_CONNECT_IND, NULL},
589 {ST_PLCI_NONE, ST_PLCI_RESUMEING, EV_PLCI_RESUME_REQ, NULL},
590 /* P-0.1 */
591 {ST_PLCI_OUTGOING, ST_PLCI_NONE, EV_PLCI_CONNECT_CONF_ERROR, p0},
592 {ST_PLCI_OUTGOING, ST_PLCI_ALLOCATED, EV_PLCI_CONNECT_CONF_OK, NULL},
593 /* P-1 */
594 {ST_PLCI_ALLOCATED, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
595 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
596 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
597 {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
598 /* P-ACT */
599 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
600 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
601 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
602 {ST_PLCI_ACTIVE, ST_PLCI_HELD, EV_PLCI_HOLD_IND, NULL},
603 {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_SUSPEND_IND, NULL},
604 /* P-2 */
605 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
606 {ST_PLCI_INCOMING, ST_PLCI_FACILITY_IND, EV_PLCI_FACILITY_IND_UP, NULL},
607 {ST_PLCI_INCOMING, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_RESP, NULL},
608 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
609 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
610 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
611 {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CD_IND, NULL},
612 /* P-3 */
613 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
614 {ST_PLCI_FACILITY_IND, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
615 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
616 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
617 {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
618 /* P-4 */
619 {ST_PLCI_ACCEPTING, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
620 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
621 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
622 {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
623 /* P-5 */
624 {ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
625 /* P-6 */
626 {ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0},
627 /* P-0.Res */
628 {ST_PLCI_RESUMEING, ST_PLCI_NONE, EV_PLCI_RESUME_CONF_ERROR, p0},
629 {ST_PLCI_RESUMEING, ST_PLCI_RESUME, EV_PLCI_RESUME_CONF_OK, NULL},
630 /* P-RES */
631 {ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, NULL},
632 /* P-HELD */
633 {ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, NULL},
634 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
Joe Perches475be4d2012-02-19 19:52:38 -0800637static void plci_change_state(capidrv_contr *card, capidrv_plci *plci, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct plcistatechange *p = plcitable;
640 while (p->event) {
641 if (plci->state == p->actstate && p->event == event) {
642 if (debugmode)
643 printk(KERN_DEBUG "capidrv-%d: plci_change_state:0x%x %d -> %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800644 card->contrnr, plci->plci, plci->state, p->nextstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 plci->state = p->nextstate;
646 if (p->changefunc)
647 p->changefunc(card, plci);
648 return;
649 }
650 p++;
651 }
652 printk(KERN_ERR "capidrv-%d: plci_change_state:0x%x state=%d event=%d ????\n",
653 card->contrnr, plci->plci, plci->state, event);
654}
655
656/* ------------------------------------------------------------------ */
657
658static _cmsg cmsg;
659
Joe Perches475be4d2012-02-19 19:52:38 -0800660static void n0(capidrv_contr *card, capidrv_ncci *ncci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 isdn_ctrl cmd;
663
664 capi_fill_DISCONNECT_REQ(&cmsg,
665 global.ap.applid,
666 card->msgid++,
667 ncci->plcip->plci,
668 NULL, /* BChannelinformation */
669 NULL, /* Keypadfacility */
670 NULL, /* Useruserdata */ /* $$$$ */
671 NULL /* Facilitydataarray */
Joe Perches475be4d2012-02-19 19:52:38 -0800672 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ);
Tilman Schmidte4847022009-10-06 12:18:10 +0000674 send_message(card, &cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 cmd.command = ISDN_STAT_BHUP;
677 cmd.driver = card->myid;
678 cmd.arg = ncci->chan;
679 card->interface.statcallb(&cmd);
680 free_ncci(card, ncci);
681}
682
683/* ------------------------------------------------------------------ */
684
685struct nccistatechange {
686 int actstate;
687 int nextstate;
688 int event;
Joe Perches475be4d2012-02-19 19:52:38 -0800689 void (*changefunc)(capidrv_contr *card, capidrv_ncci *ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690};
691
692static struct nccistatechange nccitable[] =
693{
Joe Perches475be4d2012-02-19 19:52:38 -0800694 /* N-0 */
695 {ST_NCCI_NONE, ST_NCCI_OUTGOING, EV_NCCI_CONNECT_B3_REQ, NULL},
696 {ST_NCCI_NONE, ST_NCCI_INCOMING, EV_NCCI_CONNECT_B3_IND, NULL},
697 /* N-0.1 */
698 {ST_NCCI_OUTGOING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_CONF_OK, NULL},
699 {ST_NCCI_OUTGOING, ST_NCCI_NONE, EV_NCCI_CONNECT_B3_CONF_ERROR, n0},
700 /* N-1 */
701 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_CONNECT_B3_REJECT, NULL},
702 {ST_NCCI_INCOMING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_RESP, NULL},
703 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
704 {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
705 /* N-2 */
706 {ST_NCCI_ALLOCATED, ST_NCCI_ACTIVE, EV_NCCI_CONNECT_B3_ACTIVE_IND, NULL},
707 {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
708 {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
709 /* N-ACT */
710 {ST_NCCI_ACTIVE, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
711 {ST_NCCI_ACTIVE, ST_NCCI_RESETING, EV_NCCI_RESET_B3_REQ, NULL},
712 {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
713 {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
714 /* N-3 */
715 {ST_NCCI_RESETING, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
716 {ST_NCCI_RESETING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
717 {ST_NCCI_RESETING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
718 /* N-4 */
719 {ST_NCCI_DISCONNECTING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
720 {ST_NCCI_DISCONNECTING, ST_NCCI_PREVIOUS, EV_NCCI_DISCONNECT_B3_CONF_ERROR, NULL},
721 /* N-5 */
722 {ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0},
723 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
Joe Perches475be4d2012-02-19 19:52:38 -0800726static void ncci_change_state(capidrv_contr *card, capidrv_ncci *ncci, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct nccistatechange *p = nccitable;
729 while (p->event) {
730 if (ncci->state == p->actstate && p->event == event) {
731 if (debugmode)
732 printk(KERN_DEBUG "capidrv-%d: ncci_change_state:0x%x %d -> %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800733 card->contrnr, ncci->ncci, ncci->state, p->nextstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (p->nextstate == ST_NCCI_PREVIOUS) {
735 ncci->state = ncci->oldstate;
736 ncci->oldstate = p->actstate;
737 } else {
738 ncci->oldstate = p->actstate;
739 ncci->state = p->nextstate;
740 }
741 if (p->changefunc)
742 p->changefunc(card, ncci);
743 return;
744 }
745 p++;
746 }
747 printk(KERN_ERR "capidrv-%d: ncci_change_state:0x%x state=%d event=%d ????\n",
748 card->contrnr, ncci->ncci, ncci->state, event);
749}
750
751/* ------------------------------------------------------------------- */
752
Joe Perches475be4d2012-02-19 19:52:38 -0800753static inline int new_bchan(capidrv_contr *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 int i;
756 for (i = 0; i < card->nbchan; i++) {
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700757 if (card->bchans[i].plcip == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 card->bchans[i].disconnecting = 0;
759 return i;
760 }
761 }
762 return -1;
763}
764
765/* ------------------------------------------------------------------- */
Paul Bolleca05e3a2014-06-01 23:47:24 +0200766static char *capi_info2str(u16 reason)
767{
Paul Bollea79f5d22014-06-01 23:47:24 +0200768#ifndef CONFIG_ISDN_CAPI_CAPIDRV_VERBOSE
Paul Bolleca05e3a2014-06-01 23:47:24 +0200769 return "..";
770#else
771 switch (reason) {
772
773/*-- informative values (corresponding message was processed) -----*/
774 case 0x0001:
775 return "NCPI not supported by current protocol, NCPI ignored";
776 case 0x0002:
777 return "Flags not supported by current protocol, flags ignored";
778 case 0x0003:
779 return "Alert already sent by another application";
780
781/*-- error information concerning CAPI_REGISTER -----*/
782 case 0x1001:
783 return "Too many applications";
784 case 0x1002:
785 return "Logical block size too small, must be at least 128 Bytes";
786 case 0x1003:
787 return "Buffer exceeds 64 kByte";
788 case 0x1004:
789 return "Message buffer size too small, must be at least 1024 Bytes";
790 case 0x1005:
791 return "Max. number of logical connections not supported";
792 case 0x1006:
793 return "Reserved";
794 case 0x1007:
795 return "The message could not be accepted because of an internal busy condition";
796 case 0x1008:
797 return "OS resource error (no memory ?)";
798 case 0x1009:
799 return "CAPI not installed";
800 case 0x100A:
801 return "Controller does not support external equipment";
802 case 0x100B:
803 return "Controller does only support external equipment";
804
805/*-- error information concerning message exchange functions -----*/
806 case 0x1101:
807 return "Illegal application number";
808 case 0x1102:
809 return "Illegal command or subcommand or message length less than 12 bytes";
810 case 0x1103:
811 return "The message could not be accepted because of a queue full condition !! The error code does not imply that CAPI cannot receive messages directed to another controller, PLCI or NCCI";
812 case 0x1104:
813 return "Queue is empty";
814 case 0x1105:
815 return "Queue overflow, a message was lost !! This indicates a configuration error. The only recovery from this error is to perform a CAPI_RELEASE";
816 case 0x1106:
817 return "Unknown notification parameter";
818 case 0x1107:
819 return "The Message could not be accepted because of an internal busy condition";
820 case 0x1108:
821 return "OS Resource error (no memory ?)";
822 case 0x1109:
823 return "CAPI not installed";
824 case 0x110A:
825 return "Controller does not support external equipment";
826 case 0x110B:
827 return "Controller does only support external equipment";
828
829/*-- error information concerning resource / coding problems -----*/
830 case 0x2001:
831 return "Message not supported in current state";
832 case 0x2002:
833 return "Illegal Controller / PLCI / NCCI";
834 case 0x2003:
835 return "Out of PLCI";
836 case 0x2004:
837 return "Out of NCCI";
838 case 0x2005:
839 return "Out of LISTEN";
840 case 0x2006:
841 return "Out of FAX resources (protocol T.30)";
842 case 0x2007:
843 return "Illegal message parameter coding";
844
845/*-- error information concerning requested services -----*/
846 case 0x3001:
847 return "B1 protocol not supported";
848 case 0x3002:
849 return "B2 protocol not supported";
850 case 0x3003:
851 return "B3 protocol not supported";
852 case 0x3004:
853 return "B1 protocol parameter not supported";
854 case 0x3005:
855 return "B2 protocol parameter not supported";
856 case 0x3006:
857 return "B3 protocol parameter not supported";
858 case 0x3007:
859 return "B protocol combination not supported";
860 case 0x3008:
861 return "NCPI not supported";
862 case 0x3009:
863 return "CIP Value unknown";
864 case 0x300A:
865 return "Flags not supported (reserved bits)";
866 case 0x300B:
867 return "Facility not supported";
868 case 0x300C:
869 return "Data length not supported by current protocol";
870 case 0x300D:
871 return "Reset procedure not supported by current protocol";
872
873/*-- informations about the clearing of a physical connection -----*/
874 case 0x3301:
875 return "Protocol error layer 1 (broken line or B-channel removed by signalling protocol)";
876 case 0x3302:
877 return "Protocol error layer 2";
878 case 0x3303:
879 return "Protocol error layer 3";
880 case 0x3304:
881 return "Another application got that call";
882/*-- T.30 specific reasons -----*/
883 case 0x3311:
884 return "Connecting not successful (remote station is no FAX G3 machine)";
885 case 0x3312:
886 return "Connecting not successful (training error)";
887 case 0x3313:
888 return "Disconnected before transfer (remote station does not support transfer mode, e.g. resolution)";
889 case 0x3314:
890 return "Disconnected during transfer (remote abort)";
891 case 0x3315:
892 return "Disconnected during transfer (remote procedure error, e.g. unsuccessful repetition of T.30 commands)";
893 case 0x3316:
894 return "Disconnected during transfer (local tx data underrun)";
895 case 0x3317:
896 return "Disconnected during transfer (local rx data overflow)";
897 case 0x3318:
898 return "Disconnected during transfer (local abort)";
899 case 0x3319:
900 return "Illegal parameter coding (e.g. SFF coding error)";
901
902/*-- disconnect causes from the network according to ETS 300 102-1/Q.931 -----*/
903 case 0x3481: return "Unallocated (unassigned) number";
904 case 0x3482: return "No route to specified transit network";
905 case 0x3483: return "No route to destination";
906 case 0x3486: return "Channel unacceptable";
907 case 0x3487:
908 return "Call awarded and being delivered in an established channel";
909 case 0x3490: return "Normal call clearing";
910 case 0x3491: return "User busy";
911 case 0x3492: return "No user responding";
912 case 0x3493: return "No answer from user (user alerted)";
913 case 0x3495: return "Call rejected";
914 case 0x3496: return "Number changed";
915 case 0x349A: return "Non-selected user clearing";
916 case 0x349B: return "Destination out of order";
917 case 0x349C: return "Invalid number format";
918 case 0x349D: return "Facility rejected";
919 case 0x349E: return "Response to STATUS ENQUIRY";
920 case 0x349F: return "Normal, unspecified";
921 case 0x34A2: return "No circuit / channel available";
922 case 0x34A6: return "Network out of order";
923 case 0x34A9: return "Temporary failure";
924 case 0x34AA: return "Switching equipment congestion";
925 case 0x34AB: return "Access information discarded";
926 case 0x34AC: return "Requested circuit / channel not available";
927 case 0x34AF: return "Resources unavailable, unspecified";
928 case 0x34B1: return "Quality of service unavailable";
929 case 0x34B2: return "Requested facility not subscribed";
930 case 0x34B9: return "Bearer capability not authorized";
931 case 0x34BA: return "Bearer capability not presently available";
932 case 0x34BF: return "Service or option not available, unspecified";
933 case 0x34C1: return "Bearer capability not implemented";
934 case 0x34C2: return "Channel type not implemented";
935 case 0x34C5: return "Requested facility not implemented";
936 case 0x34C6: return "Only restricted digital information bearer capability is available";
937 case 0x34CF: return "Service or option not implemented, unspecified";
938 case 0x34D1: return "Invalid call reference value";
939 case 0x34D2: return "Identified channel does not exist";
940 case 0x34D3: return "A suspended call exists, but this call identity does not";
941 case 0x34D4: return "Call identity in use";
942 case 0x34D5: return "No call suspended";
943 case 0x34D6: return "Call having the requested call identity has been cleared";
944 case 0x34D8: return "Incompatible destination";
945 case 0x34DB: return "Invalid transit network selection";
946 case 0x34DF: return "Invalid message, unspecified";
947 case 0x34E0: return "Mandatory information element is missing";
948 case 0x34E1: return "Message type non-existent or not implemented";
949 case 0x34E2: return "Message not compatible with call state or message type non-existent or not implemented";
950 case 0x34E3: return "Information element non-existent or not implemented";
951 case 0x34E4: return "Invalid information element contents";
952 case 0x34E5: return "Message not compatible with call state";
953 case 0x34E6: return "Recovery on timer expiry";
954 case 0x34EF: return "Protocol error, unspecified";
955 case 0x34FF: return "Interworking, unspecified";
956
957 default: return "No additional information";
958 }
959#endif
960}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Joe Perches475be4d2012-02-19 19:52:38 -0800962static void handle_controller(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
965
966 if (!card) {
967 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
968 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
969 cmsg->adr.adrController & 0x7f);
970 return;
971 }
972 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
973
974 case CAPI_LISTEN_CONF: /* Controller */
975 if (debugmode)
976 printk(KERN_DEBUG "capidrv-%d: listenconf Info=0x%4x (%s) cipmask=0x%x\n",
977 card->contrnr, cmsg->Info, capi_info2str(cmsg->Info), card->cipmask);
978 if (cmsg->Info) {
979 listen_change_state(card, EV_LISTEN_CONF_ERROR);
980 } else if (card->cipmask == 0) {
981 listen_change_state(card, EV_LISTEN_CONF_EMPTY);
982 } else {
983 listen_change_state(card, EV_LISTEN_CONF_OK);
984 }
985 break;
986
987 case CAPI_MANUFACTURER_IND: /* Controller */
Joe Perches475be4d2012-02-19 19:52:38 -0800988 if (cmsg->ManuID == 0x214D5641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 && cmsg->Class == 0
990 && cmsg->Function == 1) {
Joe Perches475be4d2012-02-19 19:52:38 -0800991 u8 *data = cmsg->ManuData + 3;
992 u16 len = cmsg->ManuData[0];
993 u16 layer;
994 int direction;
995 if (len == 255) {
996 len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
997 data += 2;
998 }
999 len -= 2;
1000 layer = ((*(data - 1)) << 8) | *(data - 2);
1001 if (layer & 0x300)
1002 direction = (layer & 0x200) ? 0 : 1;
1003 else direction = (layer & 0x800) ? 0 : 1;
1004 if (layer & 0x0C00) {
1005 if ((layer & 0xff) == 0x80) {
1006 handle_dtrace_data(card, direction, 1, data, len);
1007 break;
1008 }
1009 } else if ((layer & 0xff) < 0x80) {
1010 handle_dtrace_data(card, direction, 0, data, len);
1011 break;
1012 }
1013 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
1014 card->contrnr,
1015 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1016 cmsg->adr.adrController, layer);
1017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019 goto ignored;
1020 case CAPI_MANUFACTURER_CONF: /* Controller */
1021 if (cmsg->ManuID == 0x214D5641) {
Joe Perches475be4d2012-02-19 19:52:38 -08001022 char *s = NULL;
1023 switch (cmsg->Class) {
1024 case 0: break;
1025 case 1: s = "unknown class"; break;
1026 case 2: s = "unknown function"; break;
1027 default: s = "unknown error"; break;
1028 }
1029 if (s)
1030 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
1031 card->contrnr,
1032 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1033 cmsg->adr.adrController,
1034 cmsg->Function, s);
1035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 goto ignored;
1038 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1039 goto ignored;
1040 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1041 goto ignored;
1042 case CAPI_INFO_IND: /* Controller/plci */
1043 goto ignored;
1044 case CAPI_INFO_CONF: /* Controller/plci */
1045 goto ignored;
1046
1047 default:
1048 printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
1049 card->contrnr,
1050 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1051 cmsg->adr.adrController);
1052 }
1053 return;
1054
Joe Perches475be4d2012-02-19 19:52:38 -08001055ignored:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
1057 card->contrnr,
1058 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1059 cmsg->adr.adrController);
1060}
1061
Joe Perches475be4d2012-02-19 19:52:38 -08001062static void handle_incoming_call(capidrv_contr *card, _cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 capidrv_plci *plcip;
1065 capidrv_bchan *bchan;
1066 isdn_ctrl cmd;
1067 int chan;
1068
1069 if ((chan = new_bchan(card)) == -1) {
1070 printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
1071 return;
1072 }
1073 bchan = &card->bchans[chan];
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001074 if ((plcip = new_plci(card, chan)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 printk(KERN_ERR "capidrv-%d: incoming call: no memory, sorry.\n", card->contrnr);
1076 return;
1077 }
1078 bchan->incoming = 1;
1079 plcip->plci = cmsg->adr.adrPLCI;
1080 plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
1081
1082 cmd.command = ISDN_STAT_ICALL;
1083 cmd.driver = card->myid;
1084 cmd.arg = chan;
1085 memset(&cmd.parm.setup, 0, sizeof(cmd.parm.setup));
1086 strncpy(cmd.parm.setup.phone,
Joe Perches475be4d2012-02-19 19:52:38 -08001087 cmsg->CallingPartyNumber + 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 cmsg->CallingPartyNumber[0] - 2);
1089 strncpy(cmd.parm.setup.eazmsn,
Joe Perches475be4d2012-02-19 19:52:38 -08001090 cmsg->CalledPartyNumber + 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 cmsg->CalledPartyNumber[0] - 1);
1092 cmd.parm.setup.si1 = cip2si1(cmsg->CIPValue);
1093 cmd.parm.setup.si2 = cip2si2(cmsg->CIPValue);
1094 cmd.parm.setup.plan = cmsg->CallingPartyNumber[1];
1095 cmd.parm.setup.screen = cmsg->CallingPartyNumber[2];
1096
Joe Perches475be4d2012-02-19 19:52:38 -08001097 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n",
1098 card->contrnr,
1099 cmd.parm.setup.phone,
1100 cmd.parm.setup.si1,
1101 cmd.parm.setup.si2,
1102 cmd.parm.setup.eazmsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (cmd.parm.setup.si1 == 1 && cmd.parm.setup.si2 != 0) {
Joe Perches475be4d2012-02-19 19:52:38 -08001105 printk(KERN_INFO "capidrv-%d: patching si2=%d to 0 for VBOX\n",
1106 card->contrnr,
1107 cmd.parm.setup.si2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 cmd.parm.setup.si2 = 0;
1109 }
1110
1111 switch (card->interface.statcallb(&cmd)) {
1112 case 0:
1113 case 3:
1114 /* No device matching this call.
1115 * and isdn_common.c has send a HANGUP command
1116 * which is ignored in state ST_PLCI_INCOMING,
1117 * so we send RESP to ignore the call
1118 */
1119 capi_cmsg_answer(cmsg);
1120 cmsg->Reject = 1; /* ignore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
Tilman Schmidte4847022009-10-06 12:18:10 +00001122 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s ignored\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001124 card->contrnr,
1125 cmd.parm.setup.phone,
1126 cmd.parm.setup.si1,
1127 cmd.parm.setup.si2,
1128 cmd.parm.setup.eazmsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130 case 1:
1131 /* At least one device matching this call (RING on ttyI)
1132 * HL-driver may send ALERTING on the D-channel in this
1133 * case.
1134 * really means: RING on ttyI or a net interface
1135 * accepted this call already.
1136 *
1137 * If the call was accepted, state has already changed,
1138 * and CONNECT_RESP already sent.
1139 */
1140 if (plcip->state == ST_PLCI_INCOMING) {
1141 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001142 card->contrnr,
1143 cmd.parm.setup.phone,
1144 cmd.parm.setup.si1,
1145 cmd.parm.setup.si2,
1146 cmd.parm.setup.eazmsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 capi_fill_ALERT_REQ(cmsg,
1148 global.ap.applid,
1149 card->msgid++,
1150 plcip->plci, /* adr */
1151 NULL,/* BChannelinformation */
1152 NULL,/* Keypadfacility */
1153 NULL,/* Useruserdata */
1154 NULL /* Facilitydataarray */
Joe Perches475be4d2012-02-19 19:52:38 -08001155 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 plcip->msgid = cmsg->Messagenumber;
1157 send_message(card, cmsg);
1158 } else {
1159 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001160 card->contrnr,
1161 cmd.parm.setup.phone,
1162 cmd.parm.setup.si1,
1163 cmd.parm.setup.si2,
1164 cmd.parm.setup.eazmsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 break;
1167
1168 case 2: /* Call will be rejected. */
1169 capi_cmsg_answer(cmsg);
1170 cmsg->Reject = 2; /* reject call, normal call clearing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
Tilman Schmidte4847022009-10-06 12:18:10 +00001172 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174
1175 default:
1176 /* An error happened. (Invalid parameters for example.) */
1177 capi_cmsg_answer(cmsg);
1178 cmsg->Reject = 8; /* reject call,
1179 destination out of order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
Tilman Schmidte4847022009-10-06 12:18:10 +00001181 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183 }
1184 return;
1185}
1186
Joe Perches475be4d2012-02-19 19:52:38 -08001187static void handle_plci(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1190 capidrv_plci *plcip;
1191 isdn_ctrl cmd;
Karsten Keil17f0cd22007-02-28 20:13:50 -08001192 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 if (!card) {
1195 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1196 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1197 cmsg->adr.adrController & 0x7f);
1198 return;
1199 }
1200 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1201
1202 case CAPI_DISCONNECT_IND: /* plci */
1203 if (cmsg->Reason) {
1204 printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001205 card->contrnr,
1206 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
1208 }
1209 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
1210 capi_cmsg_answer(cmsg);
1211 send_message(card, cmsg);
1212 goto notfound;
1213 }
1214 card->bchans[plcip->chan].disconnecting = 1;
1215 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
1216 capi_cmsg_answer(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
Tilman Schmidte4847022009-10-06 12:18:10 +00001218 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
1220
1221 case CAPI_DISCONNECT_CONF: /* plci */
1222 if (cmsg->Info) {
1223 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001224 card->contrnr,
1225 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1226 cmsg->Info, capi_info2str(cmsg->Info),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 cmsg->adr.adrPLCI);
1228 }
1229 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1230 goto notfound;
1231
1232 card->bchans[plcip->chan].disconnecting = 1;
1233 break;
1234
1235 case CAPI_ALERT_CONF: /* plci */
1236 if (cmsg->Info) {
1237 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001238 card->contrnr,
1239 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1240 cmsg->Info, capi_info2str(cmsg->Info),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 cmsg->adr.adrPLCI);
1242 }
1243 break;
1244
1245 case CAPI_CONNECT_IND: /* plci */
1246 handle_incoming_call(card, cmsg);
1247 break;
1248
1249 case CAPI_CONNECT_CONF: /* plci */
1250 if (cmsg->Info) {
1251 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001252 card->contrnr,
1253 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1254 cmsg->Info, capi_info2str(cmsg->Info),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 cmsg->adr.adrPLCI);
1256 }
1257 if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
1258 goto notfound;
1259
1260 plcip->plci = cmsg->adr.adrPLCI;
1261 if (cmsg->Info) {
1262 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
1263 } else {
1264 plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
1265 }
1266 break;
1267
1268 case CAPI_CONNECT_ACTIVE_IND: /* plci */
1269
1270 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1271 goto notfound;
1272
1273 if (card->bchans[plcip->chan].incoming) {
1274 capi_cmsg_answer(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
Tilman Schmidte4847022009-10-06 12:18:10 +00001276 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 } else {
1278 capidrv_ncci *nccip;
1279 capi_cmsg_answer(cmsg);
1280 send_message(card, cmsg);
1281
1282 nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
1283
1284 if (!nccip) {
1285 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1286 break; /* $$$$ */
1287 }
1288 capi_fill_CONNECT_B3_REQ(cmsg,
1289 global.ap.applid,
1290 card->msgid++,
1291 plcip->plci, /* adr */
1292 NULL /* NCPI */
Joe Perches475be4d2012-02-19 19:52:38 -08001293 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 nccip->msgid = cmsg->Messagenumber;
Tilman Schmidte4847022009-10-06 12:18:10 +00001295 plci_change_state(card, plcip,
1296 EV_PLCI_CONNECT_ACTIVE_IND);
1297 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 send_message(card, cmsg);
1299 cmd.command = ISDN_STAT_DCONN;
1300 cmd.driver = card->myid;
1301 cmd.arg = plcip->chan;
1302 card->interface.statcallb(&cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 break;
1305
1306 case CAPI_INFO_IND: /* Controller/plci */
1307
1308 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1309 goto notfound;
1310
1311 if (cmsg->InfoNumber == 0x4000) {
1312 if (cmsg->InfoElement[0] == 4) {
1313 cmd.command = ISDN_STAT_CINF;
1314 cmd.driver = card->myid;
1315 cmd.arg = plcip->chan;
1316 sprintf(cmd.parm.num, "%lu",
1317 (unsigned long)
1318 ((u32) cmsg->InfoElement[1]
Joe Perches475be4d2012-02-19 19:52:38 -08001319 | ((u32) (cmsg->InfoElement[2]) << 8)
1320 | ((u32) (cmsg->InfoElement[3]) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 | ((u32) (cmsg->InfoElement[4]) << 24)));
1322 card->interface.statcallb(&cmd);
1323 break;
1324 }
1325 }
Karsten Keil17f0cd22007-02-28 20:13:50 -08001326 cdb = capi_cmsg2str(cmsg);
1327 if (cdb) {
1328 printk(KERN_WARNING "capidrv-%d: %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001329 card->contrnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001330 cdebbuf_free(cdb);
1331 } else
1332 printk(KERN_WARNING "capidrv-%d: CAPI_INFO_IND InfoNumber %x not handled\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001333 card->contrnr, cmsg->InfoNumber);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336
1337 case CAPI_CONNECT_ACTIVE_CONF: /* plci */
1338 goto ignored;
1339 case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */
1340 goto ignored;
1341 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1342 goto ignored;
1343 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1344 goto ignored;
1345
1346 case CAPI_INFO_CONF: /* Controller/plci */
1347 goto ignored;
1348
1349 default:
1350 printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
1351 card->contrnr,
1352 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1353 cmsg->adr.adrPLCI);
1354 }
1355 return;
Joe Perches475be4d2012-02-19 19:52:38 -08001356ignored:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
1358 card->contrnr,
1359 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1360 cmsg->adr.adrPLCI);
1361 return;
Joe Perches475be4d2012-02-19 19:52:38 -08001362notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
1364 card->contrnr,
1365 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1366 cmsg->adr.adrPLCI);
1367 return;
1368}
1369
Joe Perches475be4d2012-02-19 19:52:38 -08001370static void handle_ncci(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1373 capidrv_plci *plcip;
1374 capidrv_ncci *nccip;
1375 isdn_ctrl cmd;
1376 int len;
1377
1378 if (!card) {
1379 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1380 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1381 cmsg->adr.adrController & 0x7f);
1382 return;
1383 }
1384 switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1385
1386 case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */
1387 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1388 goto notfound;
1389
1390 capi_cmsg_answer(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
Tilman Schmidte4847022009-10-06 12:18:10 +00001392 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 cmd.command = ISDN_STAT_BCONN;
1395 cmd.driver = card->myid;
1396 cmd.arg = nccip->chan;
1397 card->interface.statcallb(&cmd);
1398
1399 printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
1400 card->contrnr, nccip->chan, nccip->ncci);
1401 break;
1402
1403 case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */
1404 goto ignored;
1405
1406 case CAPI_CONNECT_B3_IND: /* ncci */
1407
1408 plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
1409 if (plcip) {
1410 nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
1411 if (nccip) {
1412 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
1413 capi_fill_CONNECT_B3_RESP(cmsg,
1414 global.ap.applid,
1415 card->msgid++,
1416 nccip->ncci, /* adr */
1417 0, /* Reject */
1418 NULL /* NCPI */
Joe Perches475be4d2012-02-19 19:52:38 -08001419 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
Tilman Schmidte4847022009-10-06 12:18:10 +00001421 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 }
1424 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1425 } else {
1426 printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001427 card->contrnr,
1428 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 cmsg->adr.adrNCCI);
1430 }
1431 capi_fill_CONNECT_B3_RESP(cmsg,
1432 global.ap.applid,
1433 card->msgid++,
1434 cmsg->adr.adrNCCI,
1435 2, /* Reject */
1436 NULL /* NCPI */
Joe Perches475be4d2012-02-19 19:52:38 -08001437 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 send_message(card, cmsg);
1439 break;
1440
1441 case CAPI_CONNECT_B3_CONF: /* ncci */
1442
1443 if (!(nccip = find_ncci_by_msgid(card,
1444 cmsg->adr.adrNCCI,
1445 cmsg->Messagenumber)))
1446 goto notfound;
1447
1448 nccip->ncci = cmsg->adr.adrNCCI;
1449 if (cmsg->Info) {
1450 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001451 card->contrnr,
1452 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1453 cmsg->Info, capi_info2str(cmsg->Info),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 cmsg->adr.adrNCCI);
1455 }
1456
1457 if (cmsg->Info)
1458 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
1459 else
1460 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
1461 break;
1462
1463 case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */
1464 capi_cmsg_answer(cmsg);
1465 send_message(card, cmsg);
1466 break;
1467
1468 case CAPI_DATA_B3_IND: /* ncci */
1469 /* handled in handle_data() */
1470 goto ignored;
1471
1472 case CAPI_DATA_B3_CONF: /* ncci */
1473 if (cmsg->Info) {
1474 printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001475 cmsg->Info, capi_info2str(cmsg->Info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1478 goto notfound;
1479
1480 len = capidrv_del_ack(nccip, cmsg->DataHandle);
1481 if (len < 0)
1482 break;
Joe Perches475be4d2012-02-19 19:52:38 -08001483 cmd.command = ISDN_STAT_BSENT;
1484 cmd.driver = card->myid;
1485 cmd.arg = nccip->chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 cmd.parm.length = len;
Joe Perches475be4d2012-02-19 19:52:38 -08001487 card->interface.statcallb(&cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
1489
1490 case CAPI_DISCONNECT_B3_IND: /* ncci */
1491 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1492 goto notfound;
1493
1494 card->bchans[nccip->chan].disconnecting = 1;
1495 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
1496 capi_cmsg_answer(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
Tilman Schmidte4847022009-10-06 12:18:10 +00001498 send_message(card, cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 break;
1500
1501 case CAPI_DISCONNECT_B3_CONF: /* ncci */
1502 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1503 goto notfound;
1504 if (cmsg->Info) {
1505 printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001506 card->contrnr,
1507 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1508 cmsg->Info, capi_info2str(cmsg->Info),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 cmsg->adr.adrNCCI);
1510 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
1511 }
1512 break;
1513
1514 case CAPI_RESET_B3_IND: /* ncci */
1515 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1516 goto notfound;
1517 ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
1518 capi_cmsg_answer(cmsg);
1519 send_message(card, cmsg);
1520 break;
1521
1522 case CAPI_RESET_B3_CONF: /* ncci */
1523 goto ignored; /* $$$$ */
1524
1525 case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1526 goto ignored;
1527 case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
1528 goto ignored;
1529
1530 default:
1531 printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
1532 card->contrnr,
1533 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1534 cmsg->adr.adrNCCI);
1535 }
1536 return;
Joe Perches475be4d2012-02-19 19:52:38 -08001537ignored:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
1539 card->contrnr,
1540 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1541 cmsg->adr.adrNCCI);
1542 return;
Joe Perches475be4d2012-02-19 19:52:38 -08001543notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1545 card->contrnr,
1546 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1547 cmsg->adr.adrNCCI);
1548}
1549
1550
Joe Perches475be4d2012-02-19 19:52:38 -08001551static void handle_data(_cmsg *cmsg, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1554 capidrv_ncci *nccip;
1555
1556 if (!card) {
1557 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1558 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1559 cmsg->adr.adrController & 0x7f);
1560 kfree_skb(skb);
1561 return;
1562 }
1563 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
1564 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1565 card->contrnr,
1566 capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1567 cmsg->adr.adrNCCI);
1568 kfree_skb(skb);
1569 return;
1570 }
1571 (void) skb_pull(skb, CAPIMSG_LEN(skb->data));
1572 card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
1573 capi_cmsg_answer(cmsg);
1574 send_message(card, cmsg);
1575}
1576
1577static _cmsg s_cmsg;
1578
1579static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
1580{
1581 capi_message2cmsg(&s_cmsg, skb->data);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001582 if (debugmode > 3) {
1583 _cdebbuf *cdb = capi_cmsg2str(&s_cmsg);
1584
1585 if (cdb) {
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001586 printk(KERN_DEBUG "%s: applid=%d %s\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -08001587 ap->applid, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001588 cdebbuf_free(cdb);
1589 } else
1590 printk(KERN_DEBUG "%s: applid=%d %s not traced\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001591 __func__, ap->applid,
1592 capi_cmd2str(s_cmsg.Command, s_cmsg.Subcommand));
Karsten Keil17f0cd22007-02-28 20:13:50 -08001593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (s_cmsg.Command == CAPI_DATA_B3
1595 && s_cmsg.Subcommand == CAPI_IND) {
1596 handle_data(&s_cmsg, skb);
1597 return;
1598 }
1599 if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
1600 handle_controller(&s_cmsg);
1601 else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
1602 handle_plci(&s_cmsg);
1603 else
1604 handle_ncci(&s_cmsg);
1605 /*
1606 * data of skb used in s_cmsg,
1607 * free data when s_cmsg is not used again
1608 * thanks to Lars Heete <hel@admin.de>
1609 */
1610 kfree_skb(skb);
1611}
1612
1613/* ------------------------------------------------------------------- */
1614
Joe Perches475be4d2012-02-19 19:52:38 -08001615#define PUTBYTE_TO_STATUS(card, byte) \
1616 do { \
1617 *(card)->q931_write++ = (byte); \
1618 if ((card)->q931_write > (card)->q931_end) \
1619 (card)->q931_write = (card)->q931_buf; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 } while (0)
1621
1622static void handle_dtrace_data(capidrv_contr *card,
Joe Perches475be4d2012-02-19 19:52:38 -08001623 int send, int level2, u8 *data, u16 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Joe Perches475be4d2012-02-19 19:52:38 -08001625 u8 *p, *end;
1626 isdn_ctrl cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Joe Perches475be4d2012-02-19 19:52:38 -08001628 if (!len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001630 card->contrnr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return;
1632 }
1633
1634 if (level2) {
1635 PUTBYTE_TO_STATUS(card, 'D');
1636 PUTBYTE_TO_STATUS(card, '2');
Joe Perches475be4d2012-02-19 19:52:38 -08001637 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1638 PUTBYTE_TO_STATUS(card, ':');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 } else {
Joe Perches475be4d2012-02-19 19:52:38 -08001640 PUTBYTE_TO_STATUS(card, 'D');
1641 PUTBYTE_TO_STATUS(card, '3');
1642 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1643 PUTBYTE_TO_STATUS(card, ':');
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Joe Perches475be4d2012-02-19 19:52:38 -08001646 for (p = data, end = data + len; p < end; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 PUTBYTE_TO_STATUS(card, ' ');
Andy Shevchenko735c65c2010-07-15 02:37:18 +00001648 PUTBYTE_TO_STATUS(card, hex_asc_hi(*p));
1649 PUTBYTE_TO_STATUS(card, hex_asc_lo(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 PUTBYTE_TO_STATUS(card, '\n');
1652
1653 cmd.command = ISDN_STAT_STAVAIL;
1654 cmd.driver = card->myid;
Joe Perches475be4d2012-02-19 19:52:38 -08001655 cmd.arg = len * 3 + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 card->interface.statcallb(&cmd);
1657}
1658
1659/* ------------------------------------------------------------------- */
1660
1661static _cmsg cmdcmsg;
1662
Joe Perches475be4d2012-02-19 19:52:38 -08001663static int capidrv_ioctl(isdn_ctrl *c, capidrv_contr *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 switch (c->arg) {
1666 case 1:
1667 debugmode = (int)(*((unsigned int *)c->parm.num));
1668 printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001669 card->contrnr, debugmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
1671 default:
1672 printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001673 card->contrnr, c->arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return -EINVAL;
1675 }
1676 return -EINVAL;
1677}
1678
1679/*
1680 * Handle leased lines (CAPI-Bundling)
1681 */
1682
1683struct internal_bchannelinfo {
Joe Perches475be4d2012-02-19 19:52:38 -08001684 unsigned short channelalloc;
1685 unsigned short operation;
1686 unsigned char cmask[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687};
1688
1689static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
1690{
1691 unsigned long bmask = 0;
1692 int active = !0;
1693 char *s;
1694 int i;
1695
1696 if (strncmp(teln, "FV:", 3) != 0)
1697 return 1;
1698 s = teln + 3;
1699 while (*s && *s == ' ') s++;
1700 if (!*s) return -2;
1701 if (*s == 'p' || *s == 'P') {
1702 active = 0;
1703 s++;
1704 }
1705 if (*s == 'a' || *s == 'A') {
1706 active = !0;
1707 s++;
1708 }
1709 while (*s) {
1710 int digit1 = 0;
1711 int digit2 = 0;
Andy Shevchenkoa7a4f1c2010-09-07 05:14:30 +00001712 char *endp;
1713
1714 digit1 = simple_strtoul(s, &endp, 10);
1715 if (s == endp)
1716 return -3;
1717 s = endp;
1718
Roel Kluinfecc7032009-01-04 16:22:04 -08001719 if (digit1 <= 0 || digit1 > 30) return -4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (*s == 0 || *s == ',' || *s == ' ') {
1721 bmask |= (1 << digit1);
1722 digit1 = 0;
1723 if (*s) s++;
1724 continue;
1725 }
1726 if (*s != '-') return -5;
1727 s++;
Andy Shevchenkoa7a4f1c2010-09-07 05:14:30 +00001728
1729 digit2 = simple_strtoul(s, &endp, 10);
1730 if (s == endp)
1731 return -3;
1732 s = endp;
1733
Roel Kluinfecc7032009-01-04 16:22:04 -08001734 if (digit2 <= 0 || digit2 > 30) return -4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (*s == 0 || *s == ',' || *s == ' ') {
1736 if (digit1 > digit2)
Joe Perches475be4d2012-02-19 19:52:38 -08001737 for (i = digit2; i <= digit1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 bmask |= (1 << i);
Joe Perches475be4d2012-02-19 19:52:38 -08001739 else
1740 for (i = digit1; i <= digit2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 bmask |= (1 << i);
1742 digit1 = digit2 = 0;
1743 if (*s) s++;
1744 continue;
1745 }
1746 return -6;
1747 }
1748 if (activep) *activep = active;
1749 if (bmaskp) *bmaskp = bmask;
1750 return 0;
1751}
1752
Joe Perches475be4d2012-02-19 19:52:38 -08001753static int FVteln2capi20(char *teln, u8 AdditionalInfo[1 + 2 + 2 + 31])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755 unsigned long bmask;
1756 int active;
1757 int rc, i;
Joe Perches475be4d2012-02-19 19:52:38 -08001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 rc = decodeFVteln(teln, &bmask, &active);
1760 if (rc) return rc;
1761 /* Length */
Joe Perches475be4d2012-02-19 19:52:38 -08001762 AdditionalInfo[0] = 2 + 2 + 31;
1763 /* Channel: 3 => use channel allocation */
1764 AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /* Operation: 0 => DTE mode, 1 => DCE mode */
Joe Perches475be4d2012-02-19 19:52:38 -08001766 if (active) {
1767 AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
1768 } else {
1769 AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 /* Channel mask array */
1772 AdditionalInfo[5] = 0; /* no D-Channel */
Joe Perches475be4d2012-02-19 19:52:38 -08001773 for (i = 1; i <= 30; i++)
1774 AdditionalInfo[5 + i] = (bmask & (1 << i)) ? 0xff : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return 0;
1776}
1777
Joe Perches475be4d2012-02-19 19:52:38 -08001778static int capidrv_command(isdn_ctrl *c, capidrv_contr *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 isdn_ctrl cmd;
1781 struct capidrv_bchan *bchan;
1782 struct capidrv_plci *plcip;
Joe Perches475be4d2012-02-19 19:52:38 -08001783 u8 AdditionalInfo[1 + 2 + 2 + 31];
1784 int rc, isleasedline = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (c->command == ISDN_CMD_IOCTL)
1787 return capidrv_ioctl(c, card);
1788
1789 switch (c->command) {
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +00001790 case ISDN_CMD_DIAL: {
Joe Perches475be4d2012-02-19 19:52:38 -08001791 u8 calling[ISDN_MSNLEN + 3];
1792 u8 called[ISDN_MSNLEN + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Joe Perches475be4d2012-02-19 19:52:38 -08001794 if (debugmode)
1795 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
1796 card->contrnr,
1797 c->arg,
1798 c->parm.setup.phone,
1799 c->parm.setup.si1,
1800 c->parm.setup.si2,
1801 c->parm.setup.eazmsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Joe Perches475be4d2012-02-19 19:52:38 -08001803 bchan = &card->bchans[c->arg % card->nbchan];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Joe Perches475be4d2012-02-19 19:52:38 -08001805 if (bchan->plcip) {
1806 printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
1807 card->contrnr,
1808 c->arg,
1809 c->parm.setup.phone,
1810 c->parm.setup.si1,
1811 c->parm.setup.si2,
1812 c->parm.setup.eazmsn,
1813 bchan->plcip->plci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
1815 }
Joe Perches475be4d2012-02-19 19:52:38 -08001816 bchan->si1 = c->parm.setup.si1;
1817 bchan->si2 = c->parm.setup.si2;
1818
1819 strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
1820 strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
1821 rc = FVteln2capi20(bchan->num, AdditionalInfo);
1822 isleasedline = (rc == 0);
1823 if (rc < 0)
1824 printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
1825
1826 if (isleasedline) {
1827 calling[0] = 0;
1828 called[0] = 0;
1829 if (debugmode)
1830 printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
1831 } else {
1832 calling[0] = strlen(bchan->mynum) + 2;
1833 calling[1] = 0;
1834 calling[2] = 0x80;
1835 strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
1836 called[0] = strlen(bchan->num) + 1;
1837 called[1] = 0x80;
1838 strncpy(called + 2, bchan->num, ISDN_MSNLEN);
1839 }
1840
1841 capi_fill_CONNECT_REQ(&cmdcmsg,
1842 global.ap.applid,
1843 card->msgid++,
1844 card->contrnr, /* adr */
1845 si2cip(bchan->si1, bchan->si2), /* cipvalue */
1846 called, /* CalledPartyNumber */
1847 calling, /* CallingPartyNumber */
1848 NULL, /* CalledPartySubaddress */
1849 NULL, /* CallingPartySubaddress */
1850 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1851 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1852 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1853 b1config(bchan->l2, bchan->l3), /* B1configuration */
1854 NULL, /* B2configuration */
1855 NULL, /* B3configuration */
1856 NULL, /* BC */
1857 NULL, /* LLC */
1858 NULL, /* HLC */
1859 /* BChannelinformation */
1860 isleasedline ? AdditionalInfo : NULL,
1861 NULL, /* Keypadfacility */
1862 NULL, /* Useruserdata */
1863 NULL /* Facilitydataarray */
1864 );
1865 if ((plcip = new_plci(card, (c->arg % card->nbchan))) == NULL) {
1866 cmd.command = ISDN_STAT_DHUP;
1867 cmd.driver = card->myid;
1868 cmd.arg = (c->arg % card->nbchan);
1869 card->interface.statcallb(&cmd);
1870 return -1;
1871 }
1872 plcip->msgid = cmdcmsg.Messagenumber;
1873 plcip->leasedline = isleasedline;
1874 plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
1875 send_message(card, &cmdcmsg);
1876 return 0;
1877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 case ISDN_CMD_ACCEPTD:
1880
1881 bchan = &card->bchans[c->arg % card->nbchan];
1882 if (debugmode)
1883 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
1884 card->contrnr,
1885 c->arg, bchan->l2, bchan->l3);
1886
1887 capi_fill_CONNECT_RESP(&cmdcmsg,
1888 global.ap.applid,
1889 card->msgid++,
1890 bchan->plcip->plci, /* adr */
1891 0, /* Reject */
1892 b1prot(bchan->l2, bchan->l3), /* B1protocol */
1893 b2prot(bchan->l2, bchan->l3), /* B2protocol */
1894 b3prot(bchan->l2, bchan->l3), /* B3protocol */
1895 b1config(bchan->l2, bchan->l3), /* B1configuration */
1896 NULL, /* B2configuration */
1897 NULL, /* B3configuration */
1898 NULL, /* ConnectedNumber */
1899 NULL, /* ConnectedSubaddress */
1900 NULL, /* LLC */
1901 NULL, /* BChannelinformation */
1902 NULL, /* Keypadfacility */
1903 NULL, /* Useruserdata */
1904 NULL /* Facilitydataarray */
Joe Perches475be4d2012-02-19 19:52:38 -08001905 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 capi_cmsg2message(&cmdcmsg, cmdcmsg.buf);
1907 plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
1908 send_message(card, &cmdcmsg);
1909 return 0;
1910
1911 case ISDN_CMD_ACCEPTB:
1912 if (debugmode)
1913 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
1914 card->contrnr,
1915 c->arg);
1916 return -ENOSYS;
1917
1918 case ISDN_CMD_HANGUP:
1919 if (debugmode)
1920 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
1921 card->contrnr,
1922 c->arg);
1923 bchan = &card->bchans[c->arg % card->nbchan];
1924
1925 if (bchan->disconnecting) {
1926 if (debugmode)
1927 printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
1928 card->contrnr,
1929 c->arg);
1930 return 0;
1931 }
1932 if (bchan->nccip) {
1933 bchan->disconnecting = 1;
1934 capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
1935 global.ap.applid,
1936 card->msgid++,
1937 bchan->nccip->ncci,
1938 NULL /* NCPI */
Joe Perches475be4d2012-02-19 19:52:38 -08001939 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
1941 send_message(card, &cmdcmsg);
1942 return 0;
1943 } else if (bchan->plcip) {
1944 if (bchan->plcip->state == ST_PLCI_INCOMING) {
1945 /*
1946 * just ignore, we a called from
1947 * isdn_status_callback(),
1948 * which will return 0 or 2, this is handled
1949 * by the CONNECT_IND handler
1950 */
1951 bchan->disconnecting = 1;
1952 return 0;
1953 } else if (bchan->plcip->plci) {
1954 bchan->disconnecting = 1;
1955 capi_fill_DISCONNECT_REQ(&cmdcmsg,
1956 global.ap.applid,
1957 card->msgid++,
Joe Perches475be4d2012-02-19 19:52:38 -08001958 bchan->plcip->plci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 NULL, /* BChannelinformation */
1960 NULL, /* Keypadfacility */
1961 NULL, /* Useruserdata */
1962 NULL /* Facilitydataarray */
Joe Perches475be4d2012-02-19 19:52:38 -08001963 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
1965 send_message(card, &cmdcmsg);
1966 return 0;
1967 } else {
1968 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
1969 card->contrnr,
1970 c->arg);
1971 return -EINVAL;
1972 }
1973 }
1974 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001975 card->contrnr,
1976 c->arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -EINVAL;
1978/* ready */
1979
1980 case ISDN_CMD_SETL2:
1981 if (debugmode)
1982 printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
1983 card->contrnr,
1984 (c->arg & 0xff), (c->arg >> 8));
1985 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1986 bchan->l2 = (c->arg >> 8);
1987 return 0;
1988
1989 case ISDN_CMD_SETL3:
1990 if (debugmode)
1991 printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
1992 card->contrnr,
1993 (c->arg & 0xff), (c->arg >> 8));
1994 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1995 bchan->l3 = (c->arg >> 8);
1996 return 0;
1997
1998 case ISDN_CMD_SETEAZ:
1999 if (debugmode)
2000 printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
2001 card->contrnr,
2002 c->parm.num, c->arg);
2003 bchan = &card->bchans[c->arg % card->nbchan];
2004 strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
2005 return 0;
2006
2007 case ISDN_CMD_CLREAZ:
2008 if (debugmode)
2009 printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002010 card->contrnr, c->arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 bchan = &card->bchans[c->arg % card->nbchan];
2012 bchan->msn[0] = 0;
2013 return 0;
2014
2015 default:
2016 printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002017 card->contrnr, c->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return -EINVAL;
2019 }
2020 return 0;
2021}
2022
Joe Perches475be4d2012-02-19 19:52:38 -08002023static int if_command(isdn_ctrl *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 capidrv_contr *card = findcontrbydriverid(c->driver);
2026
2027 if (card)
2028 return capidrv_command(c, card);
2029
2030 printk(KERN_ERR
Joe Perches475be4d2012-02-19 19:52:38 -08002031 "capidrv: if_command %d called with invalid driverId %d!\n",
2032 c->command, c->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return -ENODEV;
2034}
2035
2036static _cmsg sendcmsg;
2037
2038static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
2039{
2040 capidrv_contr *card = findcontrbydriverid(id);
2041 capidrv_bchan *bchan;
2042 capidrv_ncci *nccip;
2043 int len = skb->len;
2044 int msglen;
2045 u16 errcode;
2046 u16 datahandle;
Jeff Garzikb3932432007-10-25 23:02:14 -04002047 u32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 if (!card) {
2050 printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
2051 id);
2052 return 0;
2053 }
2054 if (debugmode > 4)
2055 printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002056 card->contrnr, len, skb, doack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 bchan = &card->bchans[channel % card->nbchan];
2058 nccip = bchan->nccip;
2059 if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
2060 printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
2061 card->contrnr, card->name, channel);
2062 return 0;
2063 }
2064 datahandle = nccip->datahandle;
Jeff Garzikb3932432007-10-25 23:02:14 -04002065
2066 /*
2067 * Here we copy pointer skb->data into the 32-bit 'Data' field.
2068 * The 'Data' field is not used in practice in linux kernel
2069 * (neither in 32 or 64 bit), but should have some value,
2070 * since a CAPI message trace will display it.
2071 *
2072 * The correct value in the 32 bit case is the address of the
2073 * data, in 64 bit it makes no sense, we use 0 there.
2074 */
2075
2076#ifdef CONFIG_64BIT
2077 data = 0;
2078#else
2079 data = (unsigned long) skb->data;
2080#endif
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
2083 nccip->ncci, /* adr */
Jeff Garzikb3932432007-10-25 23:02:14 -04002084 data, /* Data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 skb->len, /* DataLength */
2086 datahandle, /* DataHandle */
2087 0 /* Flags */
Joe Perches475be4d2012-02-19 19:52:38 -08002088 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
Joe Perches475be4d2012-02-19 19:52:38 -08002091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 capi_cmsg2message(&sendcmsg, sendcmsg.buf);
2094 msglen = CAPIMSG_LEN(sendcmsg.buf);
2095 if (skb_headroom(skb) < msglen) {
2096 struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
2097 if (!nskb) {
2098 printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002099 card->contrnr);
2100 (void)capidrv_del_ack(nccip, datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return 0;
2102 }
2103 printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
2104 card->contrnr, skb_headroom(skb), msglen);
2105 memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
2106 errcode = capi20_put_message(&global.ap, nskb);
2107 if (errcode == CAPI_NOERROR) {
2108 dev_kfree_skb(skb);
2109 nccip->datahandle++;
2110 return len;
2111 }
2112 if (debugmode > 3)
2113 printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002114 card->contrnr, errcode, capi_info2str(errcode));
2115 (void)capidrv_del_ack(nccip, datahandle);
2116 dev_kfree_skb(nskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
2118 } else {
2119 memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
2120 errcode = capi20_put_message(&global.ap, skb);
2121 if (errcode == CAPI_NOERROR) {
2122 nccip->datahandle++;
2123 return len;
2124 }
2125 if (debugmode > 3)
2126 printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002127 card->contrnr, errcode, capi_info2str(errcode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 skb_pull(skb, msglen);
Joe Perches475be4d2012-02-19 19:52:38 -08002129 (void)capidrv_del_ack(nccip, datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
2131 }
2132}
2133
2134static int if_readstat(u8 __user *buf, int len, int id, int channel)
2135{
2136 capidrv_contr *card = findcontrbydriverid(id);
2137 int count;
2138 u8 __user *p;
2139
2140 if (!card) {
2141 printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
2142 id);
2143 return -ENODEV;
2144 }
2145
Joe Perches475be4d2012-02-19 19:52:38 -08002146 for (p = buf, count = 0; count < len; p++, count++) {
Jeff Garzik7786ce12006-10-17 00:10:40 -07002147 if (put_user(*card->q931_read++, p))
2148 return -EFAULT;
Joe Perches475be4d2012-02-19 19:52:38 -08002149 if (card->q931_read > card->q931_end)
2150 card->q931_read = card->q931_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152 return count;
2153
2154}
2155
2156static void enable_dchannel_trace(capidrv_contr *card)
2157{
Joe Perches475be4d2012-02-19 19:52:38 -08002158 u8 manufacturer[CAPI_MANUFACTURER_LEN];
2159 capi_version version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 u16 contr = card->contrnr;
2161 u16 errcode;
2162 u16 avmversion[3];
2163
Joe Perches475be4d2012-02-19 19:52:38 -08002164 errcode = capi20_get_manufacturer(contr, manufacturer);
2165 if (errcode != CAPI_NOERROR) {
2166 printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
2167 card->name, errcode);
2168 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07002170 if (strstr(manufacturer, "AVM") == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -08002171 printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
2172 card->name, manufacturer);
2173 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
Joe Perches475be4d2012-02-19 19:52:38 -08002175 errcode = capi20_get_version(contr, &version);
2176 if (errcode != CAPI_NOERROR) {
2177 printk(KERN_ERR "%s: can't get version (0x%x)\n",
2178 card->name, errcode);
2179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181 avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
2182 avmversion[1] = (version.majormanuversion << 4) & 0xf0;
2183 avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
2184 avmversion[2] |= version.minormanuversion & 0x0f;
2185
Joe Perches475be4d2012-02-19 19:52:38 -08002186 if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
2188 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
2189 card->msgid++,
2190 contr,
2191 0x214D5641, /* ManuID */
2192 0, /* Class */
2193 1, /* Function */
2194 (_cstruct)"\004\200\014\000\000");
2195 } else {
2196 printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
2197 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
2198 card->msgid++,
2199 contr,
2200 0x214D5641, /* ManuID */
2201 0, /* Class */
2202 1, /* Function */
2203 (_cstruct)"\004\002\003\000\000");
2204 }
2205 send_message(card, &cmdcmsg);
2206}
2207
2208
2209static void send_listen(capidrv_contr *card)
2210{
2211 capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
2212 card->msgid++,
2213 card->contrnr, /* controller */
2214 1 << 6, /* Infomask */
2215 card->cipmask,
2216 card->cipmask2,
2217 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 listen_change_state(card, EV_LISTEN_REQ);
Tilman Schmidte4847022009-10-06 12:18:10 +00002219 send_message(card, &cmdcmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222static void listentimerfunc(unsigned long x)
2223{
2224 capidrv_contr *card = (capidrv_contr *)x;
2225 if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
2226 printk(KERN_ERR "%s: controller dead ??\n", card->name);
Joe Perches475be4d2012-02-19 19:52:38 -08002227 send_listen(card);
2228 mod_timer(&card->listentimer, jiffies + 60 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231
2232static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
2233{
2234 capidrv_contr *card;
2235 unsigned long flags;
2236 isdn_ctrl cmd;
2237 char id[20];
2238 int i;
2239
2240 sprintf(id, "capidrv-%d", contr);
2241 if (!try_module_get(THIS_MODULE)) {
2242 printk(KERN_WARNING "capidrv: (%s) Could not reserve module\n", id);
2243 return -1;
2244 }
Burman Yan41f96932006-12-08 02:39:35 -08002245 if (!(card = kzalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08002247 "capidrv: (%s) Could not allocate contr-struct.\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return -1;
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 card->owner = THIS_MODULE;
2251 init_timer(&card->listentimer);
2252 strcpy(card->name, id);
2253 card->contrnr = contr;
2254 card->nbchan = profp->nbchannel;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002255 card->bchans = kmalloc(sizeof(capidrv_bchan) * card->nbchan, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!card->bchans) {
2257 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08002258 "capidrv: (%s) Could not allocate bchan-structs.\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 module_put(card->owner);
2260 kfree(card);
2261 return -1;
2262 }
2263 card->interface.channels = profp->nbchannel;
2264 card->interface.maxbufsize = 2048;
2265 card->interface.command = if_command;
2266 card->interface.writebuf_skb = if_sendbuf;
2267 card->interface.writecmd = NULL;
2268 card->interface.readstat = if_readstat;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +00002269 card->interface.features =
2270 ISDN_FEATURE_L2_HDLC |
Joe Perches475be4d2012-02-19 19:52:38 -08002271 ISDN_FEATURE_L2_TRANS |
2272 ISDN_FEATURE_L3_TRANS |
2273 ISDN_FEATURE_P_UNKNOWN |
2274 ISDN_FEATURE_L2_X75I |
2275 ISDN_FEATURE_L2_X75UI |
2276 ISDN_FEATURE_L2_X75BUI;
2277 if (profp->support1 & (1 << 2))
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +00002278 card->interface.features |=
2279 ISDN_FEATURE_L2_V11096 |
Joe Perches475be4d2012-02-19 19:52:38 -08002280 ISDN_FEATURE_L2_V11019 |
2281 ISDN_FEATURE_L2_V11038;
2282 if (profp->support1 & (1 << 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 card->interface.features |= ISDN_FEATURE_L2_MODEM;
2284 card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
2285 strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
2286
2287
2288 card->q931_read = card->q931_buf;
2289 card->q931_write = card->q931_buf;
2290 card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
2291
2292 if (!register_isdn(&card->interface)) {
2293 printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
2294 kfree(card->bchans);
2295 module_put(card->owner);
2296 kfree(card);
2297 return -1;
2298 }
2299 card->myid = card->interface.channels;
2300 memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
2301 for (i = 0; i < card->nbchan; i++) {
2302 card->bchans[i].contr = card;
2303 }
2304
2305 spin_lock_irqsave(&global_lock, flags);
2306 card->next = global.contr_list;
2307 global.contr_list = card;
2308 global.ncontr++;
2309 spin_unlock_irqrestore(&global_lock, flags);
2310
2311 cmd.command = ISDN_STAT_RUN;
2312 cmd.driver = card->myid;
2313 card->interface.statcallb(&cmd);
2314
2315 card->cipmask = 0x1FFF03FF; /* any */
2316 card->cipmask2 = 0;
2317
2318 card->listentimer.data = (unsigned long)card;
2319 card->listentimer.function = listentimerfunc;
2320 send_listen(card);
Joe Perches475be4d2012-02-19 19:52:38 -08002321 mod_timer(&card->listentimer, jiffies + 60 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 printk(KERN_INFO "%s: now up (%d B channels)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002324 card->name, card->nbchan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 enable_dchannel_trace(card);
2327
2328 return 0;
2329}
2330
2331static int capidrv_delcontr(u16 contr)
2332{
2333 capidrv_contr **pp, *card;
2334 unsigned long flags;
2335 isdn_ctrl cmd;
2336
2337 spin_lock_irqsave(&global_lock, flags);
2338 for (card = global.contr_list; card; card = card->next) {
2339 if (card->contrnr == contr)
2340 break;
2341 }
2342 if (!card) {
2343 spin_unlock_irqrestore(&global_lock, flags);
2344 printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
2345 return -1;
2346 }
Jeff Garzikb3932432007-10-25 23:02:14 -04002347
2348 /* FIXME: maybe a race condition the card should be removed
2349 * here from global list /kkeil
2350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 spin_unlock_irqrestore(&global_lock, flags);
2352
2353 del_timer(&card->listentimer);
2354
2355 if (debugmode)
2356 printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002357 card->contrnr, card->myid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 cmd.command = ISDN_STAT_STOP;
2360 cmd.driver = card->myid;
2361 card->interface.statcallb(&cmd);
2362
2363 while (card->nbchan) {
2364
2365 cmd.command = ISDN_STAT_DISCH;
2366 cmd.driver = card->myid;
Joe Perches475be4d2012-02-19 19:52:38 -08002367 cmd.arg = card->nbchan - 1;
2368 cmd.parm.num[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (debugmode)
2370 printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002371 card->contrnr, card->myid, cmd.arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 card->interface.statcallb(&cmd);
2373
Joe Perches475be4d2012-02-19 19:52:38 -08002374 if (card->bchans[card->nbchan - 1].nccip)
2375 free_ncci(card, card->bchans[card->nbchan - 1].nccip);
2376 if (card->bchans[card->nbchan - 1].plcip)
2377 free_plci(card, card->bchans[card->nbchan - 1].plcip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (card->plci_list)
2379 printk(KERN_ERR "capidrv: bug in free_plci()\n");
2380 card->nbchan--;
2381 }
2382 kfree(card->bchans);
2383 card->bchans = NULL;
2384
2385 if (debugmode)
2386 printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002387 card->contrnr, card->myid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 cmd.command = ISDN_STAT_UNLOAD;
2390 cmd.driver = card->myid;
2391 card->interface.statcallb(&cmd);
2392
2393 if (debugmode)
2394 printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002395 card->contrnr, card->myid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 spin_lock_irqsave(&global_lock, flags);
2398 for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
2399 if (*pp == card) {
2400 *pp = (*pp)->next;
2401 card->next = NULL;
2402 global.ncontr--;
2403 break;
2404 }
2405 }
2406 spin_unlock_irqrestore(&global_lock, flags);
2407
2408 module_put(card->owner);
2409 printk(KERN_INFO "%s: now down.\n", card->name);
2410 kfree(card);
2411 return 0;
2412}
2413
2414
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002415static int
2416lower_callback(struct notifier_block *nb, unsigned long val, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002418 capi_profile profile;
2419 u32 contr = (long)v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002421 switch (val) {
2422 case CAPICTR_UP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 printk(KERN_INFO "capidrv: controller %hu up\n", contr);
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002424 if (capi20_get_profile(contr, &profile) == CAPI_NOERROR)
2425 (void) capidrv_addcontr(contr, &profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 break;
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002427 case CAPICTR_DOWN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 printk(KERN_INFO "capidrv: controller %hu down\n", contr);
2429 (void) capidrv_delcontr(contr);
2430 break;
2431 }
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002432 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
2435/*
2436 * /proc/capi/capidrv:
2437 * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
2438 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002439static int capidrv_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002441 seq_printf(m, "%lu %lu %lu %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002442 global.ap.nrecvctlpkt,
2443 global.ap.nrecvdatapkt,
2444 global.ap.nsentctlpkt,
2445 global.ap.nsentdatapkt);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002449static int capidrv_proc_open(struct inode *inode, struct file *file)
2450{
2451 return single_open(file, capidrv_proc_show, NULL);
2452}
2453
2454static const struct file_operations capidrv_proc_fops = {
2455 .owner = THIS_MODULE,
2456 .open = capidrv_proc_open,
2457 .read = seq_read,
2458 .llseek = seq_lseek,
2459 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460};
2461
2462static void __init proc_init(void)
2463{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002464 proc_create("capi/capidrv", 0, NULL, &capidrv_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
2467static void __exit proc_exit(void)
2468{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002469 remove_proc_entry("capi/capidrv", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002472static struct notifier_block capictr_nb = {
2473 .notifier_call = lower_callback,
2474};
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476static int __init capidrv_init(void)
2477{
2478 capi_profile profile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 u32 ncontr, contr;
2480 u16 errcode;
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 global.ap.rparam.level3cnt = -2; /* number of bchannels twice */
2483 global.ap.rparam.datablkcnt = 16;
2484 global.ap.rparam.datablklen = 2048;
2485
2486 global.ap.recv_message = capidrv_recv_message;
2487 errcode = capi20_register(&global.ap);
2488 if (errcode) {
2489 return -EIO;
2490 }
2491
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002492 register_capictr_notifier(&capictr_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 errcode = capi20_get_profile(0, &profile);
2495 if (errcode != CAPI_NOERROR) {
Tejun Heo7fa5e852010-12-24 15:59:05 +01002496 unregister_capictr_notifier(&capictr_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 capi20_release(&global.ap);
2498 return -EIO;
2499 }
2500
2501 ncontr = profile.ncontroller;
2502 for (contr = 1; contr <= ncontr; contr++) {
2503 errcode = capi20_get_profile(contr, &profile);
2504 if (errcode != CAPI_NOERROR)
2505 continue;
2506 (void) capidrv_addcontr(contr, &profile);
2507 }
2508 proc_init();
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return 0;
2511}
2512
2513static void __exit capidrv_exit(void)
2514{
Jan Kiszkaef69bb22010-02-08 10:12:13 +00002515 unregister_capictr_notifier(&capictr_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 capi20_release(&global.ap);
2517
2518 proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
2521module_init(capidrv_init);
2522module_exit(capidrv_exit);