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;
42 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
43 MODULE_AUTHOR("Carsten Paeth");
44 MODULE_LICENSE("GPL");
45 module_param(showcapimsgs
, uint
, 0);
47 /* ------------------------------------------------------------- */
49 struct capictr_event
{
50 struct work_struct work
;
55 /* ------------------------------------------------------------- */
57 static struct capi_version driver_version
= {2, 0, 1, 1<<4};
58 static char driver_serial
[CAPI_SERIAL_LEN
] = "0004711";
59 static char capi_manufakturer
[64] = "AVM Berlin";
61 #define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
63 LIST_HEAD(capi_drivers
);
64 DEFINE_MUTEX(capi_drivers_lock
);
66 struct capi_ctr
*capi_controller
[CAPI_MAXCONTR
];
67 DEFINE_MUTEX(capi_controller_lock
);
69 struct capi20_appl
*capi_applications
[CAPI_MAXAPPL
];
71 static int ncontrollers
;
73 static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list
);
75 /* -------- controller ref counting -------------------------------------- */
77 static inline struct capi_ctr
*
78 capi_ctr_get(struct capi_ctr
*ctr
)
80 if (!try_module_get(ctr
->owner
))
86 capi_ctr_put(struct capi_ctr
*ctr
)
88 module_put(ctr
->owner
);
91 /* ------------------------------------------------------------- */
93 static inline struct capi_ctr
*get_capi_ctr_by_nr(u16 contr
)
95 if (contr
- 1 >= CAPI_MAXCONTR
)
98 return capi_controller
[contr
- 1];
101 static inline struct capi20_appl
*get_capi_appl_by_nr(u16 applid
)
103 if (applid
- 1 >= CAPI_MAXAPPL
)
106 return rcu_dereference(capi_applications
[applid
- 1]);
109 /* -------- util functions ------------------------------------ */
111 static inline int capi_cmd_valid(u8 cmd
)
116 case CAPI_CONNECT_ACTIVE
:
117 case CAPI_CONNECT_B3_ACTIVE
:
118 case CAPI_CONNECT_B3
:
119 case CAPI_CONNECT_B3_T90_ACTIVE
:
121 case CAPI_DISCONNECT_B3
:
122 case CAPI_DISCONNECT
:
126 case CAPI_MANUFACTURER
:
128 case CAPI_SELECT_B_PROTOCOL
:
134 static inline int capi_subcmd_valid(u8 subcmd
)
146 /* ------------------------------------------------------------ */
149 register_appl(struct capi_ctr
*ctr
, u16 applid
, capi_register_params
*rparam
)
151 ctr
= capi_ctr_get(ctr
);
154 ctr
->register_appl(ctr
, applid
, rparam
);
156 printk(KERN_WARNING
"%s: cannot get controller resources\n",
161 static void release_appl(struct capi_ctr
*ctr
, u16 applid
)
163 DBG("applid %#x", applid
);
165 ctr
->release_appl(ctr
, applid
);
169 static void notify_up(u32 contr
)
171 struct capi20_appl
*ap
;
172 struct capi_ctr
*ctr
;
175 mutex_lock(&capi_controller_lock
);
177 if (showcapimsgs
& 1)
178 printk(KERN_DEBUG
"kcapi: notify up contr %d\n", contr
);
180 ctr
= get_capi_ctr_by_nr(contr
);
182 if (ctr
->state
== CAPI_CTR_RUNNING
)
185 ctr
->state
= CAPI_CTR_RUNNING
;
187 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
188 ap
= get_capi_appl_by_nr(applid
);
191 register_appl(ctr
, applid
, &ap
->rparam
);
194 wake_up_interruptible_all(&ctr
->state_wait_queue
);
196 printk(KERN_WARNING
"%s: invalid contr %d\n", __func__
, contr
);
199 mutex_unlock(&capi_controller_lock
);
202 static void ctr_down(struct capi_ctr
*ctr
, int new_state
)
204 struct capi20_appl
*ap
;
207 if (ctr
->state
== CAPI_CTR_DETECTED
|| ctr
->state
== CAPI_CTR_DETACHED
)
210 ctr
->state
= new_state
;
212 memset(ctr
->manu
, 0, sizeof(ctr
->manu
));
213 memset(&ctr
->version
, 0, sizeof(ctr
->version
));
214 memset(&ctr
->profile
, 0, sizeof(ctr
->profile
));
215 memset(ctr
->serial
, 0, sizeof(ctr
->serial
));
217 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
218 ap
= get_capi_appl_by_nr(applid
);
223 wake_up_interruptible_all(&ctr
->state_wait_queue
);
226 static void notify_down(u32 contr
)
228 struct capi_ctr
*ctr
;
230 mutex_lock(&capi_controller_lock
);
232 if (showcapimsgs
& 1)
233 printk(KERN_DEBUG
"kcapi: notify down contr %d\n", contr
);
235 ctr
= get_capi_ctr_by_nr(contr
);
237 ctr_down(ctr
, CAPI_CTR_DETECTED
);
239 printk(KERN_WARNING
"%s: invalid contr %d\n", __func__
, contr
);
241 mutex_unlock(&capi_controller_lock
);
245 notify_handler(struct notifier_block
*nb
, unsigned long val
, void *v
)
260 static void do_notify_work(struct work_struct
*work
)
262 struct capictr_event
*event
=
263 container_of(work
, struct capictr_event
, work
);
265 blocking_notifier_call_chain(&ctr_notifier_list
, event
->type
,
266 (void *)(long)event
->controller
);
271 * The notifier will result in adding/deleteing of devices. Devices can
272 * only removed in user process, not in bh.
274 static int notify_push(unsigned int event_type
, u32 controller
)
276 struct capictr_event
*event
= kmalloc(sizeof(*event
), GFP_ATOMIC
);
281 INIT_WORK(&event
->work
, do_notify_work
);
282 event
->type
= event_type
;
283 event
->controller
= controller
;
285 schedule_work(&event
->work
);
289 int register_capictr_notifier(struct notifier_block
*nb
)
291 return blocking_notifier_chain_register(&ctr_notifier_list
, nb
);
293 EXPORT_SYMBOL_GPL(register_capictr_notifier
);
295 int unregister_capictr_notifier(struct notifier_block
*nb
)
297 return blocking_notifier_chain_unregister(&ctr_notifier_list
, nb
);
299 EXPORT_SYMBOL_GPL(unregister_capictr_notifier
);
301 /* -------- Receiver ------------------------------------------ */
303 static void recv_handler(struct work_struct
*work
)
306 struct capi20_appl
*ap
=
307 container_of(work
, struct capi20_appl
, recv_work
);
309 if ((!ap
) || (ap
->release_in_progress
))
312 mutex_lock(&ap
->recv_mtx
);
313 while ((skb
= skb_dequeue(&ap
->recv_queue
))) {
314 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_IND
)
319 ap
->recv_message(ap
, skb
);
321 mutex_unlock(&ap
->recv_mtx
);
325 * capi_ctr_handle_message() - handle incoming CAPI message
326 * @ctr: controller descriptor structure.
327 * @appl: application ID.
330 * Called by hardware driver to pass a CAPI message to the application.
333 void capi_ctr_handle_message(struct capi_ctr
*ctr
, u16 appl
,
336 struct capi20_appl
*ap
;
341 if (ctr
->state
!= CAPI_CTR_RUNNING
) {
342 cdb
= capi_message2str(skb
->data
);
344 printk(KERN_INFO
"kcapi: controller [%03d] not active, got: %s",
348 printk(KERN_INFO
"kcapi: controller [%03d] not active, cannot trace\n",
353 cmd
= CAPIMSG_COMMAND(skb
->data
);
354 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
355 if (cmd
== CAPI_DATA_B3
&& subcmd
== CAPI_IND
) {
357 if (ctr
->traceflag
> 2)
364 showctl
|= (ctr
->traceflag
& 1);
367 printk(KERN_DEBUG
"kcapi: got [%03d] id#%d %s len=%u\n",
368 ctr
->cnr
, CAPIMSG_APPID(skb
->data
),
369 capi_cmd2str(cmd
, subcmd
),
370 CAPIMSG_LEN(skb
->data
));
372 cdb
= capi_message2str(skb
->data
);
374 printk(KERN_DEBUG
"kcapi: got [%03d] %s\n",
378 printk(KERN_DEBUG
"kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
379 ctr
->cnr
, CAPIMSG_APPID(skb
->data
),
380 capi_cmd2str(cmd
, subcmd
),
381 CAPIMSG_LEN(skb
->data
));
387 ap
= get_capi_appl_by_nr(CAPIMSG_APPID(skb
->data
));
390 cdb
= capi_message2str(skb
->data
);
392 printk(KERN_ERR
"kcapi: handle_message: applid %d state released (%s)\n",
393 CAPIMSG_APPID(skb
->data
), cdb
->buf
);
396 printk(KERN_ERR
"kcapi: handle_message: applid %d state released (%s) cannot trace\n",
397 CAPIMSG_APPID(skb
->data
),
398 capi_cmd2str(cmd
, subcmd
));
401 skb_queue_tail(&ap
->recv_queue
, skb
);
402 schedule_work(&ap
->recv_work
);
411 EXPORT_SYMBOL(capi_ctr_handle_message
);
414 * capi_ctr_ready() - signal CAPI controller ready
415 * @ctr: controller descriptor structure.
417 * Called by hardware driver to signal that the controller is up and running.
420 void capi_ctr_ready(struct capi_ctr
*ctr
)
422 printk(KERN_NOTICE
"kcapi: controller [%03d] \"%s\" ready.\n",
423 ctr
->cnr
, ctr
->name
);
425 notify_push(CAPICTR_UP
, ctr
->cnr
);
428 EXPORT_SYMBOL(capi_ctr_ready
);
431 * capi_ctr_down() - signal CAPI controller not ready
432 * @ctr: controller descriptor structure.
434 * Called by hardware driver to signal that the controller is down and
435 * unavailable for use.
438 void capi_ctr_down(struct capi_ctr
*ctr
)
440 printk(KERN_NOTICE
"kcapi: controller [%03d] down.\n", ctr
->cnr
);
442 notify_push(CAPICTR_DOWN
, ctr
->cnr
);
445 EXPORT_SYMBOL(capi_ctr_down
);
448 * capi_ctr_suspend_output() - suspend controller
449 * @ctr: controller descriptor structure.
451 * Called by hardware driver to stop data flow.
453 * Note: The caller is responsible for synchronizing concurrent state changes
454 * as well as invocations of capi_ctr_handle_message.
457 void capi_ctr_suspend_output(struct capi_ctr
*ctr
)
460 printk(KERN_DEBUG
"kcapi: controller [%03d] suspend\n",
466 EXPORT_SYMBOL(capi_ctr_suspend_output
);
469 * capi_ctr_resume_output() - resume controller
470 * @ctr: controller descriptor structure.
472 * Called by hardware driver to resume data flow.
474 * Note: The caller is responsible for synchronizing concurrent state changes
475 * as well as invocations of capi_ctr_handle_message.
478 void capi_ctr_resume_output(struct capi_ctr
*ctr
)
481 printk(KERN_DEBUG
"kcapi: controller [%03d] resumed\n",
487 EXPORT_SYMBOL(capi_ctr_resume_output
);
489 /* ------------------------------------------------------------- */
492 * attach_capi_ctr() - register CAPI controller
493 * @ctr: controller descriptor structure.
495 * Called by hardware driver to register a controller with the CAPI subsystem.
496 * Return value: 0 on success, error code < 0 on error
499 int attach_capi_ctr(struct capi_ctr
*ctr
)
503 mutex_lock(&capi_controller_lock
);
505 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
506 if (!capi_controller
[i
])
509 if (i
== CAPI_MAXCONTR
) {
510 mutex_unlock(&capi_controller_lock
);
511 printk(KERN_ERR
"kcapi: out of controller slots\n");
514 capi_controller
[i
] = ctr
;
516 ctr
->nrecvctlpkt
= 0;
517 ctr
->nrecvdatapkt
= 0;
518 ctr
->nsentctlpkt
= 0;
519 ctr
->nsentdatapkt
= 0;
521 ctr
->state
= CAPI_CTR_DETECTED
;
523 ctr
->traceflag
= showcapimsgs
;
524 init_waitqueue_head(&ctr
->state_wait_queue
);
526 sprintf(ctr
->procfn
, "capi/controllers/%d", ctr
->cnr
);
527 ctr
->procent
= proc_create_data(ctr
->procfn
, 0, NULL
, ctr
->proc_fops
, ctr
);
531 mutex_unlock(&capi_controller_lock
);
533 printk(KERN_NOTICE
"kcapi: controller [%03d]: %s attached\n",
534 ctr
->cnr
, ctr
->name
);
538 EXPORT_SYMBOL(attach_capi_ctr
);
541 * detach_capi_ctr() - unregister CAPI controller
542 * @ctr: controller descriptor structure.
544 * Called by hardware driver to remove the registration of a controller
545 * with the CAPI subsystem.
546 * Return value: 0 on success, error code < 0 on error
549 int detach_capi_ctr(struct capi_ctr
*ctr
)
553 mutex_lock(&capi_controller_lock
);
555 ctr_down(ctr
, CAPI_CTR_DETACHED
);
557 if (capi_controller
[ctr
->cnr
- 1] != ctr
) {
561 capi_controller
[ctr
->cnr
- 1] = NULL
;
565 remove_proc_entry(ctr
->procfn
, NULL
);
567 printk(KERN_NOTICE
"kcapi: controller [%03d]: %s unregistered\n",
568 ctr
->cnr
, ctr
->name
);
571 mutex_unlock(&capi_controller_lock
);
576 EXPORT_SYMBOL(detach_capi_ctr
);
579 * register_capi_driver() - register CAPI driver
580 * @driver: driver descriptor structure.
582 * Called by hardware driver to register itself with the CAPI subsystem.
585 void register_capi_driver(struct capi_driver
*driver
)
587 mutex_lock(&capi_drivers_lock
);
588 list_add_tail(&driver
->list
, &capi_drivers
);
589 mutex_unlock(&capi_drivers_lock
);
592 EXPORT_SYMBOL(register_capi_driver
);
595 * unregister_capi_driver() - unregister CAPI driver
596 * @driver: driver descriptor structure.
598 * Called by hardware driver to unregister itself from the CAPI subsystem.
601 void unregister_capi_driver(struct capi_driver
*driver
)
603 mutex_lock(&capi_drivers_lock
);
604 list_del(&driver
->list
);
605 mutex_unlock(&capi_drivers_lock
);
608 EXPORT_SYMBOL(unregister_capi_driver
);
610 /* ------------------------------------------------------------- */
611 /* -------- CAPI2.0 Interface ---------------------------------- */
612 /* ------------------------------------------------------------- */
615 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
617 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
618 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
621 u16
capi20_isinstalled(void)
623 u16 ret
= CAPI_REGNOTINSTALLED
;
626 mutex_lock(&capi_controller_lock
);
628 for (i
= 0; i
< CAPI_MAXCONTR
; i
++)
629 if (capi_controller
[i
] &&
630 capi_controller
[i
]->state
== CAPI_CTR_RUNNING
) {
635 mutex_unlock(&capi_controller_lock
);
640 EXPORT_SYMBOL(capi20_isinstalled
);
643 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
644 * @ap: CAPI application descriptor structure.
646 * Register an application's presence with CAPI.
647 * A unique application ID is assigned and stored in @ap->applid.
648 * After this function returns successfully, the message receive
649 * callback function @ap->recv_message() may be called at any time
650 * until capi20_release() has been called for the same @ap.
651 * Return value: CAPI result code
654 u16
capi20_register(struct capi20_appl
*ap
)
661 if (ap
->rparam
.datablklen
< 128)
662 return CAPI_LOGBLKSIZETOSMALL
;
665 ap
->nrecvdatapkt
= 0;
667 ap
->nsentdatapkt
= 0;
668 mutex_init(&ap
->recv_mtx
);
669 skb_queue_head_init(&ap
->recv_queue
);
670 INIT_WORK(&ap
->recv_work
, recv_handler
);
671 ap
->release_in_progress
= 0;
673 mutex_lock(&capi_controller_lock
);
675 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
676 if (capi_applications
[applid
- 1] == NULL
)
679 if (applid
> CAPI_MAXAPPL
) {
680 mutex_unlock(&capi_controller_lock
);
681 return CAPI_TOOMANYAPPLS
;
685 capi_applications
[applid
- 1] = ap
;
687 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
688 if (!capi_controller
[i
] ||
689 capi_controller
[i
]->state
!= CAPI_CTR_RUNNING
)
691 register_appl(capi_controller
[i
], applid
, &ap
->rparam
);
694 mutex_unlock(&capi_controller_lock
);
696 if (showcapimsgs
& 1) {
697 printk(KERN_DEBUG
"kcapi: appl %d up\n", applid
);
703 EXPORT_SYMBOL(capi20_register
);
706 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
707 * @ap: CAPI application descriptor structure.
709 * Terminate an application's registration with CAPI.
710 * After this function returns successfully, the message receive
711 * callback function @ap->recv_message() will no longer be called.
712 * Return value: CAPI result code
715 u16
capi20_release(struct capi20_appl
*ap
)
719 DBG("applid %#x", ap
->applid
);
721 mutex_lock(&capi_controller_lock
);
723 ap
->release_in_progress
= 1;
724 capi_applications
[ap
->applid
- 1] = NULL
;
728 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
729 if (!capi_controller
[i
] ||
730 capi_controller
[i
]->state
!= CAPI_CTR_RUNNING
)
732 release_appl(capi_controller
[i
], ap
->applid
);
735 mutex_unlock(&capi_controller_lock
);
737 flush_scheduled_work();
738 skb_queue_purge(&ap
->recv_queue
);
740 if (showcapimsgs
& 1) {
741 printk(KERN_DEBUG
"kcapi: appl %d down\n", ap
->applid
);
747 EXPORT_SYMBOL(capi20_release
);
750 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
751 * @ap: CAPI application descriptor structure.
752 * @skb: CAPI message.
754 * Transfer a single message to CAPI.
755 * Return value: CAPI result code
758 u16
capi20_put_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
760 struct capi_ctr
*ctr
;
764 DBG("applid %#x", ap
->applid
);
766 if (ncontrollers
== 0)
767 return CAPI_REGNOTINSTALLED
;
768 if ((ap
->applid
== 0) || ap
->release_in_progress
)
769 return CAPI_ILLAPPNR
;
771 || !capi_cmd_valid(CAPIMSG_COMMAND(skb
->data
))
772 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb
->data
)))
773 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL
;
776 * The controller reference is protected by the existence of the
777 * application passed to us. We assume that the caller properly
778 * synchronizes this service with capi20_release.
780 ctr
= get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb
->data
));
781 if (!ctr
|| ctr
->state
!= CAPI_CTR_RUNNING
)
782 return CAPI_REGNOTINSTALLED
;
784 return CAPI_SENDQUEUEFULL
;
786 cmd
= CAPIMSG_COMMAND(skb
->data
);
787 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
789 if (cmd
== CAPI_DATA_B3
&& subcmd
== CAPI_REQ
) {
792 if (ctr
->traceflag
> 2)
800 showctl
|= (ctr
->traceflag
& 1);
803 printk(KERN_DEBUG
"kcapi: put [%03d] id#%d %s len=%u\n",
804 CAPIMSG_CONTROLLER(skb
->data
),
805 CAPIMSG_APPID(skb
->data
),
806 capi_cmd2str(cmd
, subcmd
),
807 CAPIMSG_LEN(skb
->data
));
809 _cdebbuf
*cdb
= capi_message2str(skb
->data
);
811 printk(KERN_DEBUG
"kcapi: put [%03d] %s\n",
812 CAPIMSG_CONTROLLER(skb
->data
),
816 printk(KERN_DEBUG
"kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
817 CAPIMSG_CONTROLLER(skb
->data
),
818 CAPIMSG_APPID(skb
->data
),
819 capi_cmd2str(cmd
, subcmd
),
820 CAPIMSG_LEN(skb
->data
));
823 return ctr
->send_message(ctr
, skb
);
826 EXPORT_SYMBOL(capi20_put_message
);
829 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
830 * @contr: controller number.
831 * @buf: result buffer (64 bytes).
833 * Retrieve information about the manufacturer of the specified ISDN controller
834 * or (for @contr == 0) the driver itself.
835 * Return value: CAPI result code
838 u16
capi20_get_manufacturer(u32 contr
, u8
*buf
)
840 struct capi_ctr
*ctr
;
844 strlcpy(buf
, capi_manufakturer
, CAPI_MANUFACTURER_LEN
);
848 mutex_lock(&capi_controller_lock
);
850 ctr
= get_capi_ctr_by_nr(contr
);
851 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
852 strlcpy(buf
, ctr
->manu
, CAPI_MANUFACTURER_LEN
);
855 ret
= CAPI_REGNOTINSTALLED
;
857 mutex_unlock(&capi_controller_lock
);
861 EXPORT_SYMBOL(capi20_get_manufacturer
);
864 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
865 * @contr: controller number.
866 * @verp: result structure.
868 * Retrieve version information for the specified ISDN controller
869 * or (for @contr == 0) the driver itself.
870 * Return value: CAPI result code
873 u16
capi20_get_version(u32 contr
, struct capi_version
*verp
)
875 struct capi_ctr
*ctr
;
879 *verp
= driver_version
;
883 mutex_lock(&capi_controller_lock
);
885 ctr
= get_capi_ctr_by_nr(contr
);
886 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
887 memcpy(verp
, &ctr
->version
, sizeof(capi_version
));
890 ret
= CAPI_REGNOTINSTALLED
;
892 mutex_unlock(&capi_controller_lock
);
896 EXPORT_SYMBOL(capi20_get_version
);
899 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
900 * @contr: controller number.
901 * @serial: result buffer (8 bytes).
903 * Retrieve the serial number of the specified ISDN controller
904 * or (for @contr == 0) the driver itself.
905 * Return value: CAPI result code
908 u16
capi20_get_serial(u32 contr
, u8
*serial
)
910 struct capi_ctr
*ctr
;
914 strlcpy(serial
, driver_serial
, CAPI_SERIAL_LEN
);
918 mutex_lock(&capi_controller_lock
);
920 ctr
= get_capi_ctr_by_nr(contr
);
921 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
922 strlcpy(serial
, ctr
->serial
, CAPI_SERIAL_LEN
);
925 ret
= CAPI_REGNOTINSTALLED
;
927 mutex_unlock(&capi_controller_lock
);
931 EXPORT_SYMBOL(capi20_get_serial
);
934 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
935 * @contr: controller number.
936 * @profp: result structure.
938 * Retrieve capability information for the specified ISDN controller
939 * or (for @contr == 0) the number of installed controllers.
940 * Return value: CAPI result code
943 u16
capi20_get_profile(u32 contr
, struct capi_profile
*profp
)
945 struct capi_ctr
*ctr
;
949 profp
->ncontroller
= ncontrollers
;
953 mutex_lock(&capi_controller_lock
);
955 ctr
= get_capi_ctr_by_nr(contr
);
956 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
957 memcpy(profp
, &ctr
->profile
, sizeof(struct capi_profile
));
960 ret
= CAPI_REGNOTINSTALLED
;
962 mutex_unlock(&capi_controller_lock
);
966 EXPORT_SYMBOL(capi20_get_profile
);
968 /* Must be called with capi_controller_lock held. */
969 static int wait_on_ctr_state(struct capi_ctr
*ctr
, unsigned int state
)
974 ctr
= capi_ctr_get(ctr
);
979 prepare_to_wait(&ctr
->state_wait_queue
, &wait
,
982 if (ctr
->state
== state
)
984 if (ctr
->state
== CAPI_CTR_DETACHED
) {
988 if (signal_pending(current
)) {
993 mutex_unlock(&capi_controller_lock
);
995 mutex_lock(&capi_controller_lock
);
997 finish_wait(&ctr
->state_wait_queue
, &wait
);
1005 static int old_capi_manufacturer(unsigned int cmd
, void __user
*data
)
1007 avmb1_loadandconfigdef ldef
;
1008 avmb1_extcarddef cdef
;
1009 avmb1_resetdef rdef
;
1010 capicardparams cparams
;
1011 struct capi_ctr
*ctr
;
1012 struct capi_driver
*driver
= NULL
;
1014 struct list_head
*l
;
1019 case AVMB1_ADDCARD_WITH_TYPE
:
1020 if (cmd
== AVMB1_ADDCARD
) {
1021 if ((retval
= copy_from_user(&cdef
, data
,
1022 sizeof(avmb1_carddef
))))
1024 cdef
.cardtype
= AVM_CARDTYPE_B1
;
1026 if ((retval
= copy_from_user(&cdef
, data
,
1027 sizeof(avmb1_extcarddef
))))
1030 cparams
.port
= cdef
.port
;
1031 cparams
.irq
= cdef
.irq
;
1032 cparams
.cardnr
= cdef
.cardnr
;
1034 mutex_lock(&capi_drivers_lock
);
1036 switch (cdef
.cardtype
) {
1037 case AVM_CARDTYPE_B1
:
1038 list_for_each(l
, &capi_drivers
) {
1039 driver
= list_entry(l
, struct capi_driver
, list
);
1040 if (strcmp(driver
->name
, "b1isa") == 0)
1044 case AVM_CARDTYPE_T1
:
1045 list_for_each(l
, &capi_drivers
) {
1046 driver
= list_entry(l
, struct capi_driver
, list
);
1047 if (strcmp(driver
->name
, "t1isa") == 0)
1056 printk(KERN_ERR
"kcapi: driver not loaded.\n");
1058 } else if (!driver
->add_card
) {
1059 printk(KERN_ERR
"kcapi: driver has no add card function.\n");
1062 retval
= driver
->add_card(driver
, &cparams
);
1064 mutex_unlock(&capi_drivers_lock
);
1068 case AVMB1_LOAD_AND_CONFIG
:
1070 if (cmd
== AVMB1_LOAD
) {
1071 if (copy_from_user(&ldef
, data
,
1072 sizeof(avmb1_loaddef
)))
1074 ldef
.t4config
.len
= 0;
1075 ldef
.t4config
.data
= NULL
;
1077 if (copy_from_user(&ldef
, data
,
1078 sizeof(avmb1_loadandconfigdef
)))
1082 mutex_lock(&capi_controller_lock
);
1084 ctr
= get_capi_ctr_by_nr(ldef
.contr
);
1087 goto load_unlock_out
;
1090 if (ctr
->load_firmware
== NULL
) {
1091 printk(KERN_DEBUG
"kcapi: load: no load function\n");
1093 goto load_unlock_out
;
1096 if (ldef
.t4file
.len
<= 0) {
1097 printk(KERN_DEBUG
"kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef
.t4file
.len
);
1099 goto load_unlock_out
;
1101 if (ldef
.t4file
.data
== NULL
) {
1102 printk(KERN_DEBUG
"kcapi: load: invalid parameter: dataptr is 0\n");
1104 goto load_unlock_out
;
1107 ldata
.firmware
.user
= 1;
1108 ldata
.firmware
.data
= ldef
.t4file
.data
;
1109 ldata
.firmware
.len
= ldef
.t4file
.len
;
1110 ldata
.configuration
.user
= 1;
1111 ldata
.configuration
.data
= ldef
.t4config
.data
;
1112 ldata
.configuration
.len
= ldef
.t4config
.len
;
1114 if (ctr
->state
!= CAPI_CTR_DETECTED
) {
1115 printk(KERN_INFO
"kcapi: load: contr=%d not in detect state\n", ldef
.contr
);
1117 goto load_unlock_out
;
1119 ctr
->state
= CAPI_CTR_LOADING
;
1121 retval
= ctr
->load_firmware(ctr
, &ldata
);
1123 ctr
->state
= CAPI_CTR_DETECTED
;
1124 goto load_unlock_out
;
1127 retval
= wait_on_ctr_state(ctr
, CAPI_CTR_RUNNING
);
1130 mutex_unlock(&capi_controller_lock
);
1133 case AVMB1_RESETCARD
:
1134 if (copy_from_user(&rdef
, data
, sizeof(avmb1_resetdef
)))
1139 mutex_lock(&capi_controller_lock
);
1141 ctr
= get_capi_ctr_by_nr(rdef
.contr
);
1144 goto reset_unlock_out
;
1147 if (ctr
->state
== CAPI_CTR_DETECTED
)
1148 goto reset_unlock_out
;
1150 if (ctr
->reset_ctr
== NULL
) {
1151 printk(KERN_DEBUG
"kcapi: reset: no reset function\n");
1153 goto reset_unlock_out
;
1156 ctr
->reset_ctr(ctr
);
1158 retval
= wait_on_ctr_state(ctr
, CAPI_CTR_DETECTED
);
1161 mutex_unlock(&capi_controller_lock
);
1169 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1173 * Perform manufacturer specific command.
1174 * Return value: CAPI result code
1177 int capi20_manufacturer(unsigned int cmd
, void __user
*data
)
1179 struct capi_ctr
*ctr
;
1185 case AVMB1_LOAD_AND_CONFIG
:
1186 case AVMB1_RESETCARD
:
1187 case AVMB1_GET_CARDINFO
:
1188 case AVMB1_REMOVECARD
:
1189 return old_capi_manufacturer(cmd
, data
);
1191 case KCAPI_CMD_TRACE
:
1195 if (copy_from_user(&fdef
, data
, sizeof(kcapi_flagdef
)))
1198 mutex_lock(&capi_controller_lock
);
1200 ctr
= get_capi_ctr_by_nr(fdef
.contr
);
1202 ctr
->traceflag
= fdef
.flag
;
1203 printk(KERN_INFO
"kcapi: contr [%03d] set trace=%d\n",
1204 ctr
->cnr
, ctr
->traceflag
);
1209 mutex_unlock(&capi_controller_lock
);
1213 case KCAPI_CMD_ADDCARD
:
1215 struct list_head
*l
;
1216 struct capi_driver
*driver
= NULL
;
1217 capicardparams cparams
;
1220 if ((retval
= copy_from_user(&cdef
, data
, sizeof(cdef
))))
1223 cparams
.port
= cdef
.port
;
1224 cparams
.irq
= cdef
.irq
;
1225 cparams
.membase
= cdef
.membase
;
1226 cparams
.cardnr
= cdef
.cardnr
;
1227 cparams
.cardtype
= 0;
1228 cdef
.driver
[sizeof(cdef
.driver
)-1] = 0;
1230 mutex_lock(&capi_drivers_lock
);
1232 list_for_each(l
, &capi_drivers
) {
1233 driver
= list_entry(l
, struct capi_driver
, list
);
1234 if (strcmp(driver
->name
, cdef
.driver
) == 0)
1237 if (driver
== NULL
) {
1238 printk(KERN_ERR
"kcapi: driver \"%s\" not loaded.\n",
1241 } else if (!driver
->add_card
) {
1242 printk(KERN_ERR
"kcapi: driver \"%s\" has no add card function.\n", cdef
.driver
);
1245 retval
= driver
->add_card(driver
, &cparams
);
1247 mutex_unlock(&capi_drivers_lock
);
1252 printk(KERN_ERR
"kcapi: manufacturer command %d unknown.\n",
1260 EXPORT_SYMBOL(capi20_manufacturer
);
1262 /* ------------------------------------------------------------- */
1263 /* -------- Init & Cleanup ------------------------------------- */
1264 /* ------------------------------------------------------------- */
1267 * init / exit functions
1270 static struct notifier_block capictr_nb
= {
1271 .notifier_call
= notify_handler
,
1272 .priority
= INT_MAX
,
1275 static int __init
kcapi_init(void)
1279 register_capictr_notifier(&capictr_nb
);
1281 err
= cdebug_init();
1287 static void __exit
kcapi_exit(void)
1291 /* make sure all notifiers are finished */
1292 flush_scheduled_work();
1296 module_init(kcapi_init
);
1297 module_exit(kcapi_exit
);