Merge branch 'for-linus' of git://neil.brown.name/md
[linux-2.6] / drivers / isdn / capi / kcapi.c
1 /* $Id: kcapi.c,v 1.1.2.8 2004/03/26 19:57:20 armin Exp $
2  * 
3  * Kernel CAPI 2.0 Module
4  * 
5  * Copyright 1999 by Carsten Paeth <calle@calle.de>
6  * Copyright 2002 by Kai Germaschewski <kai@germaschewski.name>
7  * 
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  *
11  */
12
13 #define AVMB1_COMPAT
14
15 #include "kcapi.h"
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/skbuff.h>
23 #include <linux/workqueue.h>
24 #include <linux/capi.h>
25 #include <linux/kernelcapi.h>
26 #include <linux/init.h>
27 #include <linux/moduleparam.h>
28 #include <linux/delay.h>
29 #include <asm/uaccess.h>
30 #include <linux/isdn/capicmd.h>
31 #include <linux/isdn/capiutil.h>
32 #ifdef AVMB1_COMPAT
33 #include <linux/b1lli.h>
34 #endif
35 #include <linux/mutex.h>
36
37 static char *revision = "$Revision: 1.1.2.8 $";
38
39 /* ------------------------------------------------------------- */
40
41 static int showcapimsgs = 0;
42
43 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
44 MODULE_AUTHOR("Carsten Paeth");
45 MODULE_LICENSE("GPL");
46 module_param(showcapimsgs, uint, 0);
47
48 /* ------------------------------------------------------------- */
49
50 struct capi_notifier {
51         struct work_struct work;
52         unsigned int cmd;
53         u32 controller;
54         u16 applid;
55         u32 ncci;
56 };
57
58 /* ------------------------------------------------------------- */
59
60 static struct capi_version driver_version = {2, 0, 1, 1<<4};
61 static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
62 static char capi_manufakturer[64] = "AVM Berlin";
63
64 #define NCCI2CTRL(ncci)    (((ncci) >> 24) & 0x7f)
65
66 LIST_HEAD(capi_drivers);
67 DEFINE_RWLOCK(capi_drivers_list_lock);
68
69 static DEFINE_RWLOCK(application_lock);
70 static DEFINE_MUTEX(controller_mutex);
71
72 struct capi20_appl *capi_applications[CAPI_MAXAPPL];
73 struct capi_ctr *capi_cards[CAPI_MAXCONTR];
74
75 static int ncards;
76
77 /* -------- controller ref counting -------------------------------------- */
78
79 static inline struct capi_ctr *
80 capi_ctr_get(struct capi_ctr *card)
81 {
82         if (!try_module_get(card->owner))
83                 return NULL;
84         return card;
85 }
86
87 static inline void
88 capi_ctr_put(struct capi_ctr *card)
89 {
90         module_put(card->owner);
91 }
92
93 /* ------------------------------------------------------------- */
94
95 static inline struct capi_ctr *get_capi_ctr_by_nr(u16 contr)
96 {
97         if (contr - 1 >= CAPI_MAXCONTR)
98                 return NULL;
99
100         return capi_cards[contr - 1];
101 }
102
103 static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid)
104 {
105         if (applid - 1 >= CAPI_MAXAPPL)
106                 return NULL;
107
108         return capi_applications[applid - 1];
109 }
110
111 /* -------- util functions ------------------------------------ */
112
113 static inline int capi_cmd_valid(u8 cmd)
114 {
115         switch (cmd) {
116         case CAPI_ALERT:
117         case CAPI_CONNECT:
118         case CAPI_CONNECT_ACTIVE:
119         case CAPI_CONNECT_B3_ACTIVE:
120         case CAPI_CONNECT_B3:
121         case CAPI_CONNECT_B3_T90_ACTIVE:
122         case CAPI_DATA_B3:
123         case CAPI_DISCONNECT_B3:
124         case CAPI_DISCONNECT:
125         case CAPI_FACILITY:
126         case CAPI_INFO:
127         case CAPI_LISTEN:
128         case CAPI_MANUFACTURER:
129         case CAPI_RESET_B3:
130         case CAPI_SELECT_B_PROTOCOL:
131                 return 1;
132         }
133         return 0;
134 }
135
136 static inline int capi_subcmd_valid(u8 subcmd)
137 {
138         switch (subcmd) {
139         case CAPI_REQ:
140         case CAPI_CONF:
141         case CAPI_IND:
142         case CAPI_RESP:
143                 return 1;
144         }
145         return 0;
146 }
147
148 /* ------------------------------------------------------------ */
149
150 static void register_appl(struct capi_ctr *card, u16 applid, capi_register_params *rparam)
151 {
152         card = capi_ctr_get(card);
153
154         if (card)
155                 card->register_appl(card, applid, rparam);
156         else
157                 printk(KERN_WARNING "%s: cannot get card resources\n", __func__);
158 }
159
160
161 static void release_appl(struct capi_ctr *card, u16 applid)
162 {
163         DBG("applid %#x", applid);
164         
165         card->release_appl(card, applid);
166         capi_ctr_put(card);
167 }
168
169 /* -------- KCI_CONTRUP --------------------------------------- */
170
171 static void notify_up(u32 contr)
172 {
173         struct capi_ctr *card = get_capi_ctr_by_nr(contr);
174         struct capi20_appl *ap;
175         u16 applid;
176
177         if (showcapimsgs & 1) {
178                 printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr);
179         }
180         if (!card) {
181                 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
182                 return;
183         }
184         for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
185                 ap = get_capi_appl_by_nr(applid);
186                 if (!ap || ap->release_in_progress) continue;
187                 register_appl(card, applid, &ap->rparam);
188                 if (ap->callback && !ap->release_in_progress)
189                         ap->callback(KCI_CONTRUP, contr, &card->profile);
190         }
191 }
192
193 /* -------- KCI_CONTRDOWN ------------------------------------- */
194
195 static void notify_down(u32 contr)
196 {
197         struct capi20_appl *ap;
198         u16 applid;
199
200         if (showcapimsgs & 1) {
201                 printk(KERN_DEBUG "kcapi: notify down contr %d\n", contr);
202         }
203
204         for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
205                 ap = get_capi_appl_by_nr(applid);
206                 if (ap && ap->callback && !ap->release_in_progress)
207                         ap->callback(KCI_CONTRDOWN, contr, NULL);
208         }
209 }
210
211 static void notify_handler(struct work_struct *work)
212 {
213         struct capi_notifier *np =
214                 container_of(work, struct capi_notifier, work);
215
216         switch (np->cmd) {
217         case KCI_CONTRUP:
218                 notify_up(np->controller);
219                 break;
220         case KCI_CONTRDOWN:
221                 notify_down(np->controller);
222                 break;
223         }
224
225         kfree(np);
226 }
227
228 /*
229  * The notifier will result in adding/deleteing of devices. Devices can
230  * only removed in user process, not in bh.
231  */
232 static int notify_push(unsigned int cmd, u32 controller, u16 applid, u32 ncci)
233 {
234         struct capi_notifier *np = kmalloc(sizeof(*np), GFP_ATOMIC);
235
236         if (!np)
237                 return -ENOMEM;
238
239         INIT_WORK(&np->work, notify_handler);
240         np->cmd = cmd;
241         np->controller = controller;
242         np->applid = applid;
243         np->ncci = ncci;
244
245         schedule_work(&np->work);
246         return 0;
247 }
248
249         
250 /* -------- Receiver ------------------------------------------ */
251
252 static void recv_handler(struct work_struct *work)
253 {
254         struct sk_buff *skb;
255         struct capi20_appl *ap =
256                 container_of(work, struct capi20_appl, recv_work);
257
258         if ((!ap) || (ap->release_in_progress))
259                 return;
260
261         mutex_lock(&ap->recv_mtx);
262         while ((skb = skb_dequeue(&ap->recv_queue))) {
263                 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND)
264                         ap->nrecvdatapkt++;
265                 else
266                         ap->nrecvctlpkt++;
267
268                 ap->recv_message(ap, skb);
269         }
270         mutex_unlock(&ap->recv_mtx);
271 }
272
273 /**
274  * capi_ctr_handle_message() - handle incoming CAPI message
275  * @card:       controller descriptor structure.
276  * @appl:       application ID.
277  * @skb:        message.
278  *
279  * Called by hardware driver to pass a CAPI message to the application.
280  */
281
282 void capi_ctr_handle_message(struct capi_ctr * card, u16 appl, struct sk_buff *skb)
283 {
284         struct capi20_appl *ap;
285         int showctl = 0;
286         u8 cmd, subcmd;
287         unsigned long flags;
288         _cdebbuf *cdb;
289
290         if (card->cardstate != CARD_RUNNING) {
291                 cdb = capi_message2str(skb->data);
292                 if (cdb) {
293                         printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
294                                 card->cnr, cdb->buf);
295                         cdebbuf_free(cdb);
296                 } else
297                         printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
298                                 card->cnr);
299                 goto error;
300         }
301
302         cmd = CAPIMSG_COMMAND(skb->data);
303         subcmd = CAPIMSG_SUBCOMMAND(skb->data);
304         if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
305                 card->nrecvdatapkt++;
306                 if (card->traceflag > 2) showctl |= 2;
307         } else {
308                 card->nrecvctlpkt++;
309                 if (card->traceflag) showctl |= 2;
310         }
311         showctl |= (card->traceflag & 1);
312         if (showctl & 2) {
313                 if (showctl & 1) {
314                         printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
315                                card->cnr, CAPIMSG_APPID(skb->data),
316                                capi_cmd2str(cmd, subcmd),
317                                CAPIMSG_LEN(skb->data));
318                 } else {
319                         cdb = capi_message2str(skb->data);
320                         if (cdb) {
321                                 printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
322                                         card->cnr, cdb->buf);
323                                 cdebbuf_free(cdb);
324                         } else
325                                 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
326                                         card->cnr, CAPIMSG_APPID(skb->data),
327                                         capi_cmd2str(cmd, subcmd),
328                                         CAPIMSG_LEN(skb->data));
329                 }
330
331         }
332
333         read_lock_irqsave(&application_lock, flags);
334         ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
335         if ((!ap) || (ap->release_in_progress)) {
336                 read_unlock_irqrestore(&application_lock, flags);
337                 cdb = capi_message2str(skb->data);
338                 if (cdb) {
339                         printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
340                         CAPIMSG_APPID(skb->data), cdb->buf);
341                         cdebbuf_free(cdb);
342                 } else
343                         printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
344                                 CAPIMSG_APPID(skb->data),
345                                 capi_cmd2str(cmd, subcmd));
346                 goto error;
347         }
348         skb_queue_tail(&ap->recv_queue, skb);
349         schedule_work(&ap->recv_work);
350         read_unlock_irqrestore(&application_lock, flags);
351
352         return;
353
354 error:
355         kfree_skb(skb);
356 }
357
358 EXPORT_SYMBOL(capi_ctr_handle_message);
359
360 /**
361  * capi_ctr_ready() - signal CAPI controller ready
362  * @card:       controller descriptor structure.
363  *
364  * Called by hardware driver to signal that the controller is up and running.
365  */
366
367 void capi_ctr_ready(struct capi_ctr * card)
368 {
369         card->cardstate = CARD_RUNNING;
370
371         printk(KERN_NOTICE "kcapi: card [%03d] \"%s\" ready.\n",
372                card->cnr, card->name);
373
374         notify_push(KCI_CONTRUP, card->cnr, 0, 0);
375 }
376
377 EXPORT_SYMBOL(capi_ctr_ready);
378
379 /**
380  * capi_ctr_down() - signal CAPI controller not ready
381  * @card:       controller descriptor structure.
382  *
383  * Called by hardware driver to signal that the controller is down and
384  * unavailable for use.
385  */
386
387 void capi_ctr_down(struct capi_ctr * card)
388 {
389         u16 appl;
390
391         DBG("");
392
393         if (card->cardstate == CARD_DETECTED)
394                 return;
395
396         card->cardstate = CARD_DETECTED;
397
398         memset(card->manu, 0, sizeof(card->manu));
399         memset(&card->version, 0, sizeof(card->version));
400         memset(&card->profile, 0, sizeof(card->profile));
401         memset(card->serial, 0, sizeof(card->serial));
402
403         for (appl = 1; appl <= CAPI_MAXAPPL; appl++) {
404                 struct capi20_appl *ap = get_capi_appl_by_nr(appl);
405                 if (!ap || ap->release_in_progress)
406                         continue;
407
408                 capi_ctr_put(card);
409         }
410
411         printk(KERN_NOTICE "kcapi: card [%03d] down.\n", card->cnr);
412
413         notify_push(KCI_CONTRDOWN, card->cnr, 0, 0);
414 }
415
416 EXPORT_SYMBOL(capi_ctr_down);
417
418 /**
419  * capi_ctr_suspend_output() - suspend controller
420  * @card:       controller descriptor structure.
421  *
422  * Called by hardware driver to stop data flow.
423  */
424
425 void capi_ctr_suspend_output(struct capi_ctr *card)
426 {
427         if (!card->blocked) {
428                 printk(KERN_DEBUG "kcapi: card [%03d] suspend\n", card->cnr);
429                 card->blocked = 1;
430         }
431 }
432
433 EXPORT_SYMBOL(capi_ctr_suspend_output);
434
435 /**
436  * capi_ctr_resume_output() - resume controller
437  * @card:       controller descriptor structure.
438  *
439  * Called by hardware driver to resume data flow.
440  */
441
442 void capi_ctr_resume_output(struct capi_ctr *card)
443 {
444         if (card->blocked) {
445                 printk(KERN_DEBUG "kcapi: card [%03d] resume\n", card->cnr);
446                 card->blocked = 0;
447         }
448 }
449
450 EXPORT_SYMBOL(capi_ctr_resume_output);
451
452 /* ------------------------------------------------------------- */
453
454 /**
455  * attach_capi_ctr() - register CAPI controller
456  * @card:       controller descriptor structure.
457  *
458  * Called by hardware driver to register a controller with the CAPI subsystem.
459  * Return value: 0 on success, error code < 0 on error
460  */
461
462 int
463 attach_capi_ctr(struct capi_ctr *card)
464 {
465         int i;
466
467         mutex_lock(&controller_mutex);
468
469         for (i = 0; i < CAPI_MAXCONTR; i++) {
470                 if (capi_cards[i] == NULL)
471                         break;
472         }
473         if (i == CAPI_MAXCONTR) {
474                 mutex_unlock(&controller_mutex);
475                 printk(KERN_ERR "kcapi: out of controller slots\n");
476                 return -EBUSY;
477         }
478         capi_cards[i] = card;
479
480         mutex_unlock(&controller_mutex);
481
482         card->nrecvctlpkt = 0;
483         card->nrecvdatapkt = 0;
484         card->nsentctlpkt = 0;
485         card->nsentdatapkt = 0;
486         card->cnr = i + 1;
487         card->cardstate = CARD_DETECTED;
488         card->blocked = 0;
489         card->traceflag = showcapimsgs;
490
491         sprintf(card->procfn, "capi/controllers/%d", card->cnr);
492         card->procent = create_proc_entry(card->procfn, 0, NULL);
493         if (card->procent) {
494            card->procent->read_proc = 
495                 (int (*)(char *,char **,off_t,int,int *,void *))
496                         card->ctr_read_proc;
497            card->procent->data = card;
498         }
499
500         ncards++;
501         printk(KERN_NOTICE "kcapi: Controller [%03d]: %s attached\n",
502                         card->cnr, card->name);
503         return 0;
504 }
505
506 EXPORT_SYMBOL(attach_capi_ctr);
507
508 /**
509  * detach_capi_ctr() - unregister CAPI controller
510  * @card:       controller descriptor structure.
511  *
512  * Called by hardware driver to remove the registration of a controller
513  * with the CAPI subsystem.
514  * Return value: 0 on success, error code < 0 on error
515  */
516
517 int detach_capi_ctr(struct capi_ctr *card)
518 {
519         if (card->cardstate != CARD_DETECTED)
520                 capi_ctr_down(card);
521
522         ncards--;
523
524         if (card->procent) {
525            remove_proc_entry(card->procfn, NULL);
526            card->procent = NULL;
527         }
528         capi_cards[card->cnr - 1] = NULL;
529         printk(KERN_NOTICE "kcapi: Controller [%03d]: %s unregistered\n",
530                         card->cnr, card->name);
531
532         return 0;
533 }
534
535 EXPORT_SYMBOL(detach_capi_ctr);
536
537 /**
538  * register_capi_driver() - register CAPI driver
539  * @driver:     driver descriptor structure.
540  *
541  * Called by hardware driver to register itself with the CAPI subsystem.
542  */
543
544 void register_capi_driver(struct capi_driver *driver)
545 {
546         unsigned long flags;
547
548         write_lock_irqsave(&capi_drivers_list_lock, flags);
549         list_add_tail(&driver->list, &capi_drivers);
550         write_unlock_irqrestore(&capi_drivers_list_lock, flags);
551 }
552
553 EXPORT_SYMBOL(register_capi_driver);
554
555 /**
556  * unregister_capi_driver() - unregister CAPI driver
557  * @driver:     driver descriptor structure.
558  *
559  * Called by hardware driver to unregister itself from the CAPI subsystem.
560  */
561
562 void unregister_capi_driver(struct capi_driver *driver)
563 {
564         unsigned long flags;
565
566         write_lock_irqsave(&capi_drivers_list_lock, flags);
567         list_del(&driver->list);
568         write_unlock_irqrestore(&capi_drivers_list_lock, flags);
569 }
570
571 EXPORT_SYMBOL(unregister_capi_driver);
572
573 /* ------------------------------------------------------------- */
574 /* -------- CAPI2.0 Interface ---------------------------------- */
575 /* ------------------------------------------------------------- */
576
577 /**
578  * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
579  *
580  * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
581  *      is ready for use, CAPI_REGNOTINSTALLED otherwise)
582  */
583
584 u16 capi20_isinstalled(void)
585 {
586         int i;
587         for (i = 0; i < CAPI_MAXCONTR; i++) {
588                 if (capi_cards[i] && capi_cards[i]->cardstate == CARD_RUNNING)
589                         return CAPI_NOERROR;
590         }
591         return CAPI_REGNOTINSTALLED;
592 }
593
594 EXPORT_SYMBOL(capi20_isinstalled);
595
596 /**
597  * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
598  * @ap:         CAPI application descriptor structure.
599  *
600  * Register an application's presence with CAPI.
601  * A unique application ID is assigned and stored in @ap->applid.
602  * After this function returns successfully, the message receive
603  * callback function @ap->recv_message() may be called at any time
604  * until capi20_release() has been called for the same @ap.
605  * Return value: CAPI result code
606  */
607
608 u16 capi20_register(struct capi20_appl *ap)
609 {
610         int i;
611         u16 applid;
612         unsigned long flags;
613
614         DBG("");
615
616         if (ap->rparam.datablklen < 128)
617                 return CAPI_LOGBLKSIZETOSMALL;
618
619         write_lock_irqsave(&application_lock, flags);
620
621         for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
622                 if (capi_applications[applid - 1] == NULL)
623                         break;
624         }
625         if (applid > CAPI_MAXAPPL) {
626                 write_unlock_irqrestore(&application_lock, flags);
627                 return CAPI_TOOMANYAPPLS;
628         }
629
630         ap->applid = applid;
631         capi_applications[applid - 1] = ap;
632
633         ap->nrecvctlpkt = 0;
634         ap->nrecvdatapkt = 0;
635         ap->nsentctlpkt = 0;
636         ap->nsentdatapkt = 0;
637         ap->callback = NULL;
638         mutex_init(&ap->recv_mtx);
639         skb_queue_head_init(&ap->recv_queue);
640         INIT_WORK(&ap->recv_work, recv_handler);
641         ap->release_in_progress = 0;
642
643         write_unlock_irqrestore(&application_lock, flags);
644         
645         mutex_lock(&controller_mutex);
646         for (i = 0; i < CAPI_MAXCONTR; i++) {
647                 if (!capi_cards[i] || capi_cards[i]->cardstate != CARD_RUNNING)
648                         continue;
649                 register_appl(capi_cards[i], applid, &ap->rparam);
650         }
651         mutex_unlock(&controller_mutex);
652
653         if (showcapimsgs & 1) {
654                 printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
655         }
656
657         return CAPI_NOERROR;
658 }
659
660 EXPORT_SYMBOL(capi20_register);
661
662 /**
663  * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
664  * @ap:         CAPI application descriptor structure.
665  *
666  * Terminate an application's registration with CAPI.
667  * After this function returns successfully, the message receive
668  * callback function @ap->recv_message() will no longer be called.
669  * Return value: CAPI result code
670  */
671
672 u16 capi20_release(struct capi20_appl *ap)
673 {
674         int i;
675         unsigned long flags;
676
677         DBG("applid %#x", ap->applid);
678
679         write_lock_irqsave(&application_lock, flags);
680         ap->release_in_progress = 1;
681         capi_applications[ap->applid - 1] = NULL;
682         write_unlock_irqrestore(&application_lock, flags);
683
684         mutex_lock(&controller_mutex);
685         for (i = 0; i < CAPI_MAXCONTR; i++) {
686                 if (!capi_cards[i] || capi_cards[i]->cardstate != CARD_RUNNING)
687                         continue;
688                 release_appl(capi_cards[i], ap->applid);
689         }
690         mutex_unlock(&controller_mutex);
691
692         flush_scheduled_work();
693         skb_queue_purge(&ap->recv_queue);
694
695         if (showcapimsgs & 1) {
696                 printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
697         }
698
699         return CAPI_NOERROR;
700 }
701
702 EXPORT_SYMBOL(capi20_release);
703
704 /**
705  * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
706  * @ap:         CAPI application descriptor structure.
707  * @skb:        CAPI message.
708  *
709  * Transfer a single message to CAPI.
710  * Return value: CAPI result code
711  */
712
713 u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
714 {
715         struct capi_ctr *card;
716         int showctl = 0;
717         u8 cmd, subcmd;
718
719         DBG("applid %#x", ap->applid);
720  
721         if (ncards == 0)
722                 return CAPI_REGNOTINSTALLED;
723         if ((ap->applid == 0) || ap->release_in_progress)
724                 return CAPI_ILLAPPNR;
725         if (skb->len < 12
726             || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
727             || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
728                 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
729         card = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
730         if (!card || card->cardstate != CARD_RUNNING) {
731                 card = get_capi_ctr_by_nr(1); // XXX why?
732                 if (!card || card->cardstate != CARD_RUNNING) 
733                         return CAPI_REGNOTINSTALLED;
734         }
735         if (card->blocked)
736                 return CAPI_SENDQUEUEFULL;
737
738         cmd = CAPIMSG_COMMAND(skb->data);
739         subcmd = CAPIMSG_SUBCOMMAND(skb->data);
740
741         if (cmd == CAPI_DATA_B3 && subcmd== CAPI_REQ) {
742                 card->nsentdatapkt++;
743                 ap->nsentdatapkt++;
744                 if (card->traceflag > 2) showctl |= 2;
745         } else {
746                 card->nsentctlpkt++;
747                 ap->nsentctlpkt++;
748                 if (card->traceflag) showctl |= 2;
749         }
750         showctl |= (card->traceflag & 1);
751         if (showctl & 2) {
752                 if (showctl & 1) {
753                         printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
754                                CAPIMSG_CONTROLLER(skb->data),
755                                CAPIMSG_APPID(skb->data),
756                                capi_cmd2str(cmd, subcmd),
757                                CAPIMSG_LEN(skb->data));
758                 } else {
759                         _cdebbuf *cdb = capi_message2str(skb->data);
760                         if (cdb) {
761                                 printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
762                                         CAPIMSG_CONTROLLER(skb->data),
763                                         cdb->buf);
764                                 cdebbuf_free(cdb);
765                         } else
766                                 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
767                                         CAPIMSG_CONTROLLER(skb->data),
768                                         CAPIMSG_APPID(skb->data),
769                                         capi_cmd2str(cmd, subcmd),
770                                         CAPIMSG_LEN(skb->data));
771                 }
772         }
773         return card->send_message(card, skb);
774 }
775
776 EXPORT_SYMBOL(capi20_put_message);
777
778 /**
779  * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
780  * @contr:      controller number.
781  * @buf:        result buffer (64 bytes).
782  *
783  * Retrieve information about the manufacturer of the specified ISDN controller
784  * or (for @contr == 0) the driver itself.
785  * Return value: CAPI result code
786  */
787
788 u16 capi20_get_manufacturer(u32 contr, u8 *buf)
789 {
790         struct capi_ctr *card;
791
792         if (contr == 0) {
793                 strlcpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
794                 return CAPI_NOERROR;
795         }
796         card = get_capi_ctr_by_nr(contr);
797         if (!card || card->cardstate != CARD_RUNNING) 
798                 return CAPI_REGNOTINSTALLED;
799         strlcpy(buf, card->manu, CAPI_MANUFACTURER_LEN);
800         return CAPI_NOERROR;
801 }
802
803 EXPORT_SYMBOL(capi20_get_manufacturer);
804
805 /**
806  * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
807  * @contr:      controller number.
808  * @verp:       result structure.
809  *
810  * Retrieve version information for the specified ISDN controller
811  * or (for @contr == 0) the driver itself.
812  * Return value: CAPI result code
813  */
814
815 u16 capi20_get_version(u32 contr, struct capi_version *verp)
816 {
817         struct capi_ctr *card;
818
819         if (contr == 0) {
820                 *verp = driver_version;
821                 return CAPI_NOERROR;
822         }
823         card = get_capi_ctr_by_nr(contr);
824         if (!card || card->cardstate != CARD_RUNNING) 
825                 return CAPI_REGNOTINSTALLED;
826
827         memcpy((void *) verp, &card->version, sizeof(capi_version));
828         return CAPI_NOERROR;
829 }
830
831 EXPORT_SYMBOL(capi20_get_version);
832
833 /**
834  * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
835  * @contr:      controller number.
836  * @serial:     result buffer (8 bytes).
837  *
838  * Retrieve the serial number of the specified ISDN controller
839  * or (for @contr == 0) the driver itself.
840  * Return value: CAPI result code
841  */
842
843 u16 capi20_get_serial(u32 contr, u8 *serial)
844 {
845         struct capi_ctr *card;
846
847         if (contr == 0) {
848                 strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
849                 return CAPI_NOERROR;
850         }
851         card = get_capi_ctr_by_nr(contr);
852         if (!card || card->cardstate != CARD_RUNNING) 
853                 return CAPI_REGNOTINSTALLED;
854
855         strlcpy((void *) serial, card->serial, CAPI_SERIAL_LEN);
856         return CAPI_NOERROR;
857 }
858
859 EXPORT_SYMBOL(capi20_get_serial);
860
861 /**
862  * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
863  * @contr:      controller number.
864  * @profp:      result structure.
865  *
866  * Retrieve capability information for the specified ISDN controller
867  * or (for @contr == 0) the number of installed controllers.
868  * Return value: CAPI result code
869  */
870
871 u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
872 {
873         struct capi_ctr *card;
874
875         if (contr == 0) {
876                 profp->ncontroller = ncards;
877                 return CAPI_NOERROR;
878         }
879         card = get_capi_ctr_by_nr(contr);
880         if (!card || card->cardstate != CARD_RUNNING) 
881                 return CAPI_REGNOTINSTALLED;
882
883         memcpy((void *) profp, &card->profile,
884                         sizeof(struct capi_profile));
885         return CAPI_NOERROR;
886 }
887
888 EXPORT_SYMBOL(capi20_get_profile);
889
890 #ifdef AVMB1_COMPAT
891 static int old_capi_manufacturer(unsigned int cmd, void __user *data)
892 {
893         avmb1_loadandconfigdef ldef;
894         avmb1_extcarddef cdef;
895         avmb1_resetdef rdef;
896         capicardparams cparams;
897         struct capi_ctr *card;
898         struct capi_driver *driver = NULL;
899         capiloaddata ldata;
900         struct list_head *l;
901         unsigned long flags;
902         int retval;
903
904         switch (cmd) {
905         case AVMB1_ADDCARD:
906         case AVMB1_ADDCARD_WITH_TYPE:
907                 if (cmd == AVMB1_ADDCARD) {
908                    if ((retval = copy_from_user(&cdef, data,
909                                             sizeof(avmb1_carddef))))
910                            return retval;
911                    cdef.cardtype = AVM_CARDTYPE_B1;
912                 } else {
913                    if ((retval = copy_from_user(&cdef, data,
914                                             sizeof(avmb1_extcarddef))))
915                            return retval;
916                 }
917                 cparams.port = cdef.port;
918                 cparams.irq = cdef.irq;
919                 cparams.cardnr = cdef.cardnr;
920
921                 read_lock_irqsave(&capi_drivers_list_lock, flags);
922                 switch (cdef.cardtype) {
923                         case AVM_CARDTYPE_B1:
924                                 list_for_each(l, &capi_drivers) {
925                                         driver = list_entry(l, struct capi_driver, list);
926                                         if (strcmp(driver->name, "b1isa") == 0)
927                                                 break;
928                                 }
929                                 break;
930                         case AVM_CARDTYPE_T1:
931                                 list_for_each(l, &capi_drivers) {
932                                         driver = list_entry(l, struct capi_driver, list);
933                                         if (strcmp(driver->name, "t1isa") == 0)
934                                                 break;
935                                 }
936                                 break;
937                         default:
938                                 driver = NULL;
939                                 break;
940                 }
941                 if (!driver) {
942                         read_unlock_irqrestore(&capi_drivers_list_lock, flags);
943                         printk(KERN_ERR "kcapi: driver not loaded.\n");
944                         return -EIO;
945                 }
946                 if (!driver->add_card) {
947                         read_unlock_irqrestore(&capi_drivers_list_lock, flags);
948                         printk(KERN_ERR "kcapi: driver has no add card function.\n");
949                         return -EIO;
950                 }
951
952                 retval = driver->add_card(driver, &cparams);
953                 read_unlock_irqrestore(&capi_drivers_list_lock, flags);
954                 return retval;
955
956         case AVMB1_LOAD:
957         case AVMB1_LOAD_AND_CONFIG:
958
959                 if (cmd == AVMB1_LOAD) {
960                         if (copy_from_user(&ldef, data,
961                                            sizeof(avmb1_loaddef)))
962                                 return -EFAULT;
963                         ldef.t4config.len = 0;
964                         ldef.t4config.data = NULL;
965                 } else {
966                         if (copy_from_user(&ldef, data,
967                                            sizeof(avmb1_loadandconfigdef)))
968                                 return -EFAULT;
969                 }
970                 card = get_capi_ctr_by_nr(ldef.contr);
971                 if (!card)
972                         return -EINVAL;
973                 card = capi_ctr_get(card);
974                 if (!card)
975                         return -ESRCH;
976                 if (card->load_firmware == NULL) {
977                         printk(KERN_DEBUG "kcapi: load: no load function\n");
978                         capi_ctr_put(card);
979                         return -ESRCH;
980                 }
981
982                 if (ldef.t4file.len <= 0) {
983                         printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
984                         capi_ctr_put(card);
985                         return -EINVAL;
986                 }
987                 if (ldef.t4file.data == NULL) {
988                         printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
989                         capi_ctr_put(card);
990                         return -EINVAL;
991                 }
992
993                 ldata.firmware.user = 1;
994                 ldata.firmware.data = ldef.t4file.data;
995                 ldata.firmware.len = ldef.t4file.len;
996                 ldata.configuration.user = 1;
997                 ldata.configuration.data = ldef.t4config.data;
998                 ldata.configuration.len = ldef.t4config.len;
999
1000                 if (card->cardstate != CARD_DETECTED) {
1001                         printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
1002                         capi_ctr_put(card);
1003                         return -EBUSY;
1004                 }
1005                 card->cardstate = CARD_LOADING;
1006
1007                 retval = card->load_firmware(card, &ldata);
1008
1009                 if (retval) {
1010                         card->cardstate = CARD_DETECTED;
1011                         capi_ctr_put(card);
1012                         return retval;
1013                 }
1014
1015                 while (card->cardstate != CARD_RUNNING) {
1016
1017                         msleep_interruptible(100);      /* 0.1 sec */
1018
1019                         if (signal_pending(current)) {
1020                                 capi_ctr_put(card);
1021                                 return -EINTR;
1022                         }
1023                 }
1024                 capi_ctr_put(card);
1025                 return 0;
1026
1027         case AVMB1_RESETCARD:
1028                 if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
1029                         return -EFAULT;
1030                 card = get_capi_ctr_by_nr(rdef.contr);
1031                 if (!card)
1032                         return -ESRCH;
1033
1034                 if (card->cardstate == CARD_DETECTED)
1035                         return 0;
1036
1037                 card->reset_ctr(card);
1038
1039                 while (card->cardstate > CARD_DETECTED) {
1040
1041                         msleep_interruptible(100);      /* 0.1 sec */
1042
1043                         if (signal_pending(current))
1044                                 return -EINTR;
1045                 }
1046                 return 0;
1047
1048         }
1049         return -EINVAL;
1050 }
1051 #endif
1052
1053 /**
1054  * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1055  * @cmd:        command.
1056  * @data:       parameter.
1057  *
1058  * Perform manufacturer specific command.
1059  * Return value: CAPI result code
1060  */
1061
1062 int capi20_manufacturer(unsigned int cmd, void __user *data)
1063 {
1064         struct capi_ctr *card;
1065
1066         switch (cmd) {
1067 #ifdef AVMB1_COMPAT
1068         case AVMB1_LOAD:
1069         case AVMB1_LOAD_AND_CONFIG:
1070         case AVMB1_RESETCARD:
1071         case AVMB1_GET_CARDINFO:
1072         case AVMB1_REMOVECARD:
1073                 return old_capi_manufacturer(cmd, data);
1074 #endif
1075         case KCAPI_CMD_TRACE:
1076         {
1077                 kcapi_flagdef fdef;
1078
1079                 if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
1080                         return -EFAULT;
1081
1082                 card = get_capi_ctr_by_nr(fdef.contr);
1083                 if (!card)
1084                         return -ESRCH;
1085
1086                 card->traceflag = fdef.flag;
1087                 printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
1088                         card->cnr, card->traceflag);
1089                 return 0;
1090         }
1091         case KCAPI_CMD_ADDCARD:
1092         {
1093                 struct list_head *l;
1094                 struct capi_driver *driver = NULL;
1095                 capicardparams cparams;
1096                 kcapi_carddef cdef;
1097                 int retval;
1098
1099                 if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
1100                         return retval;
1101
1102                 cparams.port = cdef.port;
1103                 cparams.irq = cdef.irq;
1104                 cparams.membase = cdef.membase;
1105                 cparams.cardnr = cdef.cardnr;
1106                 cparams.cardtype = 0;
1107                 cdef.driver[sizeof(cdef.driver)-1] = 0;
1108
1109                 list_for_each(l, &capi_drivers) {
1110                         driver = list_entry(l, struct capi_driver, list);
1111                         if (strcmp(driver->name, cdef.driver) == 0)
1112                                 break;
1113                 }
1114                 if (driver == NULL) {
1115                         printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
1116                                         cdef.driver);
1117                         return -ESRCH;
1118                 }
1119
1120                 if (!driver->add_card) {
1121                         printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
1122                         return -EIO;
1123                 }
1124
1125                 return driver->add_card(driver, &cparams);
1126         }
1127
1128         default:
1129                 printk(KERN_ERR "kcapi: manufacturer command %d unknown.\n",
1130                                         cmd);
1131                 break;
1132
1133         }
1134         return -EINVAL;
1135 }
1136
1137 EXPORT_SYMBOL(capi20_manufacturer);
1138
1139 /* temporary hack */
1140
1141 /**
1142  * capi20_set_callback() - set CAPI application notification callback function
1143  * @ap:         CAPI application descriptor structure.
1144  * @callback:   callback function (NULL to remove).
1145  *
1146  * If not NULL, the callback function will be called to notify the
1147  * application of the addition or removal of a controller.
1148  * The first argument (cmd) will tell whether the controller was added
1149  * (KCI_CONTRUP) or removed (KCI_CONTRDOWN).
1150  * The second argument (contr) will be the controller number.
1151  * For cmd==KCI_CONTRUP the third argument (data) will be a pointer to the
1152  * new controller's capability profile structure.
1153  */
1154
1155 void capi20_set_callback(struct capi20_appl *ap,
1156                          void (*callback) (unsigned int cmd, __u32 contr, void *data))
1157 {
1158         ap->callback = callback;
1159 }
1160
1161 EXPORT_SYMBOL(capi20_set_callback);
1162
1163 /* ------------------------------------------------------------- */
1164 /* -------- Init & Cleanup ------------------------------------- */
1165 /* ------------------------------------------------------------- */
1166
1167 /*
1168  * init / exit functions
1169  */
1170
1171 static int __init kcapi_init(void)
1172 {
1173         char *p;
1174         char rev[32];
1175         int ret;
1176
1177         ret = cdebug_init();
1178         if (ret)
1179                 return ret;
1180         kcapi_proc_init();
1181
1182         if ((p = strchr(revision, ':')) != NULL && p[1]) {
1183                 strlcpy(rev, p + 2, sizeof(rev));
1184                 if ((p = strchr(rev, '$')) != NULL && p > rev)
1185                    *(p-1) = 0;
1186         } else
1187                 strcpy(rev, "1.0");
1188
1189         printk(KERN_NOTICE "CAPI Subsystem Rev %s\n", rev);
1190
1191         return 0;
1192 }
1193
1194 static void __exit kcapi_exit(void)
1195 {
1196         kcapi_proc_exit();
1197
1198         /* make sure all notifiers are finished */
1199         flush_scheduled_work();
1200         cdebug_exit();
1201 }
1202
1203 module_init(kcapi_init);
1204 module_exit(kcapi_exit);