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