Merge branch 'master' into upstream
[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 CONFIG_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 CONFIG_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", __FUNCTION__);
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", __FUNCTION__, 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         down(&ap->recv_sem);
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         up(&ap->recv_sem);
271 }
272
273 void capi_ctr_handle_message(struct capi_ctr * card, u16 appl, struct sk_buff *skb)
274 {
275         struct capi20_appl *ap;
276         int showctl = 0;
277         u8 cmd, subcmd;
278         unsigned long flags;
279
280         if (card->cardstate != CARD_RUNNING) {
281                 printk(KERN_INFO "kcapi: controller %d not active, got: %s",
282                        card->cnr, capi_message2str(skb->data));
283                 goto error;
284         }
285
286         cmd = CAPIMSG_COMMAND(skb->data);
287         subcmd = CAPIMSG_SUBCOMMAND(skb->data);
288         if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
289                 card->nrecvdatapkt++;
290                 if (card->traceflag > 2) showctl |= 2;
291         } else {
292                 card->nrecvctlpkt++;
293                 if (card->traceflag) showctl |= 2;
294         }
295         showctl |= (card->traceflag & 1);
296         if (showctl & 2) {
297                 if (showctl & 1) {
298                         printk(KERN_DEBUG "kcapi: got [0x%lx] id#%d %s len=%u\n",
299                                (unsigned long) card->cnr,
300                                CAPIMSG_APPID(skb->data),
301                                capi_cmd2str(cmd, subcmd),
302                                CAPIMSG_LEN(skb->data));
303                 } else {
304                         printk(KERN_DEBUG "kcapi: got [0x%lx] %s\n",
305                                         (unsigned long) card->cnr,
306                                         capi_message2str(skb->data));
307                 }
308
309         }
310
311         read_lock_irqsave(&application_lock, flags);
312         ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
313         if ((!ap) || (ap->release_in_progress)) {
314                 read_unlock_irqrestore(&application_lock, flags);
315                 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
316                         CAPIMSG_APPID(skb->data), capi_message2str(skb->data));
317                 goto error;
318         }
319         skb_queue_tail(&ap->recv_queue, skb);
320         schedule_work(&ap->recv_work);
321         read_unlock_irqrestore(&application_lock, flags);
322
323         return;
324
325 error:
326         kfree_skb(skb);
327 }
328
329 EXPORT_SYMBOL(capi_ctr_handle_message);
330
331 void capi_ctr_ready(struct capi_ctr * card)
332 {
333         card->cardstate = CARD_RUNNING;
334
335         printk(KERN_NOTICE "kcapi: card %d \"%s\" ready.\n",
336                card->cnr, card->name);
337
338         notify_push(KCI_CONTRUP, card->cnr, 0, 0);
339 }
340
341 EXPORT_SYMBOL(capi_ctr_ready);
342
343 void capi_ctr_reseted(struct capi_ctr * card)
344 {
345         u16 appl;
346
347         DBG("");
348
349         if (card->cardstate == CARD_DETECTED)
350                 return;
351
352         card->cardstate = CARD_DETECTED;
353
354         memset(card->manu, 0, sizeof(card->manu));
355         memset(&card->version, 0, sizeof(card->version));
356         memset(&card->profile, 0, sizeof(card->profile));
357         memset(card->serial, 0, sizeof(card->serial));
358
359         for (appl = 1; appl <= CAPI_MAXAPPL; appl++) {
360                 struct capi20_appl *ap = get_capi_appl_by_nr(appl);
361                 if (!ap || ap->release_in_progress)
362                         continue;
363
364                 capi_ctr_put(card);
365         }
366
367         printk(KERN_NOTICE "kcapi: card %d down.\n", card->cnr);
368
369         notify_push(KCI_CONTRDOWN, card->cnr, 0, 0);
370 }
371
372 EXPORT_SYMBOL(capi_ctr_reseted);
373
374 void capi_ctr_suspend_output(struct capi_ctr *card)
375 {
376         if (!card->blocked) {
377                 printk(KERN_DEBUG "kcapi: card %d suspend\n", card->cnr);
378                 card->blocked = 1;
379         }
380 }
381
382 EXPORT_SYMBOL(capi_ctr_suspend_output);
383
384 void capi_ctr_resume_output(struct capi_ctr *card)
385 {
386         if (card->blocked) {
387                 printk(KERN_DEBUG "kcapi: card %d resume\n", card->cnr);
388                 card->blocked = 0;
389         }
390 }
391
392 EXPORT_SYMBOL(capi_ctr_resume_output);
393
394 /* ------------------------------------------------------------- */
395
396 int
397 attach_capi_ctr(struct capi_ctr *card)
398 {
399         int i;
400
401         mutex_lock(&controller_mutex);
402
403         for (i = 0; i < CAPI_MAXCONTR; i++) {
404                 if (capi_cards[i] == NULL)
405                         break;
406         }
407         if (i == CAPI_MAXCONTR) {
408                 mutex_unlock(&controller_mutex);
409                 printk(KERN_ERR "kcapi: out of controller slots\n");
410                 return -EBUSY;
411         }
412         capi_cards[i] = card;
413
414         mutex_unlock(&controller_mutex);
415
416         card->nrecvctlpkt = 0;
417         card->nrecvdatapkt = 0;
418         card->nsentctlpkt = 0;
419         card->nsentdatapkt = 0;
420         card->cnr = i + 1;
421         card->cardstate = CARD_DETECTED;
422         card->blocked = 0;
423         card->traceflag = showcapimsgs;
424
425         sprintf(card->procfn, "capi/controllers/%d", card->cnr);
426         card->procent = create_proc_entry(card->procfn, 0, NULL);
427         if (card->procent) {
428            card->procent->read_proc = 
429                 (int (*)(char *,char **,off_t,int,int *,void *))
430                         card->ctr_read_proc;
431            card->procent->data = card;
432         }
433
434         ncards++;
435         printk(KERN_NOTICE "kcapi: Controller %d: %s attached\n",
436                         card->cnr, card->name);
437         return 0;
438 }
439
440 EXPORT_SYMBOL(attach_capi_ctr);
441
442 int detach_capi_ctr(struct capi_ctr *card)
443 {
444         if (card->cardstate != CARD_DETECTED)
445                 capi_ctr_reseted(card);
446
447         ncards--;
448
449         if (card->procent) {
450            remove_proc_entry(card->procfn, NULL);
451            card->procent = NULL;
452         }
453         capi_cards[card->cnr - 1] = NULL;
454         printk(KERN_NOTICE "kcapi: Controller %d: %s unregistered\n",
455                         card->cnr, card->name);
456
457         return 0;
458 }
459
460 EXPORT_SYMBOL(detach_capi_ctr);
461
462 void register_capi_driver(struct capi_driver *driver)
463 {
464         unsigned long flags;
465
466         write_lock_irqsave(&capi_drivers_list_lock, flags);
467         list_add_tail(&driver->list, &capi_drivers);
468         write_unlock_irqrestore(&capi_drivers_list_lock, flags);
469 }
470
471 EXPORT_SYMBOL(register_capi_driver);
472
473 void unregister_capi_driver(struct capi_driver *driver)
474 {
475         unsigned long flags;
476
477         write_lock_irqsave(&capi_drivers_list_lock, flags);
478         list_del(&driver->list);
479         write_unlock_irqrestore(&capi_drivers_list_lock, flags);
480 }
481
482 EXPORT_SYMBOL(unregister_capi_driver);
483
484 /* ------------------------------------------------------------- */
485 /* -------- CAPI2.0 Interface ---------------------------------- */
486 /* ------------------------------------------------------------- */
487
488 u16 capi20_isinstalled(void)
489 {
490         int i;
491         for (i = 0; i < CAPI_MAXCONTR; i++) {
492                 if (capi_cards[i] && capi_cards[i]->cardstate == CARD_RUNNING)
493                         return CAPI_NOERROR;
494         }
495         return CAPI_REGNOTINSTALLED;
496 }
497
498 EXPORT_SYMBOL(capi20_isinstalled);
499
500 u16 capi20_register(struct capi20_appl *ap)
501 {
502         int i;
503         u16 applid;
504         unsigned long flags;
505
506         DBG("");
507
508         if (ap->rparam.datablklen < 128)
509                 return CAPI_LOGBLKSIZETOSMALL;
510
511         write_lock_irqsave(&application_lock, flags);
512
513         for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
514                 if (capi_applications[applid - 1] == NULL)
515                         break;
516         }
517         if (applid > CAPI_MAXAPPL) {
518                 write_unlock_irqrestore(&application_lock, flags);
519                 return CAPI_TOOMANYAPPLS;
520         }
521
522         ap->applid = applid;
523         capi_applications[applid - 1] = ap;
524
525         ap->nrecvctlpkt = 0;
526         ap->nrecvdatapkt = 0;
527         ap->nsentctlpkt = 0;
528         ap->nsentdatapkt = 0;
529         ap->callback = NULL;
530         init_MUTEX(&ap->recv_sem);
531         skb_queue_head_init(&ap->recv_queue);
532         INIT_WORK(&ap->recv_work, recv_handler);
533         ap->release_in_progress = 0;
534
535         write_unlock_irqrestore(&application_lock, flags);
536         
537         mutex_lock(&controller_mutex);
538         for (i = 0; i < CAPI_MAXCONTR; i++) {
539                 if (!capi_cards[i] || capi_cards[i]->cardstate != CARD_RUNNING)
540                         continue;
541                 register_appl(capi_cards[i], applid, &ap->rparam);
542         }
543         mutex_unlock(&controller_mutex);
544
545         if (showcapimsgs & 1) {
546                 printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
547         }
548
549         return CAPI_NOERROR;
550 }
551
552 EXPORT_SYMBOL(capi20_register);
553
554 u16 capi20_release(struct capi20_appl *ap)
555 {
556         int i;
557         unsigned long flags;
558
559         DBG("applid %#x", ap->applid);
560
561         write_lock_irqsave(&application_lock, flags);
562         ap->release_in_progress = 1;
563         capi_applications[ap->applid - 1] = NULL;
564         write_unlock_irqrestore(&application_lock, flags);
565
566         mutex_lock(&controller_mutex);
567         for (i = 0; i < CAPI_MAXCONTR; i++) {
568                 if (!capi_cards[i] || capi_cards[i]->cardstate != CARD_RUNNING)
569                         continue;
570                 release_appl(capi_cards[i], ap->applid);
571         }
572         mutex_unlock(&controller_mutex);
573
574         flush_scheduled_work();
575         skb_queue_purge(&ap->recv_queue);
576
577         if (showcapimsgs & 1) {
578                 printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
579         }
580
581         return CAPI_NOERROR;
582 }
583
584 EXPORT_SYMBOL(capi20_release);
585
586 u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
587 {
588         struct capi_ctr *card;
589         int showctl = 0;
590         u8 cmd, subcmd;
591
592         DBG("applid %#x", ap->applid);
593  
594         if (ncards == 0)
595                 return CAPI_REGNOTINSTALLED;
596         if ((ap->applid == 0) || ap->release_in_progress)
597                 return CAPI_ILLAPPNR;
598         if (skb->len < 12
599             || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
600             || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
601                 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
602         card = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
603         if (!card || card->cardstate != CARD_RUNNING) {
604                 card = get_capi_ctr_by_nr(1); // XXX why?
605                 if (!card || card->cardstate != CARD_RUNNING) 
606                         return CAPI_REGNOTINSTALLED;
607         }
608         if (card->blocked)
609                 return CAPI_SENDQUEUEFULL;
610
611         cmd = CAPIMSG_COMMAND(skb->data);
612         subcmd = CAPIMSG_SUBCOMMAND(skb->data);
613
614         if (cmd == CAPI_DATA_B3 && subcmd== CAPI_REQ) {
615                 card->nsentdatapkt++;
616                 ap->nsentdatapkt++;
617                 if (card->traceflag > 2) showctl |= 2;
618         } else {
619                 card->nsentctlpkt++;
620                 ap->nsentctlpkt++;
621                 if (card->traceflag) showctl |= 2;
622         }
623         showctl |= (card->traceflag & 1);
624         if (showctl & 2) {
625                 if (showctl & 1) {
626                         printk(KERN_DEBUG "kcapi: put [%#x] id#%d %s len=%u\n",
627                                CAPIMSG_CONTROLLER(skb->data),
628                                CAPIMSG_APPID(skb->data),
629                                capi_cmd2str(cmd, subcmd),
630                                CAPIMSG_LEN(skb->data));
631                 } else {
632                         printk(KERN_DEBUG "kcapi: put [%#x] %s\n",
633                                CAPIMSG_CONTROLLER(skb->data),
634                                capi_message2str(skb->data));
635                 }
636
637         }
638         return card->send_message(card, skb);
639 }
640
641 EXPORT_SYMBOL(capi20_put_message);
642
643 u16 capi20_get_manufacturer(u32 contr, u8 *buf)
644 {
645         struct capi_ctr *card;
646
647         if (contr == 0) {
648                 strlcpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
649                 return CAPI_NOERROR;
650         }
651         card = get_capi_ctr_by_nr(contr);
652         if (!card || card->cardstate != CARD_RUNNING) 
653                 return CAPI_REGNOTINSTALLED;
654         strlcpy(buf, card->manu, CAPI_MANUFACTURER_LEN);
655         return CAPI_NOERROR;
656 }
657
658 EXPORT_SYMBOL(capi20_get_manufacturer);
659
660 u16 capi20_get_version(u32 contr, struct capi_version *verp)
661 {
662         struct capi_ctr *card;
663
664         if (contr == 0) {
665                 *verp = driver_version;
666                 return CAPI_NOERROR;
667         }
668         card = get_capi_ctr_by_nr(contr);
669         if (!card || card->cardstate != CARD_RUNNING) 
670                 return CAPI_REGNOTINSTALLED;
671
672         memcpy((void *) verp, &card->version, sizeof(capi_version));
673         return CAPI_NOERROR;
674 }
675
676 EXPORT_SYMBOL(capi20_get_version);
677
678 u16 capi20_get_serial(u32 contr, u8 *serial)
679 {
680         struct capi_ctr *card;
681
682         if (contr == 0) {
683                 strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
684                 return CAPI_NOERROR;
685         }
686         card = get_capi_ctr_by_nr(contr);
687         if (!card || card->cardstate != CARD_RUNNING) 
688                 return CAPI_REGNOTINSTALLED;
689
690         strlcpy((void *) serial, card->serial, CAPI_SERIAL_LEN);
691         return CAPI_NOERROR;
692 }
693
694 EXPORT_SYMBOL(capi20_get_serial);
695
696 u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
697 {
698         struct capi_ctr *card;
699
700         if (contr == 0) {
701                 profp->ncontroller = ncards;
702                 return CAPI_NOERROR;
703         }
704         card = get_capi_ctr_by_nr(contr);
705         if (!card || card->cardstate != CARD_RUNNING) 
706                 return CAPI_REGNOTINSTALLED;
707
708         memcpy((void *) profp, &card->profile,
709                         sizeof(struct capi_profile));
710         return CAPI_NOERROR;
711 }
712
713 EXPORT_SYMBOL(capi20_get_profile);
714
715 #ifdef CONFIG_AVMB1_COMPAT
716 static int old_capi_manufacturer(unsigned int cmd, void __user *data)
717 {
718         avmb1_loadandconfigdef ldef;
719         avmb1_extcarddef cdef;
720         avmb1_resetdef rdef;
721         capicardparams cparams;
722         struct capi_ctr *card;
723         struct capi_driver *driver = NULL;
724         capiloaddata ldata;
725         struct list_head *l;
726         unsigned long flags;
727         int retval;
728
729         switch (cmd) {
730         case AVMB1_ADDCARD:
731         case AVMB1_ADDCARD_WITH_TYPE:
732                 if (cmd == AVMB1_ADDCARD) {
733                    if ((retval = copy_from_user(&cdef, data,
734                                             sizeof(avmb1_carddef))))
735                            return retval;
736                    cdef.cardtype = AVM_CARDTYPE_B1;
737                 } else {
738                    if ((retval = copy_from_user(&cdef, data,
739                                             sizeof(avmb1_extcarddef))))
740                            return retval;
741                 }
742                 cparams.port = cdef.port;
743                 cparams.irq = cdef.irq;
744                 cparams.cardnr = cdef.cardnr;
745
746                 read_lock_irqsave(&capi_drivers_list_lock, flags);
747                 switch (cdef.cardtype) {
748                         case AVM_CARDTYPE_B1:
749                                 list_for_each(l, &capi_drivers) {
750                                         driver = list_entry(l, struct capi_driver, list);
751                                         if (strcmp(driver->name, "b1isa") == 0)
752                                                 break;
753                                 }
754                                 break;
755                         case AVM_CARDTYPE_T1:
756                                 list_for_each(l, &capi_drivers) {
757                                         driver = list_entry(l, struct capi_driver, list);
758                                         if (strcmp(driver->name, "t1isa") == 0)
759                                                 break;
760                                 }
761                                 break;
762                         default:
763                                 driver = NULL;
764                                 break;
765                 }
766                 if (!driver) {
767                         read_unlock_irqrestore(&capi_drivers_list_lock, flags);
768                         printk(KERN_ERR "kcapi: driver not loaded.\n");
769                         return -EIO;
770                 }
771                 if (!driver->add_card) {
772                         read_unlock_irqrestore(&capi_drivers_list_lock, flags);
773                         printk(KERN_ERR "kcapi: driver has no add card function.\n");
774                         return -EIO;
775                 }
776
777                 retval = driver->add_card(driver, &cparams);
778                 read_unlock_irqrestore(&capi_drivers_list_lock, flags);
779                 return retval;
780
781         case AVMB1_LOAD:
782         case AVMB1_LOAD_AND_CONFIG:
783
784                 if (cmd == AVMB1_LOAD) {
785                         if (copy_from_user(&ldef, data,
786                                            sizeof(avmb1_loaddef)))
787                                 return -EFAULT;
788                         ldef.t4config.len = 0;
789                         ldef.t4config.data = NULL;
790                 } else {
791                         if (copy_from_user(&ldef, data,
792                                            sizeof(avmb1_loadandconfigdef)))
793                                 return -EFAULT;
794                 }
795                 card = get_capi_ctr_by_nr(ldef.contr);
796                 card = capi_ctr_get(card);
797                 if (!card)
798                         return -ESRCH;
799                 if (card->load_firmware == 0) {
800                         printk(KERN_DEBUG "kcapi: load: no load function\n");
801                         return -ESRCH;
802                 }
803
804                 if (ldef.t4file.len <= 0) {
805                         printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
806                         return -EINVAL;
807                 }
808                 if (ldef.t4file.data == 0) {
809                         printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
810                         return -EINVAL;
811                 }
812
813                 ldata.firmware.user = 1;
814                 ldata.firmware.data = ldef.t4file.data;
815                 ldata.firmware.len = ldef.t4file.len;
816                 ldata.configuration.user = 1;
817                 ldata.configuration.data = ldef.t4config.data;
818                 ldata.configuration.len = ldef.t4config.len;
819
820                 if (card->cardstate != CARD_DETECTED) {
821                         printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
822                         return -EBUSY;
823                 }
824                 card->cardstate = CARD_LOADING;
825
826                 retval = card->load_firmware(card, &ldata);
827
828                 if (retval) {
829                         card->cardstate = CARD_DETECTED;
830                         capi_ctr_put(card);
831                         return retval;
832                 }
833
834                 while (card->cardstate != CARD_RUNNING) {
835
836                         msleep_interruptible(100);      /* 0.1 sec */
837
838                         if (signal_pending(current)) {
839                                 capi_ctr_put(card);
840                                 return -EINTR;
841                         }
842                 }
843                 capi_ctr_put(card);
844                 return 0;
845
846         case AVMB1_RESETCARD:
847                 if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
848                         return -EFAULT;
849                 card = get_capi_ctr_by_nr(rdef.contr);
850                 if (!card)
851                         return -ESRCH;
852
853                 if (card->cardstate == CARD_DETECTED)
854                         return 0;
855
856                 card->reset_ctr(card);
857
858                 while (card->cardstate > CARD_DETECTED) {
859
860                         msleep_interruptible(100);      /* 0.1 sec */
861
862                         if (signal_pending(current))
863                                 return -EINTR;
864                 }
865                 return 0;
866
867         }
868         return -EINVAL;
869 }
870 #endif
871
872 int capi20_manufacturer(unsigned int cmd, void __user *data)
873 {
874         struct capi_ctr *card;
875
876         switch (cmd) {
877 #ifdef CONFIG_AVMB1_COMPAT
878         case AVMB1_LOAD:
879         case AVMB1_LOAD_AND_CONFIG:
880         case AVMB1_RESETCARD:
881         case AVMB1_GET_CARDINFO:
882         case AVMB1_REMOVECARD:
883                 return old_capi_manufacturer(cmd, data);
884 #endif
885         case KCAPI_CMD_TRACE:
886         {
887                 kcapi_flagdef fdef;
888
889                 if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
890                         return -EFAULT;
891
892                 card = get_capi_ctr_by_nr(fdef.contr);
893                 if (!card)
894                         return -ESRCH;
895
896                 card->traceflag = fdef.flag;
897                 printk(KERN_INFO "kcapi: contr %d set trace=%d\n",
898                         card->cnr, card->traceflag);
899                 return 0;
900         }
901         case KCAPI_CMD_ADDCARD:
902         {
903                 struct list_head *l;
904                 struct capi_driver *driver = NULL;
905                 capicardparams cparams;
906                 kcapi_carddef cdef;
907                 int retval;
908
909                 if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
910                         return retval;
911
912                 cparams.port = cdef.port;
913                 cparams.irq = cdef.irq;
914                 cparams.membase = cdef.membase;
915                 cparams.cardnr = cdef.cardnr;
916                 cparams.cardtype = 0;
917                 cdef.driver[sizeof(cdef.driver)-1] = 0;
918
919                 list_for_each(l, &capi_drivers) {
920                         driver = list_entry(l, struct capi_driver, list);
921                         if (strcmp(driver->name, cdef.driver) == 0)
922                                 break;
923                 }
924                 if (driver == 0) {
925                         printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
926                                         cdef.driver);
927                         return -ESRCH;
928                 }
929
930                 if (!driver->add_card) {
931                         printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
932                         return -EIO;
933                 }
934
935                 return driver->add_card(driver, &cparams);
936         }
937
938         default:
939                 printk(KERN_ERR "kcapi: manufacturer command %d unknown.\n",
940                                         cmd);
941                 break;
942
943         }
944         return -EINVAL;
945 }
946
947 EXPORT_SYMBOL(capi20_manufacturer);
948
949 /* temporary hack */
950 void capi20_set_callback(struct capi20_appl *ap,
951                          void (*callback) (unsigned int cmd, __u32 contr, void *data))
952 {
953         ap->callback = callback;
954 }
955
956 EXPORT_SYMBOL(capi20_set_callback);
957
958 /* ------------------------------------------------------------- */
959 /* -------- Init & Cleanup ------------------------------------- */
960 /* ------------------------------------------------------------- */
961
962 /*
963  * init / exit functions
964  */
965
966 static int __init kcapi_init(void)
967 {
968         char *p;
969         char rev[32];
970
971         kcapi_proc_init();
972
973         if ((p = strchr(revision, ':')) != 0 && p[1]) {
974                 strlcpy(rev, p + 2, sizeof(rev));
975                 if ((p = strchr(rev, '$')) != 0 && p > rev)
976                    *(p-1) = 0;
977         } else
978                 strcpy(rev, "1.0");
979
980         printk(KERN_NOTICE "CAPI Subsystem Rev %s\n", rev);
981
982         return 0;
983 }
984
985 static void __exit kcapi_exit(void)
986 {
987         kcapi_proc_exit();
988
989         /* make sure all notifiers are finished */
990         flush_scheduled_work();
991 }
992
993 module_init(kcapi_init);
994 module_exit(kcapi_exit);