1 /* $Id: kcapi.c,v 1.1.2.8 2004/03/26 19:57:20 armin Exp $
3 * Kernel CAPI 2.0 Module
5 * Copyright 1999 by Carsten Paeth <calle@calle.de>
6 * Copyright 2002 by Kai Germaschewski <kai@germaschewski.name>
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
16 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/proc_fs.h>
21 #include <linux/sched.h>
22 #include <linux/seq_file.h>
23 #include <linux/skbuff.h>
24 #include <linux/workqueue.h>
25 #include <linux/capi.h>
26 #include <linux/kernelcapi.h>
27 #include <linux/init.h>
28 #include <linux/moduleparam.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <asm/uaccess.h>
32 #include <linux/isdn/capicmd.h>
33 #include <linux/isdn/capiutil.h>
35 #include <linux/b1lli.h>
37 #include <linux/mutex.h>
38 #include <linux/rcupdate.h>
40 static int showcapimsgs
= 0;
41 static struct workqueue_struct
*kcapi_wq
;
43 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
44 MODULE_AUTHOR("Carsten Paeth");
45 MODULE_LICENSE("GPL");
46 module_param(showcapimsgs
, uint
, 0);
48 /* ------------------------------------------------------------- */
50 struct capictr_event
{
51 struct work_struct work
;
56 /* ------------------------------------------------------------- */
58 static struct capi_version driver_version
= {2, 0, 1, 1 << 4};
59 static char driver_serial
[CAPI_SERIAL_LEN
] = "0004711";
60 static char capi_manufakturer
[64] = "AVM Berlin";
62 #define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
64 LIST_HEAD(capi_drivers
);
65 DEFINE_MUTEX(capi_drivers_lock
);
67 struct capi_ctr
*capi_controller
[CAPI_MAXCONTR
];
68 DEFINE_MUTEX(capi_controller_lock
);
70 struct capi20_appl
*capi_applications
[CAPI_MAXAPPL
];
72 static int ncontrollers
;
74 static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list
);
76 /* -------- controller ref counting -------------------------------------- */
78 static inline struct capi_ctr
*
79 capi_ctr_get(struct capi_ctr
*ctr
)
81 if (!try_module_get(ctr
->owner
))
87 capi_ctr_put(struct capi_ctr
*ctr
)
89 module_put(ctr
->owner
);
92 /* ------------------------------------------------------------- */
94 static inline struct capi_ctr
*get_capi_ctr_by_nr(u16 contr
)
96 if (contr
< 1 || contr
- 1 >= CAPI_MAXCONTR
)
99 return capi_controller
[contr
- 1];
102 static inline struct capi20_appl
*__get_capi_appl_by_nr(u16 applid
)
104 lockdep_assert_held(&capi_controller_lock
);
106 if (applid
< 1 || applid
- 1 >= CAPI_MAXAPPL
)
109 return capi_applications
[applid
- 1];
112 static inline struct capi20_appl
*get_capi_appl_by_nr(u16 applid
)
114 if (applid
< 1 || applid
- 1 >= CAPI_MAXAPPL
)
117 return rcu_dereference(capi_applications
[applid
- 1]);
120 /* -------- util functions ------------------------------------ */
122 static inline int capi_cmd_valid(u8 cmd
)
127 case CAPI_CONNECT_ACTIVE
:
128 case CAPI_CONNECT_B3_ACTIVE
:
129 case CAPI_CONNECT_B3
:
130 case CAPI_CONNECT_B3_T90_ACTIVE
:
132 case CAPI_DISCONNECT_B3
:
133 case CAPI_DISCONNECT
:
137 case CAPI_MANUFACTURER
:
139 case CAPI_SELECT_B_PROTOCOL
:
145 static inline int capi_subcmd_valid(u8 subcmd
)
157 /* ------------------------------------------------------------ */
160 register_appl(struct capi_ctr
*ctr
, u16 applid
, capi_register_params
*rparam
)
162 ctr
= capi_ctr_get(ctr
);
165 ctr
->register_appl(ctr
, applid
, rparam
);
167 printk(KERN_WARNING
"%s: cannot get controller resources\n",
172 static void release_appl(struct capi_ctr
*ctr
, u16 applid
)
174 DBG("applid %#x", applid
);
176 ctr
->release_appl(ctr
, applid
);
180 static void notify_up(u32 contr
)
182 struct capi20_appl
*ap
;
183 struct capi_ctr
*ctr
;
186 mutex_lock(&capi_controller_lock
);
188 if (showcapimsgs
& 1)
189 printk(KERN_DEBUG
"kcapi: notify up contr %d\n", contr
);
191 ctr
= get_capi_ctr_by_nr(contr
);
193 if (ctr
->state
== CAPI_CTR_RUNNING
)
196 ctr
->state
= CAPI_CTR_RUNNING
;
198 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
199 ap
= __get_capi_appl_by_nr(applid
);
201 register_appl(ctr
, applid
, &ap
->rparam
);
204 wake_up_interruptible_all(&ctr
->state_wait_queue
);
206 printk(KERN_WARNING
"%s: invalid contr %d\n", __func__
, contr
);
209 mutex_unlock(&capi_controller_lock
);
212 static void ctr_down(struct capi_ctr
*ctr
, int new_state
)
214 struct capi20_appl
*ap
;
217 if (ctr
->state
== CAPI_CTR_DETECTED
|| ctr
->state
== CAPI_CTR_DETACHED
)
220 ctr
->state
= new_state
;
222 memset(ctr
->manu
, 0, sizeof(ctr
->manu
));
223 memset(&ctr
->version
, 0, sizeof(ctr
->version
));
224 memset(&ctr
->profile
, 0, sizeof(ctr
->profile
));
225 memset(ctr
->serial
, 0, sizeof(ctr
->serial
));
227 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
228 ap
= __get_capi_appl_by_nr(applid
);
233 wake_up_interruptible_all(&ctr
->state_wait_queue
);
236 static void notify_down(u32 contr
)
238 struct capi_ctr
*ctr
;
240 mutex_lock(&capi_controller_lock
);
242 if (showcapimsgs
& 1)
243 printk(KERN_DEBUG
"kcapi: notify down contr %d\n", contr
);
245 ctr
= get_capi_ctr_by_nr(contr
);
247 ctr_down(ctr
, CAPI_CTR_DETECTED
);
249 printk(KERN_WARNING
"%s: invalid contr %d\n", __func__
, contr
);
251 mutex_unlock(&capi_controller_lock
);
255 notify_handler(struct notifier_block
*nb
, unsigned long val
, void *v
)
270 static void do_notify_work(struct work_struct
*work
)
272 struct capictr_event
*event
=
273 container_of(work
, struct capictr_event
, work
);
275 blocking_notifier_call_chain(&ctr_notifier_list
, event
->type
,
276 (void *)(long)event
->controller
);
281 * The notifier will result in adding/deleteing of devices. Devices can
282 * only removed in user process, not in bh.
284 static int notify_push(unsigned int event_type
, u32 controller
)
286 struct capictr_event
*event
= kmalloc(sizeof(*event
), GFP_ATOMIC
);
291 INIT_WORK(&event
->work
, do_notify_work
);
292 event
->type
= event_type
;
293 event
->controller
= controller
;
295 queue_work(kcapi_wq
, &event
->work
);
299 int register_capictr_notifier(struct notifier_block
*nb
)
301 return blocking_notifier_chain_register(&ctr_notifier_list
, nb
);
303 EXPORT_SYMBOL_GPL(register_capictr_notifier
);
305 int unregister_capictr_notifier(struct notifier_block
*nb
)
307 return blocking_notifier_chain_unregister(&ctr_notifier_list
, nb
);
309 EXPORT_SYMBOL_GPL(unregister_capictr_notifier
);
311 /* -------- Receiver ------------------------------------------ */
313 static void recv_handler(struct work_struct
*work
)
316 struct capi20_appl
*ap
=
317 container_of(work
, struct capi20_appl
, recv_work
);
319 if ((!ap
) || (ap
->release_in_progress
))
322 mutex_lock(&ap
->recv_mtx
);
323 while ((skb
= skb_dequeue(&ap
->recv_queue
))) {
324 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_IND
)
329 ap
->recv_message(ap
, skb
);
331 mutex_unlock(&ap
->recv_mtx
);
335 * capi_ctr_handle_message() - handle incoming CAPI message
336 * @ctr: controller descriptor structure.
337 * @appl: application ID.
340 * Called by hardware driver to pass a CAPI message to the application.
343 void capi_ctr_handle_message(struct capi_ctr
*ctr
, u16 appl
,
346 struct capi20_appl
*ap
;
351 if (ctr
->state
!= CAPI_CTR_RUNNING
) {
352 cdb
= capi_message2str(skb
->data
);
354 printk(KERN_INFO
"kcapi: controller [%03d] not active, got: %s",
358 printk(KERN_INFO
"kcapi: controller [%03d] not active, cannot trace\n",
363 cmd
= CAPIMSG_COMMAND(skb
->data
);
364 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
365 if (cmd
== CAPI_DATA_B3
&& subcmd
== CAPI_IND
) {
367 if (ctr
->traceflag
> 2)
374 showctl
|= (ctr
->traceflag
& 1);
377 printk(KERN_DEBUG
"kcapi: got [%03d] id#%d %s len=%u\n",
378 ctr
->cnr
, CAPIMSG_APPID(skb
->data
),
379 capi_cmd2str(cmd
, subcmd
),
380 CAPIMSG_LEN(skb
->data
));
382 cdb
= capi_message2str(skb
->data
);
384 printk(KERN_DEBUG
"kcapi: got [%03d] %s\n",
388 printk(KERN_DEBUG
"kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
389 ctr
->cnr
, CAPIMSG_APPID(skb
->data
),
390 capi_cmd2str(cmd
, subcmd
),
391 CAPIMSG_LEN(skb
->data
));
397 ap
= get_capi_appl_by_nr(CAPIMSG_APPID(skb
->data
));
400 cdb
= capi_message2str(skb
->data
);
402 printk(KERN_ERR
"kcapi: handle_message: applid %d state released (%s)\n",
403 CAPIMSG_APPID(skb
->data
), cdb
->buf
);
406 printk(KERN_ERR
"kcapi: handle_message: applid %d state released (%s) cannot trace\n",
407 CAPIMSG_APPID(skb
->data
),
408 capi_cmd2str(cmd
, subcmd
));
411 skb_queue_tail(&ap
->recv_queue
, skb
);
412 queue_work(kcapi_wq
, &ap
->recv_work
);
421 EXPORT_SYMBOL(capi_ctr_handle_message
);
424 * capi_ctr_ready() - signal CAPI controller ready
425 * @ctr: controller descriptor structure.
427 * Called by hardware driver to signal that the controller is up and running.
430 void capi_ctr_ready(struct capi_ctr
*ctr
)
432 printk(KERN_NOTICE
"kcapi: controller [%03d] \"%s\" ready.\n",
433 ctr
->cnr
, ctr
->name
);
435 notify_push(CAPICTR_UP
, ctr
->cnr
);
438 EXPORT_SYMBOL(capi_ctr_ready
);
441 * capi_ctr_down() - signal CAPI controller not ready
442 * @ctr: controller descriptor structure.
444 * Called by hardware driver to signal that the controller is down and
445 * unavailable for use.
448 void capi_ctr_down(struct capi_ctr
*ctr
)
450 printk(KERN_NOTICE
"kcapi: controller [%03d] down.\n", ctr
->cnr
);
452 notify_push(CAPICTR_DOWN
, ctr
->cnr
);
455 EXPORT_SYMBOL(capi_ctr_down
);
458 * capi_ctr_suspend_output() - suspend controller
459 * @ctr: controller descriptor structure.
461 * Called by hardware driver to stop data flow.
463 * Note: The caller is responsible for synchronizing concurrent state changes
464 * as well as invocations of capi_ctr_handle_message.
467 void capi_ctr_suspend_output(struct capi_ctr
*ctr
)
470 printk(KERN_DEBUG
"kcapi: controller [%03d] suspend\n",
476 EXPORT_SYMBOL(capi_ctr_suspend_output
);
479 * capi_ctr_resume_output() - resume controller
480 * @ctr: controller descriptor structure.
482 * Called by hardware driver to resume data flow.
484 * Note: The caller is responsible for synchronizing concurrent state changes
485 * as well as invocations of capi_ctr_handle_message.
488 void capi_ctr_resume_output(struct capi_ctr
*ctr
)
491 printk(KERN_DEBUG
"kcapi: controller [%03d] resumed\n",
497 EXPORT_SYMBOL(capi_ctr_resume_output
);
499 /* ------------------------------------------------------------- */
502 * attach_capi_ctr() - register CAPI controller
503 * @ctr: controller descriptor structure.
505 * Called by hardware driver to register a controller with the CAPI subsystem.
506 * Return value: 0 on success, error code < 0 on error
509 int attach_capi_ctr(struct capi_ctr
*ctr
)
513 mutex_lock(&capi_controller_lock
);
515 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
516 if (!capi_controller
[i
])
519 if (i
== CAPI_MAXCONTR
) {
520 mutex_unlock(&capi_controller_lock
);
521 printk(KERN_ERR
"kcapi: out of controller slots\n");
524 capi_controller
[i
] = ctr
;
526 ctr
->nrecvctlpkt
= 0;
527 ctr
->nrecvdatapkt
= 0;
528 ctr
->nsentctlpkt
= 0;
529 ctr
->nsentdatapkt
= 0;
531 ctr
->state
= CAPI_CTR_DETECTED
;
533 ctr
->traceflag
= showcapimsgs
;
534 init_waitqueue_head(&ctr
->state_wait_queue
);
536 sprintf(ctr
->procfn
, "capi/controllers/%d", ctr
->cnr
);
537 ctr
->procent
= proc_create_data(ctr
->procfn
, 0, NULL
, ctr
->proc_fops
, ctr
);
541 mutex_unlock(&capi_controller_lock
);
543 printk(KERN_NOTICE
"kcapi: controller [%03d]: %s attached\n",
544 ctr
->cnr
, ctr
->name
);
548 EXPORT_SYMBOL(attach_capi_ctr
);
551 * detach_capi_ctr() - unregister CAPI controller
552 * @ctr: controller descriptor structure.
554 * Called by hardware driver to remove the registration of a controller
555 * with the CAPI subsystem.
556 * Return value: 0 on success, error code < 0 on error
559 int detach_capi_ctr(struct capi_ctr
*ctr
)
563 mutex_lock(&capi_controller_lock
);
565 ctr_down(ctr
, CAPI_CTR_DETACHED
);
567 if (capi_controller
[ctr
->cnr
- 1] != ctr
) {
571 capi_controller
[ctr
->cnr
- 1] = NULL
;
575 remove_proc_entry(ctr
->procfn
, NULL
);
577 printk(KERN_NOTICE
"kcapi: controller [%03d]: %s unregistered\n",
578 ctr
->cnr
, ctr
->name
);
581 mutex_unlock(&capi_controller_lock
);
586 EXPORT_SYMBOL(detach_capi_ctr
);
589 * register_capi_driver() - register CAPI driver
590 * @driver: driver descriptor structure.
592 * Called by hardware driver to register itself with the CAPI subsystem.
595 void register_capi_driver(struct capi_driver
*driver
)
597 mutex_lock(&capi_drivers_lock
);
598 list_add_tail(&driver
->list
, &capi_drivers
);
599 mutex_unlock(&capi_drivers_lock
);
602 EXPORT_SYMBOL(register_capi_driver
);
605 * unregister_capi_driver() - unregister CAPI driver
606 * @driver: driver descriptor structure.
608 * Called by hardware driver to unregister itself from the CAPI subsystem.
611 void unregister_capi_driver(struct capi_driver
*driver
)
613 mutex_lock(&capi_drivers_lock
);
614 list_del(&driver
->list
);
615 mutex_unlock(&capi_drivers_lock
);
618 EXPORT_SYMBOL(unregister_capi_driver
);
620 /* ------------------------------------------------------------- */
621 /* -------- CAPI2.0 Interface ---------------------------------- */
622 /* ------------------------------------------------------------- */
625 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
627 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
628 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
631 u16
capi20_isinstalled(void)
633 u16 ret
= CAPI_REGNOTINSTALLED
;
636 mutex_lock(&capi_controller_lock
);
638 for (i
= 0; i
< CAPI_MAXCONTR
; i
++)
639 if (capi_controller
[i
] &&
640 capi_controller
[i
]->state
== CAPI_CTR_RUNNING
) {
645 mutex_unlock(&capi_controller_lock
);
650 EXPORT_SYMBOL(capi20_isinstalled
);
653 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
654 * @ap: CAPI application descriptor structure.
656 * Register an application's presence with CAPI.
657 * A unique application ID is assigned and stored in @ap->applid.
658 * After this function returns successfully, the message receive
659 * callback function @ap->recv_message() may be called at any time
660 * until capi20_release() has been called for the same @ap.
661 * Return value: CAPI result code
664 u16
capi20_register(struct capi20_appl
*ap
)
671 if (ap
->rparam
.datablklen
< 128)
672 return CAPI_LOGBLKSIZETOSMALL
;
675 ap
->nrecvdatapkt
= 0;
677 ap
->nsentdatapkt
= 0;
678 mutex_init(&ap
->recv_mtx
);
679 skb_queue_head_init(&ap
->recv_queue
);
680 INIT_WORK(&ap
->recv_work
, recv_handler
);
681 ap
->release_in_progress
= 0;
683 mutex_lock(&capi_controller_lock
);
685 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
686 if (capi_applications
[applid
- 1] == NULL
)
689 if (applid
> CAPI_MAXAPPL
) {
690 mutex_unlock(&capi_controller_lock
);
691 return CAPI_TOOMANYAPPLS
;
695 capi_applications
[applid
- 1] = ap
;
697 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
698 if (!capi_controller
[i
] ||
699 capi_controller
[i
]->state
!= CAPI_CTR_RUNNING
)
701 register_appl(capi_controller
[i
], applid
, &ap
->rparam
);
704 mutex_unlock(&capi_controller_lock
);
706 if (showcapimsgs
& 1) {
707 printk(KERN_DEBUG
"kcapi: appl %d up\n", applid
);
713 EXPORT_SYMBOL(capi20_register
);
716 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
717 * @ap: CAPI application descriptor structure.
719 * Terminate an application's registration with CAPI.
720 * After this function returns successfully, the message receive
721 * callback function @ap->recv_message() will no longer be called.
722 * Return value: CAPI result code
725 u16
capi20_release(struct capi20_appl
*ap
)
729 DBG("applid %#x", ap
->applid
);
731 mutex_lock(&capi_controller_lock
);
733 ap
->release_in_progress
= 1;
734 capi_applications
[ap
->applid
- 1] = NULL
;
738 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
739 if (!capi_controller
[i
] ||
740 capi_controller
[i
]->state
!= CAPI_CTR_RUNNING
)
742 release_appl(capi_controller
[i
], ap
->applid
);
745 mutex_unlock(&capi_controller_lock
);
747 flush_workqueue(kcapi_wq
);
748 skb_queue_purge(&ap
->recv_queue
);
750 if (showcapimsgs
& 1) {
751 printk(KERN_DEBUG
"kcapi: appl %d down\n", ap
->applid
);
757 EXPORT_SYMBOL(capi20_release
);
760 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
761 * @ap: CAPI application descriptor structure.
762 * @skb: CAPI message.
764 * Transfer a single message to CAPI.
765 * Return value: CAPI result code
768 u16
capi20_put_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
770 struct capi_ctr
*ctr
;
774 DBG("applid %#x", ap
->applid
);
776 if (ncontrollers
== 0)
777 return CAPI_REGNOTINSTALLED
;
778 if ((ap
->applid
== 0) || ap
->release_in_progress
)
779 return CAPI_ILLAPPNR
;
781 || !capi_cmd_valid(CAPIMSG_COMMAND(skb
->data
))
782 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb
->data
)))
783 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL
;
786 * The controller reference is protected by the existence of the
787 * application passed to us. We assume that the caller properly
788 * synchronizes this service with capi20_release.
790 ctr
= get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb
->data
));
791 if (!ctr
|| ctr
->state
!= CAPI_CTR_RUNNING
)
792 return CAPI_REGNOTINSTALLED
;
794 return CAPI_SENDQUEUEFULL
;
796 cmd
= CAPIMSG_COMMAND(skb
->data
);
797 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
799 if (cmd
== CAPI_DATA_B3
&& subcmd
== CAPI_REQ
) {
802 if (ctr
->traceflag
> 2)
810 showctl
|= (ctr
->traceflag
& 1);
813 printk(KERN_DEBUG
"kcapi: put [%03d] id#%d %s len=%u\n",
814 CAPIMSG_CONTROLLER(skb
->data
),
815 CAPIMSG_APPID(skb
->data
),
816 capi_cmd2str(cmd
, subcmd
),
817 CAPIMSG_LEN(skb
->data
));
819 _cdebbuf
*cdb
= capi_message2str(skb
->data
);
821 printk(KERN_DEBUG
"kcapi: put [%03d] %s\n",
822 CAPIMSG_CONTROLLER(skb
->data
),
826 printk(KERN_DEBUG
"kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
827 CAPIMSG_CONTROLLER(skb
->data
),
828 CAPIMSG_APPID(skb
->data
),
829 capi_cmd2str(cmd
, subcmd
),
830 CAPIMSG_LEN(skb
->data
));
833 return ctr
->send_message(ctr
, skb
);
836 EXPORT_SYMBOL(capi20_put_message
);
839 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
840 * @contr: controller number.
841 * @buf: result buffer (64 bytes).
843 * Retrieve information about the manufacturer of the specified ISDN controller
844 * or (for @contr == 0) the driver itself.
845 * Return value: CAPI result code
848 u16
capi20_get_manufacturer(u32 contr
, u8
*buf
)
850 struct capi_ctr
*ctr
;
854 strlcpy(buf
, capi_manufakturer
, CAPI_MANUFACTURER_LEN
);
858 mutex_lock(&capi_controller_lock
);
860 ctr
= get_capi_ctr_by_nr(contr
);
861 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
862 strlcpy(buf
, ctr
->manu
, CAPI_MANUFACTURER_LEN
);
865 ret
= CAPI_REGNOTINSTALLED
;
867 mutex_unlock(&capi_controller_lock
);
871 EXPORT_SYMBOL(capi20_get_manufacturer
);
874 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
875 * @contr: controller number.
876 * @verp: result structure.
878 * Retrieve version information for the specified ISDN controller
879 * or (for @contr == 0) the driver itself.
880 * Return value: CAPI result code
883 u16
capi20_get_version(u32 contr
, struct capi_version
*verp
)
885 struct capi_ctr
*ctr
;
889 *verp
= driver_version
;
893 mutex_lock(&capi_controller_lock
);
895 ctr
= get_capi_ctr_by_nr(contr
);
896 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
897 memcpy(verp
, &ctr
->version
, sizeof(capi_version
));
900 ret
= CAPI_REGNOTINSTALLED
;
902 mutex_unlock(&capi_controller_lock
);
906 EXPORT_SYMBOL(capi20_get_version
);
909 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
910 * @contr: controller number.
911 * @serial: result buffer (8 bytes).
913 * Retrieve the serial number of the specified ISDN controller
914 * or (for @contr == 0) the driver itself.
915 * Return value: CAPI result code
918 u16
capi20_get_serial(u32 contr
, u8
*serial
)
920 struct capi_ctr
*ctr
;
924 strlcpy(serial
, driver_serial
, CAPI_SERIAL_LEN
);
928 mutex_lock(&capi_controller_lock
);
930 ctr
= get_capi_ctr_by_nr(contr
);
931 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
932 strlcpy(serial
, ctr
->serial
, CAPI_SERIAL_LEN
);
935 ret
= CAPI_REGNOTINSTALLED
;
937 mutex_unlock(&capi_controller_lock
);
941 EXPORT_SYMBOL(capi20_get_serial
);
944 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
945 * @contr: controller number.
946 * @profp: result structure.
948 * Retrieve capability information for the specified ISDN controller
949 * or (for @contr == 0) the number of installed controllers.
950 * Return value: CAPI result code
953 u16
capi20_get_profile(u32 contr
, struct capi_profile
*profp
)
955 struct capi_ctr
*ctr
;
959 profp
->ncontroller
= ncontrollers
;
963 mutex_lock(&capi_controller_lock
);
965 ctr
= get_capi_ctr_by_nr(contr
);
966 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
967 memcpy(profp
, &ctr
->profile
, sizeof(struct capi_profile
));
970 ret
= CAPI_REGNOTINSTALLED
;
972 mutex_unlock(&capi_controller_lock
);
976 EXPORT_SYMBOL(capi20_get_profile
);
978 /* Must be called with capi_controller_lock held. */
979 static int wait_on_ctr_state(struct capi_ctr
*ctr
, unsigned int state
)
984 ctr
= capi_ctr_get(ctr
);
989 prepare_to_wait(&ctr
->state_wait_queue
, &wait
,
992 if (ctr
->state
== state
)
994 if (ctr
->state
== CAPI_CTR_DETACHED
) {
998 if (signal_pending(current
)) {
1003 mutex_unlock(&capi_controller_lock
);
1005 mutex_lock(&capi_controller_lock
);
1007 finish_wait(&ctr
->state_wait_queue
, &wait
);
1015 static int old_capi_manufacturer(unsigned int cmd
, void __user
*data
)
1017 avmb1_loadandconfigdef ldef
;
1018 avmb1_extcarddef cdef
;
1019 avmb1_resetdef rdef
;
1020 capicardparams cparams
;
1021 struct capi_ctr
*ctr
;
1022 struct capi_driver
*driver
= NULL
;
1024 struct list_head
*l
;
1029 case AVMB1_ADDCARD_WITH_TYPE
:
1030 if (cmd
== AVMB1_ADDCARD
) {
1031 if ((retval
= copy_from_user(&cdef
, data
,
1032 sizeof(avmb1_carddef
))))
1034 cdef
.cardtype
= AVM_CARDTYPE_B1
;
1036 if ((retval
= copy_from_user(&cdef
, data
,
1037 sizeof(avmb1_extcarddef
))))
1040 cparams
.port
= cdef
.port
;
1041 cparams
.irq
= cdef
.irq
;
1042 cparams
.cardnr
= cdef
.cardnr
;
1044 mutex_lock(&capi_drivers_lock
);
1046 switch (cdef
.cardtype
) {
1047 case AVM_CARDTYPE_B1
:
1048 list_for_each(l
, &capi_drivers
) {
1049 driver
= list_entry(l
, struct capi_driver
, list
);
1050 if (strcmp(driver
->name
, "b1isa") == 0)
1054 case AVM_CARDTYPE_T1
:
1055 list_for_each(l
, &capi_drivers
) {
1056 driver
= list_entry(l
, struct capi_driver
, list
);
1057 if (strcmp(driver
->name
, "t1isa") == 0)
1066 printk(KERN_ERR
"kcapi: driver not loaded.\n");
1068 } else if (!driver
->add_card
) {
1069 printk(KERN_ERR
"kcapi: driver has no add card function.\n");
1072 retval
= driver
->add_card(driver
, &cparams
);
1074 mutex_unlock(&capi_drivers_lock
);
1078 case AVMB1_LOAD_AND_CONFIG
:
1080 if (cmd
== AVMB1_LOAD
) {
1081 if (copy_from_user(&ldef
, data
,
1082 sizeof(avmb1_loaddef
)))
1084 ldef
.t4config
.len
= 0;
1085 ldef
.t4config
.data
= NULL
;
1087 if (copy_from_user(&ldef
, data
,
1088 sizeof(avmb1_loadandconfigdef
)))
1092 mutex_lock(&capi_controller_lock
);
1094 ctr
= get_capi_ctr_by_nr(ldef
.contr
);
1097 goto load_unlock_out
;
1100 if (ctr
->load_firmware
== NULL
) {
1101 printk(KERN_DEBUG
"kcapi: load: no load function\n");
1103 goto load_unlock_out
;
1106 if (ldef
.t4file
.len
<= 0) {
1107 printk(KERN_DEBUG
"kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef
.t4file
.len
);
1109 goto load_unlock_out
;
1111 if (ldef
.t4file
.data
== NULL
) {
1112 printk(KERN_DEBUG
"kcapi: load: invalid parameter: dataptr is 0\n");
1114 goto load_unlock_out
;
1117 ldata
.firmware
.user
= 1;
1118 ldata
.firmware
.data
= ldef
.t4file
.data
;
1119 ldata
.firmware
.len
= ldef
.t4file
.len
;
1120 ldata
.configuration
.user
= 1;
1121 ldata
.configuration
.data
= ldef
.t4config
.data
;
1122 ldata
.configuration
.len
= ldef
.t4config
.len
;
1124 if (ctr
->state
!= CAPI_CTR_DETECTED
) {
1125 printk(KERN_INFO
"kcapi: load: contr=%d not in detect state\n", ldef
.contr
);
1127 goto load_unlock_out
;
1129 ctr
->state
= CAPI_CTR_LOADING
;
1131 retval
= ctr
->load_firmware(ctr
, &ldata
);
1133 ctr
->state
= CAPI_CTR_DETECTED
;
1134 goto load_unlock_out
;
1137 retval
= wait_on_ctr_state(ctr
, CAPI_CTR_RUNNING
);
1140 mutex_unlock(&capi_controller_lock
);
1143 case AVMB1_RESETCARD
:
1144 if (copy_from_user(&rdef
, data
, sizeof(avmb1_resetdef
)))
1149 mutex_lock(&capi_controller_lock
);
1151 ctr
= get_capi_ctr_by_nr(rdef
.contr
);
1154 goto reset_unlock_out
;
1157 if (ctr
->state
== CAPI_CTR_DETECTED
)
1158 goto reset_unlock_out
;
1160 if (ctr
->reset_ctr
== NULL
) {
1161 printk(KERN_DEBUG
"kcapi: reset: no reset function\n");
1163 goto reset_unlock_out
;
1166 ctr
->reset_ctr(ctr
);
1168 retval
= wait_on_ctr_state(ctr
, CAPI_CTR_DETECTED
);
1171 mutex_unlock(&capi_controller_lock
);
1179 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1183 * Perform manufacturer specific command.
1184 * Return value: CAPI result code
1187 int capi20_manufacturer(unsigned int cmd
, void __user
*data
)
1189 struct capi_ctr
*ctr
;
1195 case AVMB1_LOAD_AND_CONFIG
:
1196 case AVMB1_RESETCARD
:
1197 case AVMB1_GET_CARDINFO
:
1198 case AVMB1_REMOVECARD
:
1199 return old_capi_manufacturer(cmd
, data
);
1201 case KCAPI_CMD_TRACE
:
1205 if (copy_from_user(&fdef
, data
, sizeof(kcapi_flagdef
)))
1208 mutex_lock(&capi_controller_lock
);
1210 ctr
= get_capi_ctr_by_nr(fdef
.contr
);
1212 ctr
->traceflag
= fdef
.flag
;
1213 printk(KERN_INFO
"kcapi: contr [%03d] set trace=%d\n",
1214 ctr
->cnr
, ctr
->traceflag
);
1219 mutex_unlock(&capi_controller_lock
);
1223 case KCAPI_CMD_ADDCARD
:
1225 struct list_head
*l
;
1226 struct capi_driver
*driver
= NULL
;
1227 capicardparams cparams
;
1230 if ((retval
= copy_from_user(&cdef
, data
, sizeof(cdef
))))
1233 cparams
.port
= cdef
.port
;
1234 cparams
.irq
= cdef
.irq
;
1235 cparams
.membase
= cdef
.membase
;
1236 cparams
.cardnr
= cdef
.cardnr
;
1237 cparams
.cardtype
= 0;
1238 cdef
.driver
[sizeof(cdef
.driver
) - 1] = 0;
1240 mutex_lock(&capi_drivers_lock
);
1242 list_for_each(l
, &capi_drivers
) {
1243 driver
= list_entry(l
, struct capi_driver
, list
);
1244 if (strcmp(driver
->name
, cdef
.driver
) == 0)
1247 if (driver
== NULL
) {
1248 printk(KERN_ERR
"kcapi: driver \"%s\" not loaded.\n",
1251 } else if (!driver
->add_card
) {
1252 printk(KERN_ERR
"kcapi: driver \"%s\" has no add card function.\n", cdef
.driver
);
1255 retval
= driver
->add_card(driver
, &cparams
);
1257 mutex_unlock(&capi_drivers_lock
);
1262 printk(KERN_ERR
"kcapi: manufacturer command %d unknown.\n",
1270 EXPORT_SYMBOL(capi20_manufacturer
);
1272 /* ------------------------------------------------------------- */
1273 /* -------- Init & Cleanup ------------------------------------- */
1274 /* ------------------------------------------------------------- */
1277 * init / exit functions
1280 static struct notifier_block capictr_nb
= {
1281 .notifier_call
= notify_handler
,
1282 .priority
= INT_MAX
,
1285 static int __init
kcapi_init(void)
1289 kcapi_wq
= alloc_workqueue("kcapi", 0, 0);
1293 register_capictr_notifier(&capictr_nb
);
1295 err
= cdebug_init();
1297 unregister_capictr_notifier(&capictr_nb
);
1298 destroy_workqueue(kcapi_wq
);
1306 static void __exit
kcapi_exit(void)
1310 unregister_capictr_notifier(&capictr_nb
);
1312 destroy_workqueue(kcapi_wq
);
1315 module_init(kcapi_init
);
1316 module_exit(kcapi_exit
);