import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / isdn / avmb1 / kcapi.c
1 /* $Id: kcapi.c,v 1.1.1.1 2005/04/11 02:50:23 jack Exp $
2  * 
3  * Kernel CAPI 2.0 Module
4  * 
5  * Copyright 1999 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 #define CONFIG_AVMB1_COMPAT
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <asm/segment.h>
21 #include <linux/proc_fs.h>
22 #include <linux/skbuff.h>
23 #include <linux/tqueue.h>
24 #include <linux/capi.h>
25 #include <linux/kernelcapi.h>
26 #include <linux/locks.h>
27 #include <linux/init.h>
28 #include <asm/uaccess.h>
29 #include "capicmd.h"
30 #include "capiutil.h"
31 #include "capilli.h"
32 #ifdef CONFIG_AVMB1_COMPAT
33 #include <linux/b1lli.h>
34 #endif
35
36 static char *revision = "$Revision: 1.1.1.1 $";
37
38 /* ------------------------------------------------------------- */
39
40 #define CARD_FREE       0
41 #define CARD_DETECTED   1
42 #define CARD_LOADING    2
43 #define CARD_RUNNING    3
44
45 /* ------------------------------------------------------------- */
46
47 static int showcapimsgs = 0;
48
49 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
50 MODULE_AUTHOR("Carsten Paeth");
51 MODULE_LICENSE("GPL");
52 MODULE_PARM(showcapimsgs, "i");
53
54 /* ------------------------------------------------------------- */
55
56 struct msgidqueue {
57         struct msgidqueue *next;
58         __u16 msgid;
59 };
60
61 struct capi_ncci {
62         struct capi_ncci *next;
63         __u16 applid;
64         __u32 ncci;
65         __u32 winsize;
66         int   nmsg;
67         spinlock_t lock;
68         struct msgidqueue *msgidqueue;
69         struct msgidqueue *msgidlast;
70         struct msgidqueue *msgidfree;
71         struct msgidqueue msgidpool[CAPI_MAXDATAWINDOW];
72 };
73
74 struct capi_appl {
75         __u16 applid;
76         capi_register_params rparam;
77         int releasing;
78         void *param;
79         void (*signal) (__u16 applid, void *param);
80         struct sk_buff_head recv_queue;
81         int nncci;
82         struct capi_ncci *nccilist;
83
84         unsigned long nrecvctlpkt;
85         unsigned long nrecvdatapkt;
86         unsigned long nsentctlpkt;
87         unsigned long nsentdatapkt;
88 };
89
90 struct capi_notifier {
91         struct capi_notifier *next;
92         unsigned int cmd;
93         __u32 controller;
94         __u16 applid;
95         __u32 ncci;
96 };
97
98 /* ------------------------------------------------------------- */
99
100 static struct capi_version driver_version = {2, 0, 1, 1<<4};
101 static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
102 static char capi_manufakturer[64] = "AVM Berlin";
103
104 #define APPL(a)            (&applications[(a)-1])
105 #define VALID_APPLID(a)    ((a) && (a) <= CAPI_MAXAPPL && APPL(a)->applid == a)
106 #define APPL_IS_FREE(a)    (APPL(a)->applid == 0)
107 #define APPL_MARK_FREE(a)  do{ APPL(a)->applid=0; MOD_DEC_USE_COUNT; }while(0)
108 #define APPL_MARK_USED(a)  do{ APPL(a)->applid=(a); MOD_INC_USE_COUNT; }while(0)
109
110 #define NCCI2CTRL(ncci)    (((ncci) >> 24) & 0x7f)
111
112 #define VALID_CARD(c)      ((c) > 0 && (c) <= CAPI_MAXCONTR)
113 #define CARD(c)            (&cards[(c)-1])
114 #define CARDNR(cp)         ((((cp)-cards)+1) & 0xff)
115
116 static struct capi_appl applications[CAPI_MAXAPPL];
117 static struct capi_ctr cards[CAPI_MAXCONTR];
118 static int ncards = 0;
119 static struct sk_buff_head recv_queue;
120 static struct capi_interface_user *capi_users = 0;
121 static spinlock_t capi_users_lock = SPIN_LOCK_UNLOCKED;
122 static struct capi_driver *drivers;
123 static spinlock_t drivers_lock = SPIN_LOCK_UNLOCKED;
124
125 static struct tq_struct tq_state_notify;
126 static struct tq_struct tq_recv_notify;
127
128 /* -------- util functions ------------------------------------ */
129
130 static char *cardstate2str(unsigned short cardstate)
131 {
132         switch (cardstate) {
133                 default:
134                 case CARD_FREE:         return "free";
135                 case CARD_DETECTED:     return "detected";
136                 case CARD_LOADING:      return "loading";
137                 case CARD_RUNNING:      return "running";
138         }
139 }
140
141 static inline int capi_cmd_valid(__u8 cmd)
142 {
143         switch (cmd) {
144         case CAPI_ALERT:
145         case CAPI_CONNECT:
146         case CAPI_CONNECT_ACTIVE:
147         case CAPI_CONNECT_B3_ACTIVE:
148         case CAPI_CONNECT_B3:
149         case CAPI_CONNECT_B3_T90_ACTIVE:
150         case CAPI_DATA_B3:
151         case CAPI_DISCONNECT_B3:
152         case CAPI_DISCONNECT:
153         case CAPI_FACILITY:
154         case CAPI_INFO:
155         case CAPI_LISTEN:
156         case CAPI_MANUFACTURER:
157         case CAPI_RESET_B3:
158         case CAPI_SELECT_B_PROTOCOL:
159                 return 1;
160         }
161         return 0;
162 }
163
164 static inline int capi_subcmd_valid(__u8 subcmd)
165 {
166         switch (subcmd) {
167         case CAPI_REQ:
168         case CAPI_CONF:
169         case CAPI_IND:
170         case CAPI_RESP:
171                 return 1;
172         }
173         return 0;
174 }
175
176 /* -------- /proc functions ----------------------------------- */
177 /*
178  * /proc/capi/applications:
179  *      applid l3cnt dblkcnt dblklen #ncci recvqueuelen
180  */
181 static int proc_applications_read_proc(char *page, char **start, off_t off,
182                                        int count, int *eof, void *data)
183 {
184         struct capi_appl *ap;
185         int i;
186         int len = 0;
187
188         for (i=0; i < CAPI_MAXAPPL; i++) {
189                 ap = &applications[i];
190                 if (ap->applid == 0) continue;
191                 len += sprintf(page+len, "%u %d %d %d %d %d\n",
192                         ap->applid,
193                         ap->rparam.level3cnt,
194                         ap->rparam.datablkcnt,
195                         ap->rparam.datablklen,
196                         ap->nncci,
197                         skb_queue_len(&ap->recv_queue));
198                 if (len <= off) {
199                         off -= len;
200                         len = 0;
201                 } else {
202                         if (len-off > count)
203                                 goto endloop;
204                 }
205         }
206 endloop:
207         *start = page+off;
208         if (len < count)
209                 *eof = 1;
210         if (len>count) len = count;
211         if (len<0) len = 0;
212         return len;
213 }
214
215 /*
216  * /proc/capi/ncci:
217  *      applid ncci winsize nblk
218  */
219 static int proc_ncci_read_proc(char *page, char **start, off_t off,
220                                        int count, int *eof, void *data)
221 {
222         struct capi_appl *ap;
223         struct capi_ncci *np;
224         int i;
225         int len = 0;
226
227         for (i=0; i < CAPI_MAXAPPL; i++) {
228                 ap = &applications[i];
229                 if (ap->applid == 0) continue;
230                 for (np = ap->nccilist; np; np = np->next) {
231                         len += sprintf(page+len, "%d 0x%x %d %d\n",
232                                 np->applid,
233                                 np->ncci,
234                                 np->winsize,
235                                 np->nmsg);
236                         if (len <= off) {
237                                 off -= len;
238                                 len = 0;
239                         } else {
240                                 if (len-off > count)
241                                         goto endloop;
242                         }
243                 }
244         }
245 endloop:
246         *start = page+off;
247         if (len < count)
248                 *eof = 1;
249         if (len>count) len = count;
250         if (len<0) len = 0;
251         return len;
252 }
253
254 /*
255  * /proc/capi/driver:
256  *      driver ncontroller
257  */
258 static int proc_driver_read_proc(char *page, char **start, off_t off,
259                                        int count, int *eof, void *data)
260 {
261         struct capi_driver *driver;
262         int len = 0;
263
264         spin_lock(&drivers_lock);
265         for (driver = drivers; driver; driver = driver->next) {
266                 len += sprintf(page+len, "%-32s %d %s\n",
267                                         driver->name,
268                                         driver->ncontroller,
269                                         driver->revision);
270                 if (len <= off) {
271                         off -= len;
272                         len = 0;
273                 } else {
274                         if (len-off > count)
275                                 goto endloop;
276                 }
277         }
278 endloop:
279         spin_unlock(&drivers_lock);
280         *start = page+off;
281         if (len < count)
282                 *eof = 1;
283         if (len>count) len = count;
284         if (len<0) len = 0;
285         return len;
286 }
287
288 /*
289  * /proc/capi/users:
290  *      name
291  */
292 static int proc_users_read_proc(char *page, char **start, off_t off,
293                                        int count, int *eof, void *data)
294 {
295         struct capi_interface_user *cp;
296         int len = 0;
297
298         spin_lock(&capi_users_lock);
299         for (cp = capi_users; cp ; cp = cp->next) {
300                 len += sprintf(page+len, "%s\n", cp->name);
301                 if (len <= off) {
302                         off -= len;
303                         len = 0;
304                 } else {
305                         if (len-off > count)
306                                 goto endloop;
307                 }
308         }
309 endloop:
310         spin_unlock(&capi_users_lock);
311         *start = page+off;
312         if (len < count)
313                 *eof = 1;
314         if (len>count) len = count;
315         if (len<0) len = 0;
316         return len;
317 }
318
319 /*
320  * /proc/capi/controller:
321  *      cnr driver cardstate name driverinfo
322  */
323 static int proc_controller_read_proc(char *page, char **start, off_t off,
324                                        int count, int *eof, void *data)
325 {
326         struct capi_ctr *cp;
327         int i;
328         int len = 0;
329
330         for (i=0; i < CAPI_MAXCONTR; i++) {
331                 cp = &cards[i];
332                 if (cp->cardstate == CARD_FREE) continue;
333                 len += sprintf(page+len, "%d %-10s %-8s %-16s %s\n",
334                         cp->cnr, cp->driver->name, 
335                         cardstate2str(cp->cardstate),
336                         cp->name,
337                         cp->driver->procinfo ?  cp->driver->procinfo(cp) : ""
338                         );
339                 if (len <= off) {
340                         off -= len;
341                         len = 0;
342                 } else {
343                         if (len-off > count)
344                                 goto endloop;
345                 }
346         }
347 endloop:
348         *start = page+off;
349         if (len < count)
350                 *eof = 1;
351         if (len>count) len = count;
352         if (len<0) len = 0;
353         return len;
354 }
355
356 /*
357  * /proc/capi/applstats:
358  *      applid nrecvctlpkt nrecvdatapkt nsentctlpkt nsentdatapkt
359  */
360 static int proc_applstats_read_proc(char *page, char **start, off_t off,
361                                        int count, int *eof, void *data)
362 {
363         struct capi_appl *ap;
364         int i;
365         int len = 0;
366
367         for (i=0; i < CAPI_MAXAPPL; i++) {
368                 ap = &applications[i];
369                 if (ap->applid == 0) continue;
370                 len += sprintf(page+len, "%u %lu %lu %lu %lu\n",
371                         ap->applid,
372                         ap->nrecvctlpkt,
373                         ap->nrecvdatapkt,
374                         ap->nsentctlpkt,
375                         ap->nsentdatapkt);
376                 if (len <= off) {
377                         off -= len;
378                         len = 0;
379                 } else {
380                         if (len-off > count)
381                                 goto endloop;
382                 }
383         }
384 endloop:
385         *start = page+off;
386         if (len < count)
387                 *eof = 1;
388         if (len>count) len = count;
389         if (len<0) len = 0;
390         return len;
391 }
392
393 /*
394  * /proc/capi/contrstats:
395  *      cnr nrecvctlpkt nrecvdatapkt nsentctlpkt nsentdatapkt
396  */
397 static int proc_contrstats_read_proc(char *page, char **start, off_t off,
398                                        int count, int *eof, void *data)
399 {
400         struct capi_ctr *cp;
401         int i;
402         int len = 0;
403
404         for (i=0; i < CAPI_MAXCONTR; i++) {
405                 cp = &cards[i];
406                 if (cp->cardstate == CARD_FREE) continue;
407                 len += sprintf(page+len, "%d %lu %lu %lu %lu\n",
408                         cp->cnr, 
409                         cp->nrecvctlpkt,
410                         cp->nrecvdatapkt,
411                         cp->nsentctlpkt,
412                         cp->nsentdatapkt);
413                 if (len <= off) {
414                         off -= len;
415                         len = 0;
416                 } else {
417                         if (len-off > count)
418                                 goto endloop;
419                 }
420         }
421 endloop:
422         *start = page+off;
423         if (len < count)
424                 *eof = 1;
425         if (len>count) len = count;
426         if (len<0) len = 0;
427         return len;
428 }
429
430 static struct procfsentries {
431   char *name;
432   mode_t mode;
433   int (*read_proc)(char *page, char **start, off_t off,
434                                        int count, int *eof, void *data);
435   struct proc_dir_entry *procent;
436 } procfsentries[] = {
437    { "capi",              S_IFDIR, 0 },
438    { "capi/applications", 0      , proc_applications_read_proc },
439    { "capi/ncci",         0      , proc_ncci_read_proc },
440    { "capi/driver",       0      , proc_driver_read_proc },
441    { "capi/users",        0      , proc_users_read_proc },
442    { "capi/controller",   0      , proc_controller_read_proc },
443    { "capi/applstats",    0      , proc_applstats_read_proc },
444    { "capi/contrstats",   0      , proc_contrstats_read_proc },
445    { "capi/drivers",      S_IFDIR, 0 },
446    { "capi/controllers",  S_IFDIR, 0 },
447 };
448
449 static void proc_capi_init(void)
450 {
451     int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
452     int i;
453
454     for (i=0; i < nelem; i++) {
455         struct procfsentries *p = procfsentries + i;
456         p->procent = create_proc_entry(p->name, p->mode, 0);
457         if (p->procent) p->procent->read_proc = p->read_proc;
458     }
459 }
460
461 static void proc_capi_exit(void)
462 {
463     int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
464     int i;
465
466     for (i=nelem-1; i >= 0; i--) {
467         struct procfsentries *p = procfsentries + i;
468         if (p->procent) {
469            remove_proc_entry(p->name, 0);
470            p->procent = 0;
471         }
472     }
473 }
474
475 /* -------- Notifier handling --------------------------------- */
476
477 static struct capi_notifier_list{
478         struct capi_notifier *head;
479         struct capi_notifier *tail;
480 } notifier_list;
481
482 static spinlock_t notifier_lock = SPIN_LOCK_UNLOCKED;
483
484 static inline void notify_enqueue(struct capi_notifier *np)
485 {
486         struct capi_notifier_list *q = &notifier_list;
487         unsigned long flags;
488
489         spin_lock_irqsave(&notifier_lock, flags);
490         if (q->tail) {
491                 q->tail->next = np;
492                 q->tail = np;
493         } else {
494                 q->head = q->tail = np;
495         }
496         spin_unlock_irqrestore(&notifier_lock, flags);
497 }
498
499 static inline struct capi_notifier *notify_dequeue(void)
500 {
501         struct capi_notifier_list *q = &notifier_list;
502         struct capi_notifier *np = 0;
503         unsigned long flags;
504
505         spin_lock_irqsave(&notifier_lock, flags);
506         if (q->head) {
507                 np = q->head;
508                 if ((q->head = np->next) == 0)
509                         q->tail = 0;
510                 np->next = 0;
511         }
512         spin_unlock_irqrestore(&notifier_lock, flags);
513         return np;
514 }
515
516 static int notify_push(unsigned int cmd, __u32 controller,
517                                 __u16 applid, __u32 ncci)
518 {
519         struct capi_notifier *np;
520
521         MOD_INC_USE_COUNT;
522         np = (struct capi_notifier *)kmalloc(sizeof(struct capi_notifier), GFP_ATOMIC);
523         if (!np) {
524                 MOD_DEC_USE_COUNT;
525                 return -1;
526         }
527         memset(np, 0, sizeof(struct capi_notifier));
528         np->cmd = cmd;
529         np->controller = controller;
530         np->applid = applid;
531         np->ncci = ncci;
532         notify_enqueue(np);
533         /*
534          * The notifier will result in adding/deleteing
535          * of devices. Devices can only removed in
536          * user process, not in bh.
537          */
538         MOD_INC_USE_COUNT;
539         if (schedule_task(&tq_state_notify) == 0)
540                 MOD_DEC_USE_COUNT;
541         return 0;
542 }
543
544 /* -------- KCI_CONTRUP --------------------------------------- */
545
546 static void notify_up(__u32 contr)
547 {
548         struct capi_interface_user *p;
549
550         printk(KERN_NOTICE "kcapi: notify up contr %d\n", contr);
551         spin_lock(&capi_users_lock);
552         for (p = capi_users; p; p = p->next) {
553                 if (!p->callback) continue;
554                 (*p->callback) (KCI_CONTRUP, contr, &CARD(contr)->profile);
555         }
556         spin_unlock(&capi_users_lock);
557 }
558
559 /* -------- KCI_CONTRDOWN ------------------------------------- */
560
561 static void notify_down(__u32 contr)
562 {
563         struct capi_interface_user *p;
564         printk(KERN_NOTICE "kcapi: notify down contr %d\n", contr);
565         spin_lock(&capi_users_lock);
566         for (p = capi_users; p; p = p->next) {
567                 if (!p->callback) continue;
568                 (*p->callback) (KCI_CONTRDOWN, contr, 0);
569         }
570         spin_unlock(&capi_users_lock);
571 }
572
573 /* -------- KCI_NCCIUP ---------------------------------------- */
574
575 static void notify_ncciup(__u32 contr, __u16 applid, __u32 ncci)
576 {
577         struct capi_interface_user *p;
578         struct capi_ncciinfo n;
579         n.applid = applid;
580         n.ncci = ncci;
581         /*printk(KERN_NOTICE "kcapi: notify up contr %d\n", contr);*/
582         spin_lock(&capi_users_lock);
583         for (p = capi_users; p; p = p->next) {
584                 if (!p->callback) continue;
585                 (*p->callback) (KCI_NCCIUP, contr, &n);
586         }
587         spin_unlock(&capi_users_lock);
588 };
589
590 /* -------- KCI_NCCIDOWN -------------------------------------- */
591
592 static void notify_nccidown(__u32 contr, __u16 applid, __u32 ncci)
593 {
594         struct capi_interface_user *p;
595         struct capi_ncciinfo n;
596         n.applid = applid;
597         n.ncci = ncci;
598         /*printk(KERN_NOTICE "kcapi: notify down contr %d\n", contr);*/
599         spin_lock(&capi_users_lock);
600         for (p = capi_users; p; p = p->next) {
601                 if (!p->callback) continue;
602                 (*p->callback) (KCI_NCCIDOWN, contr, &n);
603         }
604         spin_unlock(&capi_users_lock);
605 };
606
607 /* ------------------------------------------------------------ */
608
609 static void inline notify_doit(struct capi_notifier *np)
610 {
611         switch (np->cmd) {
612                 case KCI_CONTRUP:
613                         notify_up(np->controller);
614                         break;
615                 case KCI_CONTRDOWN:
616                         notify_down(np->controller);
617                         break;
618                 case KCI_NCCIUP:
619                         notify_ncciup(np->controller, np->applid, np->ncci);
620                         break;
621                 case KCI_NCCIDOWN:
622                         notify_nccidown(np->controller, np->applid, np->ncci);
623                         break;
624         }
625 }
626
627 static void notify_handler(void *dummy)
628 {
629         struct capi_notifier *np;
630
631         while ((np = notify_dequeue()) != 0) {
632                 notify_doit(np);
633                 kfree(np);
634                 MOD_DEC_USE_COUNT;
635         }
636         MOD_DEC_USE_COUNT;
637 }
638         
639 /* -------- NCCI Handling ------------------------------------- */
640
641 static inline void mq_init(struct capi_ncci * np)
642 {
643         int i;
644         np->lock = SPIN_LOCK_UNLOCKED;
645         np->msgidqueue = 0;
646         np->msgidlast = 0;
647         np->nmsg = 0;
648         memset(np->msgidpool, 0, sizeof(np->msgidpool));
649         np->msgidfree = &np->msgidpool[0];
650         for (i = 1; i < np->winsize; i++) {
651                 np->msgidpool[i].next = np->msgidfree;
652                 np->msgidfree = &np->msgidpool[i];
653         }
654 }
655
656 static inline int mq_enqueue(struct capi_ncci * np, __u16 msgid)
657 {
658         struct msgidqueue *mq;
659         spin_lock_bh(&np->lock);
660         if ((mq = np->msgidfree) == 0) {
661                 spin_unlock_bh(&np->lock);
662                 return 0;
663         }
664         np->msgidfree = mq->next;
665         mq->msgid = msgid;
666         mq->next = 0;
667         if (np->msgidlast)
668                 np->msgidlast->next = mq;
669         np->msgidlast = mq;
670         if (!np->msgidqueue)
671                 np->msgidqueue = mq;
672         np->nmsg++;
673         spin_unlock_bh(&np->lock);
674         return 1;
675 }
676
677 static inline int mq_dequeue(struct capi_ncci * np, __u16 msgid)
678 {
679         struct msgidqueue **pp;
680         spin_lock_bh(&np->lock);
681         for (pp = &np->msgidqueue; *pp; pp = &(*pp)->next) {
682                 if ((*pp)->msgid == msgid) {
683                         struct msgidqueue *mq = *pp;
684                         *pp = mq->next;
685                         if (mq == np->msgidlast)
686                                 np->msgidlast = 0;
687                         mq->next = np->msgidfree;
688                         np->msgidfree = mq;
689                         np->nmsg--;
690                         spin_unlock_bh(&np->lock);
691                         return 1;
692                 }
693         }
694         spin_unlock_bh(&np->lock);
695         return 0;
696 }
697
698 static void controllercb_appl_registered(struct capi_ctr * card, __u16 appl)
699 {
700 }
701
702 static void controllercb_appl_released(struct capi_ctr * card, __u16 appl)
703 {
704         struct capi_ncci **pp, **nextpp;
705         for (pp = &APPL(appl)->nccilist; *pp; pp = nextpp) {
706                 if (NCCI2CTRL((*pp)->ncci) == card->cnr) {
707                         struct capi_ncci *np = *pp;
708                         *pp = np->next;
709                         printk(KERN_INFO "kcapi: appl %d ncci 0x%x down!\n", appl, np->ncci);
710                         kfree(np);
711                         APPL(appl)->nncci--;
712                         nextpp = pp;
713                 } else {
714                         nextpp = &(*pp)->next;
715                 }
716         }
717         APPL(appl)->releasing--;
718         if (APPL(appl)->releasing <= 0) {
719                 APPL(appl)->signal = 0;
720                 APPL_MARK_FREE(appl);
721                 printk(KERN_INFO "kcapi: appl %d down\n", appl);
722         }
723 }
724 /*
725  * ncci management
726  */
727
728 static void controllercb_new_ncci(struct capi_ctr * card,
729                                         __u16 appl, __u32 ncci, __u32 winsize)
730 {
731         struct capi_ncci *np;
732         if (!VALID_APPLID(appl)) {
733                 printk(KERN_ERR "avmb1_handle_new_ncci: illegal appl %d\n", appl);
734                 return;
735         }
736         if ((np = (struct capi_ncci *) kmalloc(sizeof(struct capi_ncci), GFP_ATOMIC)) == 0) {
737                 printk(KERN_ERR "capi_new_ncci: alloc failed ncci 0x%x\n", ncci);
738                 return;
739         }
740         if (winsize > CAPI_MAXDATAWINDOW) {
741                 printk(KERN_ERR "capi_new_ncci: winsize %d too big, set to %d\n",
742                        winsize, CAPI_MAXDATAWINDOW);
743                 winsize = CAPI_MAXDATAWINDOW;
744         }
745         np->applid = appl;
746         np->ncci = ncci;
747         np->winsize = winsize;
748         mq_init(np);
749         np->next = APPL(appl)->nccilist;
750         APPL(appl)->nccilist = np;
751         APPL(appl)->nncci++;
752         printk(KERN_INFO "kcapi: appl %d ncci 0x%x up\n", appl, ncci);
753
754         notify_push(KCI_NCCIUP, CARDNR(card), appl, ncci);
755 }
756
757 static void controllercb_free_ncci(struct capi_ctr * card,
758                                 __u16 appl, __u32 ncci)
759 {
760         struct capi_ncci **pp;
761         if (!VALID_APPLID(appl)) {
762                 printk(KERN_ERR "free_ncci: illegal appl %d\n", appl);
763                 return;
764         }
765         for (pp = &APPL(appl)->nccilist; *pp; pp = &(*pp)->next) {
766                 if ((*pp)->ncci == ncci) {
767                         struct capi_ncci *np = *pp;
768                         *pp = np->next;
769                         kfree(np);
770                         APPL(appl)->nncci--;
771                         printk(KERN_INFO "kcapi: appl %d ncci 0x%x down\n", appl, ncci);
772                         notify_push(KCI_NCCIDOWN, CARDNR(card), appl, ncci);
773                         return;
774                 }
775         }
776         printk(KERN_ERR "free_ncci: ncci 0x%x not found\n", ncci);
777 }
778
779
780 static struct capi_ncci *find_ncci(struct capi_appl * app, __u32 ncci)
781 {
782         struct capi_ncci *np;
783         for (np = app->nccilist; np; np = np->next) {
784                 if (np->ncci == ncci)
785                         return np;
786         }
787         return 0;
788 }
789
790 /* -------- Receiver ------------------------------------------ */
791
792 static void recv_handler(void *dummy)
793 {
794         struct sk_buff *skb;
795
796         while ((skb = skb_dequeue(&recv_queue)) != 0) {
797                 __u16 appl = CAPIMSG_APPID(skb->data);
798                 struct capi_ncci *np;
799                 if (!VALID_APPLID(appl)) {
800                         printk(KERN_ERR "kcapi: recv_handler: applid %d ? (%s)\n",
801                                appl, capi_message2str(skb->data));
802                         kfree_skb(skb);
803                         continue;
804                 }
805                 if (APPL(appl)->signal == 0) {
806                         printk(KERN_ERR "kcapi: recv_handler: applid %d has no signal function\n",
807                                appl);
808                         kfree_skb(skb);
809                         continue;
810                 }
811                 if (   CAPIMSG_COMMAND(skb->data) == CAPI_DATA_B3
812                     && CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF
813                     && (np = find_ncci(APPL(appl), CAPIMSG_NCCI(skb->data))) != 0
814                     && mq_dequeue(np, CAPIMSG_MSGID(skb->data)) == 0) {
815                         printk(KERN_ERR "kcapi: msgid %hu ncci 0x%x not on queue\n",
816                                 CAPIMSG_MSGID(skb->data), np->ncci);
817                 }
818                 if (   CAPIMSG_COMMAND(skb->data) == CAPI_DATA_B3
819                     && CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
820                         APPL(appl)->nrecvdatapkt++;
821                 } else {
822                         APPL(appl)->nrecvctlpkt++;
823                 }
824                 skb_queue_tail(&APPL(appl)->recv_queue, skb);
825                 (APPL(appl)->signal) (APPL(appl)->applid, APPL(appl)->param);
826         }
827 }
828
829 static void controllercb_handle_capimsg(struct capi_ctr * card,
830                                 __u16 appl, struct sk_buff *skb)
831 {
832         int showctl = 0;
833         __u8 cmd, subcmd;
834
835         if (card->cardstate != CARD_RUNNING) {
836                 printk(KERN_INFO "kcapi: controller %d not active, got: %s",
837                        card->cnr, capi_message2str(skb->data));
838                 goto error;
839         }
840         cmd = CAPIMSG_COMMAND(skb->data);
841         subcmd = CAPIMSG_SUBCOMMAND(skb->data);
842         if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
843                 card->nrecvdatapkt++;
844                 if (card->traceflag > 2) showctl |= 2;
845         } else {
846                 card->nrecvctlpkt++;
847                 if (card->traceflag) showctl |= 2;
848         }
849         showctl |= (card->traceflag & 1);
850         if (showctl & 2) {
851                 if (showctl & 1) {
852                         printk(KERN_DEBUG "kcapi: got [0x%lx] id#%d %s len=%u\n",
853                                (unsigned long) card->cnr,
854                                CAPIMSG_APPID(skb->data),
855                                capi_cmd2str(cmd, subcmd),
856                                CAPIMSG_LEN(skb->data));
857                 } else {
858                         printk(KERN_DEBUG "kcapi: got [0x%lx] %s\n",
859                                         (unsigned long) card->cnr,
860                                         capi_message2str(skb->data));
861                 }
862
863         }
864         skb_queue_tail(&recv_queue, skb);
865         queue_task(&tq_recv_notify, &tq_immediate);
866         mark_bh(IMMEDIATE_BH);
867         return;
868
869 error:
870         kfree_skb(skb);
871 }
872
873 static void controllercb_ready(struct capi_ctr * card)
874 {
875         __u16 appl;
876
877         card->cardstate = CARD_RUNNING;
878
879         for (appl = 1; appl <= CAPI_MAXAPPL; appl++) {
880                 if (!VALID_APPLID(appl)) continue;
881                 if (APPL(appl)->releasing) continue;
882                 card->driver->register_appl(card, appl, &APPL(appl)->rparam);
883         }
884
885         printk(KERN_NOTICE "kcapi: card %d \"%s\" ready.\n",
886                 CARDNR(card), card->name);
887
888         notify_push(KCI_CONTRUP, CARDNR(card), 0, 0);
889 }
890
891 static void controllercb_reseted(struct capi_ctr * card)
892 {
893         __u16 appl;
894
895         if (card->cardstate == CARD_FREE)
896                 return;
897         if (card->cardstate == CARD_DETECTED)
898                 return;
899
900         card->cardstate = CARD_DETECTED;
901
902         memset(card->manu, 0, sizeof(card->manu));
903         memset(&card->version, 0, sizeof(card->version));
904         memset(&card->profile, 0, sizeof(card->profile));
905         memset(card->serial, 0, sizeof(card->serial));
906
907         for (appl = 1; appl <= CAPI_MAXAPPL; appl++) {
908                 struct capi_ncci **pp, **nextpp;
909                 for (pp = &APPL(appl)->nccilist; *pp; pp = nextpp) {
910                         if (NCCI2CTRL((*pp)->ncci) == card->cnr) {
911                                 struct capi_ncci *np = *pp;
912                                 *pp = np->next;
913                                 printk(KERN_INFO "kcapi: appl %d ncci 0x%x forced down!\n", appl, np->ncci);
914                                 notify_push(KCI_NCCIDOWN, CARDNR(card), appl, np->ncci);
915                                 kfree(np);
916                                 nextpp = pp;
917                         } else {
918                                 nextpp = &(*pp)->next;
919                         }
920                 }
921         }
922
923         printk(KERN_NOTICE "kcapi: card %d down.\n", CARDNR(card));
924
925         notify_push(KCI_CONTRDOWN, CARDNR(card), 0, 0);
926 }
927
928 static void controllercb_suspend_output(struct capi_ctr *card)
929 {
930         if (!card->blocked) {
931                 printk(KERN_DEBUG "kcapi: card %d suspend\n", CARDNR(card));
932                 card->blocked = 1;
933         }
934 }
935
936 static void controllercb_resume_output(struct capi_ctr *card)
937 {
938         if (card->blocked) {
939                 printk(KERN_DEBUG "kcapi: card %d resume\n", CARDNR(card));
940                 card->blocked = 0;
941         }
942 }
943
944 /* ------------------------------------------------------------- */
945
946
947 struct capi_ctr *
948 drivercb_attach_ctr(struct capi_driver *driver, char *name, void *driverdata)
949 {
950         struct capi_ctr *card, **pp;
951         int i;
952
953         for (i=0; i < CAPI_MAXCONTR && cards[i].cardstate != CARD_FREE; i++) ;
954    
955         if (i == CAPI_MAXCONTR) {
956                 printk(KERN_ERR "kcapi: out of controller slots\n");
957                 return 0;
958         }
959         card = &cards[i];
960         memset(card, 0, sizeof(struct capi_ctr));
961         card->driver = driver;
962         card->cnr = CARDNR(card);
963         strncpy(card->name, name, sizeof(card->name));
964         card->cardstate = CARD_DETECTED;
965         card->blocked = 0;
966         card->driverdata = driverdata;
967         card->traceflag = showcapimsgs;
968
969         card->ready = controllercb_ready; 
970         card->reseted = controllercb_reseted; 
971         card->suspend_output = controllercb_suspend_output;
972         card->resume_output = controllercb_resume_output;
973         card->handle_capimsg = controllercb_handle_capimsg;
974         card->appl_registered = controllercb_appl_registered;
975         card->appl_released = controllercb_appl_released;
976         card->new_ncci = controllercb_new_ncci;
977         card->free_ncci = controllercb_free_ncci;
978
979         for (pp = &driver->controller; *pp; pp = &(*pp)->next) ;
980         card->next = 0;
981         *pp = card;
982         driver->ncontroller++;
983         sprintf(card->procfn, "capi/controllers/%d", card->cnr);
984         card->procent = create_proc_entry(card->procfn, 0, 0);
985         if (card->procent) {
986            card->procent->read_proc = 
987                 (int (*)(char *,char **,off_t,int,int *,void *))
988                         driver->ctr_read_proc;
989            card->procent->data = card;
990         }
991
992         ncards++;
993         printk(KERN_NOTICE "kcapi: Controller %d: %s attached\n",
994                         card->cnr, card->name);
995         return card;
996 }
997
998 static int drivercb_detach_ctr(struct capi_ctr *card)
999 {
1000         struct capi_driver *driver = card->driver;
1001         struct capi_ctr **pp;
1002
1003         if (card->cardstate == CARD_FREE)
1004                 return 0;
1005         if (card->cardstate != CARD_DETECTED)
1006                 controllercb_reseted(card);
1007         for (pp = &driver->controller; *pp ; pp = &(*pp)->next) {
1008                 if (*pp == card) {
1009                         *pp = card->next;
1010                         driver->ncontroller--;
1011                         ncards--;
1012                         break;
1013                 }
1014         }
1015         if (card->procent) {
1016            remove_proc_entry(card->procfn, 0);
1017            card->procent = 0;
1018         }
1019         card->cardstate = CARD_FREE;
1020         printk(KERN_NOTICE "kcapi: Controller %d: %s unregistered\n",
1021                         card->cnr, card->name);
1022         return 0;
1023 }
1024
1025 /* ------------------------------------------------------------- */
1026
1027 /* fallback if no driver read_proc function defined by driver */
1028
1029 static int driver_read_proc(char *page, char **start, off_t off,
1030                         int count, int *eof, void *data)
1031 {
1032         struct capi_driver *driver = (struct capi_driver *)data;
1033         int len = 0;
1034
1035         len += sprintf(page+len, "%-16s %s\n", "name", driver->name);
1036         len += sprintf(page+len, "%-16s %s\n", "revision", driver->revision);
1037
1038         if (len < off) 
1039            return 0;
1040         *eof = 1;
1041         *start = page + off;
1042         return ((count < len-off) ? count : len-off);
1043 }
1044
1045 /* ------------------------------------------------------------- */
1046
1047 static struct capi_driver_interface di = {
1048     drivercb_attach_ctr,
1049     drivercb_detach_ctr,
1050 };
1051
1052 struct capi_driver_interface *attach_capi_driver(struct capi_driver *driver)
1053 {
1054         struct capi_driver **pp;
1055
1056         MOD_INC_USE_COUNT;
1057         spin_lock(&drivers_lock);
1058         for (pp = &drivers; *pp; pp = &(*pp)->next) ;
1059         driver->next = 0;
1060         *pp = driver;
1061         spin_unlock(&drivers_lock);
1062         printk(KERN_NOTICE "kcapi: driver %s attached\n", driver->name);
1063         sprintf(driver->procfn, "capi/drivers/%s", driver->name);
1064         driver->procent = create_proc_entry(driver->procfn, 0, 0);
1065         if (driver->procent) {
1066            if (driver->driver_read_proc) {
1067                    driver->procent->read_proc = 
1068                         (int (*)(char *,char **,off_t,int,int *,void *))
1069                                         driver->driver_read_proc;
1070            } else {
1071                    driver->procent->read_proc = driver_read_proc;
1072            }
1073            driver->procent->data = driver;
1074         }
1075         return &di;
1076 }
1077
1078 void detach_capi_driver(struct capi_driver *driver)
1079 {
1080         struct capi_driver **pp;
1081         spin_lock(&drivers_lock);
1082         for (pp = &drivers; *pp && *pp != driver; pp = &(*pp)->next) ;
1083         if (*pp) {
1084                 *pp = (*pp)->next;
1085                 printk(KERN_NOTICE "kcapi: driver %s detached\n", driver->name);
1086         } else {
1087                 printk(KERN_ERR "kcapi: driver %s double detach ?\n", driver->name);
1088         }
1089         spin_unlock(&drivers_lock);
1090         if (driver->procent) {
1091            remove_proc_entry(driver->procfn, 0);
1092            driver->procent = 0;
1093         }
1094         MOD_DEC_USE_COUNT;
1095 }
1096
1097 /* ------------------------------------------------------------- */
1098 /* -------- CAPI2.0 Interface ---------------------------------- */
1099 /* ------------------------------------------------------------- */
1100
1101 static __u16 capi_isinstalled(void)
1102 {
1103         int i;
1104         for (i = 0; i < CAPI_MAXCONTR; i++) {
1105                 if (cards[i].cardstate == CARD_RUNNING)
1106                         return CAPI_NOERROR;
1107         }
1108         return CAPI_REGNOTINSTALLED;
1109 }
1110
1111 static __u16 capi_register(capi_register_params * rparam, __u16 * applidp)
1112 {
1113         int appl;
1114         int i;
1115
1116         if (rparam->datablklen < 128)
1117                 return CAPI_LOGBLKSIZETOSMALL;
1118
1119         for (appl = 1; appl <= CAPI_MAXAPPL; appl++) {
1120                 if (APPL_IS_FREE(appl))
1121                         break;
1122         }
1123         if (appl > CAPI_MAXAPPL)
1124                 return CAPI_TOOMANYAPPLS;
1125
1126         APPL_MARK_USED(appl);
1127         skb_queue_head_init(&APPL(appl)->recv_queue);
1128         APPL(appl)->nncci = 0;
1129
1130         memcpy(&APPL(appl)->rparam, rparam, sizeof(capi_register_params));
1131
1132         for (i = 0; i < CAPI_MAXCONTR; i++) {
1133                 if (cards[i].cardstate != CARD_RUNNING)
1134                         continue;
1135                 cards[i].driver->register_appl(&cards[i], appl,
1136                                                 &APPL(appl)->rparam);
1137         }
1138         *applidp = appl;
1139         printk(KERN_INFO "kcapi: appl %d up\n", appl);
1140
1141         return CAPI_NOERROR;
1142 }
1143
1144 static __u16 capi_release(__u16 applid)
1145 {
1146         int i;
1147
1148         if (!VALID_APPLID(applid) || APPL(applid)->releasing)
1149                 return CAPI_ILLAPPNR;
1150         APPL(applid)->releasing++;
1151         skb_queue_purge(&APPL(applid)->recv_queue);
1152         for (i = 0; i < CAPI_MAXCONTR; i++) {
1153                 if (cards[i].cardstate != CARD_RUNNING)
1154                         continue;
1155                 APPL(applid)->releasing++;
1156                 cards[i].driver->release_appl(&cards[i], applid);
1157         }
1158         APPL(applid)->releasing--;
1159         if (APPL(applid)->releasing <= 0) {
1160                 APPL(applid)->signal = 0;
1161                 APPL_MARK_FREE(applid);
1162                 printk(KERN_INFO "kcapi: appl %d down\n", applid);
1163         }
1164         return CAPI_NOERROR;
1165 }
1166
1167 static __u16 capi_put_message(__u16 applid, struct sk_buff *skb)
1168 {
1169         struct capi_ncci *np;
1170         __u32 contr;
1171         int showctl = 0;
1172         __u8 cmd, subcmd;
1173
1174         if (ncards == 0)
1175                 return CAPI_REGNOTINSTALLED;
1176         if (!VALID_APPLID(applid))
1177                 return CAPI_ILLAPPNR;
1178         if (skb->len < 12
1179             || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
1180             || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
1181                 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
1182         contr = CAPIMSG_CONTROLLER(skb->data);
1183         if (!VALID_CARD(contr) || CARD(contr)->cardstate != CARD_RUNNING) {
1184                 contr = 1;
1185                 if (CARD(contr)->cardstate != CARD_RUNNING) 
1186                         return CAPI_REGNOTINSTALLED;
1187         }
1188         if (CARD(contr)->blocked)
1189                 return CAPI_SENDQUEUEFULL;
1190
1191         cmd = CAPIMSG_COMMAND(skb->data);
1192         subcmd = CAPIMSG_SUBCOMMAND(skb->data);
1193
1194         if (cmd == CAPI_DATA_B3 && subcmd== CAPI_REQ) {
1195                 if ((np = find_ncci(APPL(applid), CAPIMSG_NCCI(skb->data))) != 0
1196                     && mq_enqueue(np, CAPIMSG_MSGID(skb->data)) == 0)
1197                         return CAPI_SENDQUEUEFULL;
1198                 CARD(contr)->nsentdatapkt++;
1199                 APPL(applid)->nsentdatapkt++;
1200                 if (CARD(contr)->traceflag > 2) showctl |= 2;
1201         } else {
1202                 CARD(contr)->nsentctlpkt++;
1203                 APPL(applid)->nsentctlpkt++;
1204                 if (CARD(contr)->traceflag) showctl |= 2;
1205         }
1206         showctl |= (CARD(contr)->traceflag & 1);
1207         if (showctl & 2) {
1208                 if (showctl & 1) {
1209                         printk(KERN_DEBUG "kcapi: put [0x%lx] id#%d %s len=%u\n",
1210                                (unsigned long) contr,
1211                                CAPIMSG_APPID(skb->data),
1212                                capi_cmd2str(cmd, subcmd),
1213                                CAPIMSG_LEN(skb->data));
1214                 } else {
1215                         printk(KERN_DEBUG "kcapi: put [0x%lx] %s\n",
1216                                         (unsigned long) contr,
1217                                         capi_message2str(skb->data));
1218                 }
1219
1220         }
1221         CARD(contr)->driver->send_message(CARD(contr), skb);
1222         return CAPI_NOERROR;
1223 }
1224
1225 static __u16 capi_get_message(__u16 applid, struct sk_buff **msgp)
1226 {
1227         struct sk_buff *skb;
1228
1229         if (!VALID_APPLID(applid))
1230                 return CAPI_ILLAPPNR;
1231         if ((skb = skb_dequeue(&APPL(applid)->recv_queue)) == 0)
1232                 return CAPI_RECEIVEQUEUEEMPTY;
1233         *msgp = skb;
1234         return CAPI_NOERROR;
1235 }
1236
1237 static __u16 capi_set_signal(__u16 applid,
1238                              void (*signal) (__u16 applid, void *param),
1239                              void *param)
1240 {
1241         if (!VALID_APPLID(applid))
1242                 return CAPI_ILLAPPNR;
1243         APPL(applid)->signal = signal;
1244         APPL(applid)->param = param;
1245         return CAPI_NOERROR;
1246 }
1247
1248 static __u16 capi_get_manufacturer(__u32 contr, __u8 buf[CAPI_MANUFACTURER_LEN])
1249 {
1250         if (contr == 0) {
1251                 strncpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
1252                 return CAPI_NOERROR;
1253         }
1254         if (!VALID_CARD(contr) || CARD(contr)->cardstate != CARD_RUNNING) 
1255                 return CAPI_REGNOTINSTALLED;
1256
1257         strncpy(buf, CARD(contr)->manu, CAPI_MANUFACTURER_LEN);
1258         return CAPI_NOERROR;
1259 }
1260
1261 static __u16 capi_get_version(__u32 contr, struct capi_version *verp)
1262 {
1263         if (contr == 0) {
1264                 *verp = driver_version;
1265                 return CAPI_NOERROR;
1266         }
1267         if (!VALID_CARD(contr) || CARD(contr)->cardstate != CARD_RUNNING) 
1268                 return CAPI_REGNOTINSTALLED;
1269
1270         memcpy((void *) verp, &CARD(contr)->version, sizeof(capi_version));
1271         return CAPI_NOERROR;
1272 }
1273
1274 static __u16 capi_get_serial(__u32 contr, __u8 serial[CAPI_SERIAL_LEN])
1275 {
1276         if (contr == 0) {
1277                 strncpy(serial, driver_serial, CAPI_SERIAL_LEN);
1278                 return CAPI_NOERROR;
1279         }
1280         if (!VALID_CARD(contr) || CARD(contr)->cardstate != CARD_RUNNING) 
1281                 return CAPI_REGNOTINSTALLED;
1282
1283         strncpy((void *) serial, CARD(contr)->serial, CAPI_SERIAL_LEN);
1284         return CAPI_NOERROR;
1285 }
1286
1287 static __u16 capi_get_profile(__u32 contr, struct capi_profile *profp)
1288 {
1289         if (contr == 0) {
1290                 profp->ncontroller = ncards;
1291                 return CAPI_NOERROR;
1292         }
1293         if (!VALID_CARD(contr) || CARD(contr)->cardstate != CARD_RUNNING) 
1294                 return CAPI_REGNOTINSTALLED;
1295
1296         memcpy((void *) profp, &CARD(contr)->profile,
1297                         sizeof(struct capi_profile));
1298         return CAPI_NOERROR;
1299 }
1300
1301 static struct capi_driver *find_driver(char *name)
1302 {
1303         struct capi_driver *dp;
1304         spin_lock(&drivers_lock);
1305         for (dp = drivers; dp; dp = dp->next)
1306                 if (strcmp(dp->name, name) == 0)
1307                         break;
1308         spin_unlock(&drivers_lock);
1309         return dp;
1310 }
1311
1312 #ifdef CONFIG_AVMB1_COMPAT
1313 static int old_capi_manufacturer(unsigned int cmd, void *data)
1314 {
1315         avmb1_loadandconfigdef ldef;
1316         avmb1_extcarddef cdef;
1317         avmb1_resetdef rdef;
1318         avmb1_getdef gdef;
1319         struct capi_driver *driver;
1320         struct capi_ctr *card;
1321         capicardparams cparams;
1322         capiloaddata ldata;
1323         int retval;
1324
1325         switch (cmd) {
1326         case AVMB1_ADDCARD:
1327         case AVMB1_ADDCARD_WITH_TYPE:
1328                 if (cmd == AVMB1_ADDCARD) {
1329                    if ((retval = copy_from_user((void *) &cdef, data,
1330                                             sizeof(avmb1_carddef))))
1331                            return retval;
1332                    cdef.cardtype = AVM_CARDTYPE_B1;
1333                 } else {
1334                    if ((retval = copy_from_user((void *) &cdef, data,
1335                                             sizeof(avmb1_extcarddef))))
1336                            return retval;
1337                 }
1338                 cparams.port = cdef.port;
1339                 cparams.irq = cdef.irq;
1340                 cparams.cardnr = cdef.cardnr;
1341
1342                 switch (cdef.cardtype) {
1343                         case AVM_CARDTYPE_B1:
1344                                 driver = find_driver("b1isa");
1345                                 break;
1346                         case AVM_CARDTYPE_T1:
1347                                 driver = find_driver("t1isa");
1348                                 break;
1349                         default:
1350                                 driver = 0;
1351                                 break;
1352                 }
1353                 if (!driver) {
1354                         printk(KERN_ERR "kcapi: driver not loaded.\n");
1355                         return -EIO;
1356                 }
1357                 if (!driver->add_card) {
1358                         printk(KERN_ERR "kcapi: driver has no add card function.\n");
1359                         return -EIO;
1360                 }
1361
1362                 return driver->add_card(driver, &cparams);
1363
1364         case AVMB1_LOAD:
1365         case AVMB1_LOAD_AND_CONFIG:
1366
1367                 if (cmd == AVMB1_LOAD) {
1368                         if ((retval = copy_from_user((void *) &ldef, data,
1369                                                 sizeof(avmb1_loaddef))))
1370                                 return retval;
1371                         ldef.t4config.len = 0;
1372                         ldef.t4config.data = 0;
1373                 } else {
1374                         if ((retval = copy_from_user((void *) &ldef, data,
1375                                                 sizeof(avmb1_loadandconfigdef))))
1376                                 return retval;
1377                 }
1378                 if (!VALID_CARD(ldef.contr))
1379                         return -ESRCH;
1380
1381                 card = CARD(ldef.contr);
1382                 if (card->cardstate == CARD_FREE)
1383                         return -ESRCH;
1384                 if (card->driver->load_firmware == 0) {
1385                         printk(KERN_DEBUG "kcapi: load: driver \%s\" has no load function\n", card->driver->name);
1386                         return -ESRCH;
1387                 }
1388
1389                 if (ldef.t4file.len <= 0) {
1390                         printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
1391                         return -EINVAL;
1392                 }
1393                 if (ldef.t4file.data == 0) {
1394                         printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
1395                         return -EINVAL;
1396                 }
1397
1398                 ldata.firmware.user = 1;
1399                 ldata.firmware.data = ldef.t4file.data;
1400                 ldata.firmware.len = ldef.t4file.len;
1401                 ldata.configuration.user = 1;
1402                 ldata.configuration.data = ldef.t4config.data;
1403                 ldata.configuration.len = ldef.t4config.len;
1404
1405                 if (card->cardstate != CARD_DETECTED) {
1406                         printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
1407                         return -EBUSY;
1408                 }
1409                 card->cardstate = CARD_LOADING;
1410
1411                 retval = card->driver->load_firmware(card, &ldata);
1412
1413                 if (retval) {
1414                         card->cardstate = CARD_DETECTED;
1415                         return retval;
1416                 }
1417
1418                 while (card->cardstate != CARD_RUNNING) {
1419
1420                         set_current_state(TASK_INTERRUPTIBLE);
1421                         schedule_timeout(HZ/10);        /* 0.1 sec */
1422
1423                         if (signal_pending(current))
1424                                 return -EINTR;
1425                 }
1426                 return 0;
1427
1428         case AVMB1_RESETCARD:
1429                 if ((retval = copy_from_user((void *) &rdef, data,
1430                                          sizeof(avmb1_resetdef))))
1431                         return retval;
1432                 if (!VALID_CARD(rdef.contr))
1433                         return -ESRCH;
1434                 card = CARD(rdef.contr);
1435
1436                 if (card->cardstate == CARD_FREE)
1437                         return -ESRCH;
1438                 if (card->cardstate == CARD_DETECTED)
1439                         return 0;
1440
1441                 card->driver->reset_ctr(card);
1442
1443                 while (card->cardstate > CARD_DETECTED) {
1444
1445                         set_current_state(TASK_INTERRUPTIBLE);
1446                         schedule_timeout(HZ/10);        /* 0.1 sec */
1447
1448                         if (signal_pending(current))
1449                                 return -EINTR;
1450                 }
1451                 return 0;
1452
1453         case AVMB1_GET_CARDINFO:
1454                 if ((retval = copy_from_user((void *) &gdef, data,
1455                                          sizeof(avmb1_getdef))))
1456                         return retval;
1457
1458                 if (!VALID_CARD(gdef.contr))
1459                         return -ESRCH;
1460
1461                 card = CARD(gdef.contr);
1462
1463                 if (card->cardstate == CARD_FREE)
1464                         return -ESRCH;
1465
1466                 gdef.cardstate = card->cardstate;
1467                 if (card->driver == find_driver("t1isa"))
1468                         gdef.cardtype = AVM_CARDTYPE_T1;
1469                 else gdef.cardtype = AVM_CARDTYPE_B1;
1470
1471                 if ((retval = copy_to_user(data, (void *) &gdef,
1472                                          sizeof(avmb1_getdef))))
1473                         return retval;
1474
1475                 return 0;
1476
1477         case AVMB1_REMOVECARD:
1478                 if ((retval = copy_from_user((void *) &rdef, data,
1479                                          sizeof(avmb1_resetdef))))
1480                         return retval;
1481
1482                 if (!VALID_CARD(rdef.contr))
1483                         return -ESRCH;
1484                 card = CARD(rdef.contr);
1485
1486                 if (card->cardstate == CARD_FREE)
1487                         return -ESRCH;
1488
1489                 if (card->cardstate != CARD_DETECTED)
1490                         return -EBUSY;
1491
1492                 card->driver->remove_ctr(card);
1493
1494                 while (card->cardstate != CARD_FREE) {
1495
1496                         set_current_state(TASK_INTERRUPTIBLE);
1497                         schedule_timeout(HZ/10);        /* 0.1 sec */
1498
1499                         if (signal_pending(current))
1500                                 return -EINTR;
1501                 }
1502                 return 0;
1503         }
1504         return -EINVAL;
1505 }
1506 #endif
1507
1508 static int capi_manufacturer(unsigned int cmd, void *data)
1509 {
1510         struct capi_ctr *card;
1511         int retval;
1512
1513         switch (cmd) {
1514 #ifdef CONFIG_AVMB1_COMPAT
1515         case AVMB1_ADDCARD:
1516         case AVMB1_ADDCARD_WITH_TYPE:
1517         case AVMB1_LOAD:
1518         case AVMB1_LOAD_AND_CONFIG:
1519         case AVMB1_RESETCARD:
1520         case AVMB1_GET_CARDINFO:
1521         case AVMB1_REMOVECARD:
1522                 return old_capi_manufacturer(cmd, data);
1523 #endif
1524         case KCAPI_CMD_TRACE:
1525         {
1526                 kcapi_flagdef fdef;
1527
1528                 if ((retval = copy_from_user((void *) &fdef, data,
1529                                          sizeof(kcapi_flagdef))))
1530                         return retval;
1531
1532                 if (!VALID_CARD(fdef.contr))
1533                         return -ESRCH;
1534                 card = CARD(fdef.contr);
1535                 if (card->cardstate == CARD_FREE)
1536                         return -ESRCH;
1537                 card->traceflag = fdef.flag;
1538                 printk(KERN_INFO "kcapi: contr %d set trace=%d\n",
1539                         card->cnr, card->traceflag);
1540                 return 0;
1541         }
1542
1543         case KCAPI_CMD_ADDCARD:
1544         {
1545                 struct capi_driver *driver;
1546                 capicardparams cparams;
1547                 kcapi_carddef cdef;
1548
1549                 if ((retval = copy_from_user((void *) &cdef, data,
1550                                                         sizeof(cdef))))
1551                         return retval;
1552
1553                 cparams.port = cdef.port;
1554                 cparams.irq = cdef.irq;
1555                 cparams.membase = cdef.membase;
1556                 cparams.cardnr = cdef.cardnr;
1557                 cparams.cardtype = 0;
1558                 cdef.driver[sizeof(cdef.driver)-1] = 0;
1559
1560                 if ((driver = find_driver(cdef.driver)) == 0) {
1561                         printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
1562                                         cdef.driver);
1563                         return -ESRCH;
1564                 }
1565
1566                 if (!driver->add_card) {
1567                         printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
1568                         return -EIO;
1569                 }
1570
1571                 return driver->add_card(driver, &cparams);
1572         }
1573
1574         default:
1575                 printk(KERN_ERR "kcapi: manufacturer command %d unknown.\n",
1576                                         cmd);
1577                 break;
1578
1579         }
1580         return -EINVAL;
1581 }
1582
1583 struct capi_interface avmb1_interface =
1584 {
1585         capi_isinstalled,
1586         capi_register,
1587         capi_release,
1588         capi_put_message,
1589         capi_get_message,
1590         capi_set_signal,
1591         capi_get_manufacturer,
1592         capi_get_version,
1593         capi_get_serial,
1594         capi_get_profile,
1595         capi_manufacturer
1596 };
1597
1598 /* ------------------------------------------------------------- */
1599 /* -------- Exported Functions --------------------------------- */
1600 /* ------------------------------------------------------------- */
1601
1602 struct capi_interface *attach_capi_interface(struct capi_interface_user *userp)
1603 {
1604         struct capi_interface_user *p;
1605
1606         MOD_INC_USE_COUNT;
1607         spin_lock(&capi_users_lock);
1608         for (p = capi_users; p; p = p->next) {
1609                 if (p == userp) {
1610                         spin_unlock(&capi_users_lock);
1611                         printk(KERN_ERR "kcapi: double attach from %s\n",
1612                                userp->name);
1613                         MOD_DEC_USE_COUNT;
1614                         return 0;
1615                 }
1616         }
1617         userp->next = capi_users;
1618         capi_users = userp;
1619         spin_unlock(&capi_users_lock);
1620         printk(KERN_NOTICE "kcapi: %s attached\n", userp->name);
1621
1622         return &avmb1_interface;
1623 }
1624
1625 int detach_capi_interface(struct capi_interface_user *userp)
1626 {
1627         struct capi_interface_user **pp;
1628
1629         spin_lock(&capi_users_lock);
1630         for (pp = &capi_users; *pp; pp = &(*pp)->next) {
1631                 if (*pp == userp) {
1632                         *pp = userp->next;
1633                         spin_unlock(&capi_users_lock);
1634                         userp->next = 0;
1635                         printk(KERN_NOTICE "kcapi: %s detached\n", userp->name);
1636                         MOD_DEC_USE_COUNT;
1637                         return 0;
1638                 }
1639         }
1640         spin_unlock(&capi_users_lock);
1641         printk(KERN_ERR "kcapi: double detach from %s\n", userp->name);
1642         return -1;
1643 }
1644
1645 /* ------------------------------------------------------------- */
1646 /* -------- Init & Cleanup ------------------------------------- */
1647 /* ------------------------------------------------------------- */
1648
1649 EXPORT_SYMBOL(attach_capi_interface);
1650 EXPORT_SYMBOL(detach_capi_interface);
1651 EXPORT_SYMBOL(attach_capi_driver);
1652 EXPORT_SYMBOL(detach_capi_driver);
1653
1654 /*
1655  * init / exit functions
1656  */
1657
1658 static int __init kcapi_init(void)
1659 {
1660         char *p;
1661         char rev[32];
1662
1663         MOD_INC_USE_COUNT;
1664
1665         skb_queue_head_init(&recv_queue);
1666
1667         tq_state_notify.routine = notify_handler;
1668         tq_state_notify.data = 0;
1669
1670         tq_recv_notify.routine = recv_handler;
1671         tq_recv_notify.data = 0;
1672
1673         proc_capi_init();
1674
1675         if ((p = strchr(revision, ':')) != 0 && p[1]) {
1676                 strncpy(rev, p + 2, sizeof(rev));
1677                 rev[sizeof(rev)-1] = 0;
1678                 if ((p = strchr(rev, '$')) != 0 && p > rev)
1679                    *(p-1) = 0;
1680         } else
1681                 strcpy(rev, "1.0");
1682
1683 #ifdef MODULE
1684         printk(KERN_NOTICE "CAPI-driver Rev %s: loaded\n", rev);
1685 #else
1686         printk(KERN_NOTICE "CAPI-driver Rev %s: started\n", rev);
1687 #endif
1688         MOD_DEC_USE_COUNT;
1689         return 0;
1690 }
1691
1692 static void __exit kcapi_exit(void)
1693 {
1694         char rev[10];
1695         char *p;
1696
1697         if ((p = strchr(revision, ':'))) {
1698                 strcpy(rev, p + 1);
1699                 p = strchr(rev, '$');
1700                 *p = 0;
1701         } else {
1702                 strcpy(rev, "1.0");
1703         }
1704
1705         proc_capi_exit();
1706         printk(KERN_NOTICE "CAPI-driver Rev%s: unloaded\n", rev);
1707 }
1708
1709 module_init(kcapi_init);
1710 module_exit(kcapi_exit);