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 <asm/uaccess.h>
31 #include <linux/isdn/capicmd.h>
32 #include <linux/isdn/capiutil.h>
34 #include <linux/b1lli.h>
36 #include <linux/mutex.h>
37 #include <linux/rcupdate.h>
39 static int showcapimsgs
= 0;
41 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
44 module_param(showcapimsgs
, uint
, 0);
46 /* ------------------------------------------------------------- */
48 struct capictr_event
{
49 struct work_struct work
;
54 /* ------------------------------------------------------------- */
56 static struct capi_version driver_version
= {2, 0, 1, 1<<4};
57 static char driver_serial
[CAPI_SERIAL_LEN
] = "0004711";
58 static char capi_manufakturer
[64] = "AVM Berlin";
60 #define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
62 LIST_HEAD(capi_drivers
);
63 DEFINE_MUTEX(capi_drivers_lock
);
65 struct capi_ctr
*capi_controller
[CAPI_MAXCONTR
];
66 DEFINE_MUTEX(capi_controller_lock
);
68 struct capi20_appl
*capi_applications
[CAPI_MAXAPPL
];
70 static int ncontrollers
;
72 static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list
);
74 /* -------- controller ref counting -------------------------------------- */
76 static inline struct capi_ctr
*
77 capi_ctr_get(struct capi_ctr
*ctr
)
79 if (!try_module_get(ctr
->owner
))
85 capi_ctr_put(struct capi_ctr
*ctr
)
87 module_put(ctr
->owner
);
90 /* ------------------------------------------------------------- */
92 static inline struct capi_ctr
*get_capi_ctr_by_nr(u16 contr
)
94 if (contr
- 1 >= CAPI_MAXCONTR
)
97 return capi_controller
[contr
- 1];
100 static inline struct capi20_appl
*get_capi_appl_by_nr(u16 applid
)
102 if (applid
- 1 >= CAPI_MAXAPPL
)
105 return rcu_dereference(capi_applications
[applid
- 1]);
108 /* -------- util functions ------------------------------------ */
110 static inline int capi_cmd_valid(u8 cmd
)
115 case CAPI_CONNECT_ACTIVE
:
116 case CAPI_CONNECT_B3_ACTIVE
:
117 case CAPI_CONNECT_B3
:
118 case CAPI_CONNECT_B3_T90_ACTIVE
:
120 case CAPI_DISCONNECT_B3
:
121 case CAPI_DISCONNECT
:
125 case CAPI_MANUFACTURER
:
127 case CAPI_SELECT_B_PROTOCOL
:
133 static inline int capi_subcmd_valid(u8 subcmd
)
145 /* ------------------------------------------------------------ */
148 register_appl(struct capi_ctr
*ctr
, u16 applid
, capi_register_params
*rparam
)
150 ctr
= capi_ctr_get(ctr
);
153 ctr
->register_appl(ctr
, applid
, rparam
);
155 printk(KERN_WARNING
"%s: cannot get controller resources\n",
160 static void release_appl(struct capi_ctr
*ctr
, u16 applid
)
162 DBG("applid %#x", applid
);
164 ctr
->release_appl(ctr
, applid
);
168 static void notify_up(u32 contr
)
170 struct capi20_appl
*ap
;
171 struct capi_ctr
*ctr
;
174 mutex_lock(&capi_controller_lock
);
176 if (showcapimsgs
& 1)
177 printk(KERN_DEBUG
"kcapi: notify up contr %d\n", contr
);
179 ctr
= get_capi_ctr_by_nr(contr
);
181 if (ctr
->state
== CAPI_CTR_RUNNING
)
184 ctr
->state
= CAPI_CTR_RUNNING
;
186 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
187 ap
= get_capi_appl_by_nr(applid
);
190 register_appl(ctr
, applid
, &ap
->rparam
);
193 wake_up_interruptible_all(&ctr
->state_wait_queue
);
195 printk(KERN_WARNING
"%s: invalid contr %d\n", __func__
, contr
);
198 mutex_unlock(&capi_controller_lock
);
201 static void ctr_down(struct capi_ctr
*ctr
, int new_state
)
203 struct capi20_appl
*ap
;
206 if (ctr
->state
== CAPI_CTR_DETECTED
|| ctr
->state
== CAPI_CTR_DETACHED
)
209 ctr
->state
= new_state
;
211 memset(ctr
->manu
, 0, sizeof(ctr
->manu
));
212 memset(&ctr
->version
, 0, sizeof(ctr
->version
));
213 memset(&ctr
->profile
, 0, sizeof(ctr
->profile
));
214 memset(ctr
->serial
, 0, sizeof(ctr
->serial
));
216 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
217 ap
= get_capi_appl_by_nr(applid
);
222 wake_up_interruptible_all(&ctr
->state_wait_queue
);
225 static void notify_down(u32 contr
)
227 struct capi_ctr
*ctr
;
229 mutex_lock(&capi_controller_lock
);
231 if (showcapimsgs
& 1)
232 printk(KERN_DEBUG
"kcapi: notify down contr %d\n", contr
);
234 ctr
= get_capi_ctr_by_nr(contr
);
236 ctr_down(ctr
, CAPI_CTR_DETECTED
);
238 printk(KERN_WARNING
"%s: invalid contr %d\n", __func__
, contr
);
240 mutex_unlock(&capi_controller_lock
);
244 notify_handler(struct notifier_block
*nb
, unsigned long val
, void *v
)
259 static void do_notify_work(struct work_struct
*work
)
261 struct capictr_event
*event
=
262 container_of(work
, struct capictr_event
, work
);
264 blocking_notifier_call_chain(&ctr_notifier_list
, event
->type
,
265 (void *)(long)event
->controller
);
270 * The notifier will result in adding/deleteing of devices. Devices can
271 * only removed in user process, not in bh.
273 static int notify_push(unsigned int event_type
, u32 controller
)
275 struct capictr_event
*event
= kmalloc(sizeof(*event
), GFP_ATOMIC
);
280 INIT_WORK(&event
->work
, do_notify_work
);
281 event
->type
= event_type
;
282 event
->controller
= controller
;
284 schedule_work(&event
->work
);
288 int register_capictr_notifier(struct notifier_block
*nb
)
290 return blocking_notifier_chain_register(&ctr_notifier_list
, nb
);
292 EXPORT_SYMBOL_GPL(register_capictr_notifier
);
294 int unregister_capictr_notifier(struct notifier_block
*nb
)
296 return blocking_notifier_chain_unregister(&ctr_notifier_list
, nb
);
298 EXPORT_SYMBOL_GPL(unregister_capictr_notifier
);
300 /* -------- Receiver ------------------------------------------ */
302 static void recv_handler(struct work_struct
*work
)
305 struct capi20_appl
*ap
=
306 container_of(work
, struct capi20_appl
, recv_work
);
308 if ((!ap
) || (ap
->release_in_progress
))
311 mutex_lock(&ap
->recv_mtx
);
312 while ((skb
= skb_dequeue(&ap
->recv_queue
))) {
313 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_IND
)
318 ap
->recv_message(ap
, skb
);
320 mutex_unlock(&ap
->recv_mtx
);
324 * capi_ctr_handle_message() - handle incoming CAPI message
325 * @ctr: controller descriptor structure.
326 * @appl: application ID.
329 * Called by hardware driver to pass a CAPI message to the application.
332 void capi_ctr_handle_message(struct capi_ctr
*ctr
, u16 appl
,
335 struct capi20_appl
*ap
;
340 if (ctr
->state
!= CAPI_CTR_RUNNING
) {
341 cdb
= capi_message2str(skb
->data
);
343 printk(KERN_INFO
"kcapi: controller [%03d] not active, got: %s",
347 printk(KERN_INFO
"kcapi: controller [%03d] not active, cannot trace\n",
352 cmd
= CAPIMSG_COMMAND(skb
->data
);
353 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
354 if (cmd
== CAPI_DATA_B3
&& subcmd
== CAPI_IND
) {
356 if (ctr
->traceflag
> 2)
363 showctl
|= (ctr
->traceflag
& 1);
366 printk(KERN_DEBUG
"kcapi: got [%03d] id#%d %s len=%u\n",
367 ctr
->cnr
, CAPIMSG_APPID(skb
->data
),
368 capi_cmd2str(cmd
, subcmd
),
369 CAPIMSG_LEN(skb
->data
));
371 cdb
= capi_message2str(skb
->data
);
373 printk(KERN_DEBUG
"kcapi: got [%03d] %s\n",
377 printk(KERN_DEBUG
"kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
378 ctr
->cnr
, CAPIMSG_APPID(skb
->data
),
379 capi_cmd2str(cmd
, subcmd
),
380 CAPIMSG_LEN(skb
->data
));
386 ap
= get_capi_appl_by_nr(CAPIMSG_APPID(skb
->data
));
389 cdb
= capi_message2str(skb
->data
);
391 printk(KERN_ERR
"kcapi: handle_message: applid %d state released (%s)\n",
392 CAPIMSG_APPID(skb
->data
), cdb
->buf
);
395 printk(KERN_ERR
"kcapi: handle_message: applid %d state released (%s) cannot trace\n",
396 CAPIMSG_APPID(skb
->data
),
397 capi_cmd2str(cmd
, subcmd
));
400 skb_queue_tail(&ap
->recv_queue
, skb
);
401 schedule_work(&ap
->recv_work
);
410 EXPORT_SYMBOL(capi_ctr_handle_message
);
413 * capi_ctr_ready() - signal CAPI controller ready
414 * @ctr: controller descriptor structure.
416 * Called by hardware driver to signal that the controller is up and running.
419 void capi_ctr_ready(struct capi_ctr
*ctr
)
421 printk(KERN_NOTICE
"kcapi: controller [%03d] \"%s\" ready.\n",
422 ctr
->cnr
, ctr
->name
);
424 notify_push(CAPICTR_UP
, ctr
->cnr
);
427 EXPORT_SYMBOL(capi_ctr_ready
);
430 * capi_ctr_down() - signal CAPI controller not ready
431 * @ctr: controller descriptor structure.
433 * Called by hardware driver to signal that the controller is down and
434 * unavailable for use.
437 void capi_ctr_down(struct capi_ctr
*ctr
)
439 printk(KERN_NOTICE
"kcapi: controller [%03d] down.\n", ctr
->cnr
);
441 notify_push(CAPICTR_DOWN
, ctr
->cnr
);
444 EXPORT_SYMBOL(capi_ctr_down
);
447 * capi_ctr_suspend_output() - suspend controller
448 * @ctr: controller descriptor structure.
450 * Called by hardware driver to stop data flow.
452 * Note: The caller is responsible for synchronizing concurrent state changes
453 * as well as invocations of capi_ctr_handle_message.
456 void capi_ctr_suspend_output(struct capi_ctr
*ctr
)
459 printk(KERN_DEBUG
"kcapi: controller [%03d] suspend\n",
465 EXPORT_SYMBOL(capi_ctr_suspend_output
);
468 * capi_ctr_resume_output() - resume controller
469 * @ctr: controller descriptor structure.
471 * Called by hardware driver to resume data flow.
473 * Note: The caller is responsible for synchronizing concurrent state changes
474 * as well as invocations of capi_ctr_handle_message.
477 void capi_ctr_resume_output(struct capi_ctr
*ctr
)
480 printk(KERN_DEBUG
"kcapi: controller [%03d] resumed\n",
486 EXPORT_SYMBOL(capi_ctr_resume_output
);
488 /* ------------------------------------------------------------- */
491 * attach_capi_ctr() - register CAPI controller
492 * @ctr: controller descriptor structure.
494 * Called by hardware driver to register a controller with the CAPI subsystem.
495 * Return value: 0 on success, error code < 0 on error
498 int attach_capi_ctr(struct capi_ctr
*ctr
)
502 mutex_lock(&capi_controller_lock
);
504 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
505 if (!capi_controller
[i
])
508 if (i
== CAPI_MAXCONTR
) {
509 mutex_unlock(&capi_controller_lock
);
510 printk(KERN_ERR
"kcapi: out of controller slots\n");
513 capi_controller
[i
] = ctr
;
515 ctr
->nrecvctlpkt
= 0;
516 ctr
->nrecvdatapkt
= 0;
517 ctr
->nsentctlpkt
= 0;
518 ctr
->nsentdatapkt
= 0;
520 ctr
->state
= CAPI_CTR_DETECTED
;
522 ctr
->traceflag
= showcapimsgs
;
523 init_waitqueue_head(&ctr
->state_wait_queue
);
525 sprintf(ctr
->procfn
, "capi/controllers/%d", ctr
->cnr
);
526 ctr
->procent
= proc_create_data(ctr
->procfn
, 0, NULL
, ctr
->proc_fops
, ctr
);
530 mutex_unlock(&capi_controller_lock
);
532 printk(KERN_NOTICE
"kcapi: controller [%03d]: %s attached\n",
533 ctr
->cnr
, ctr
->name
);
537 EXPORT_SYMBOL(attach_capi_ctr
);
540 * detach_capi_ctr() - unregister CAPI controller
541 * @ctr: controller descriptor structure.
543 * Called by hardware driver to remove the registration of a controller
544 * with the CAPI subsystem.
545 * Return value: 0 on success, error code < 0 on error
548 int detach_capi_ctr(struct capi_ctr
*ctr
)
552 mutex_lock(&capi_controller_lock
);
554 ctr_down(ctr
, CAPI_CTR_DETACHED
);
556 if (capi_controller
[ctr
->cnr
- 1] != ctr
) {
560 capi_controller
[ctr
->cnr
- 1] = NULL
;
564 remove_proc_entry(ctr
->procfn
, NULL
);
566 printk(KERN_NOTICE
"kcapi: controller [%03d]: %s unregistered\n",
567 ctr
->cnr
, ctr
->name
);
570 mutex_unlock(&capi_controller_lock
);
575 EXPORT_SYMBOL(detach_capi_ctr
);
578 * register_capi_driver() - register CAPI driver
579 * @driver: driver descriptor structure.
581 * Called by hardware driver to register itself with the CAPI subsystem.
584 void register_capi_driver(struct capi_driver
*driver
)
586 mutex_lock(&capi_drivers_lock
);
587 list_add_tail(&driver
->list
, &capi_drivers
);
588 mutex_unlock(&capi_drivers_lock
);
591 EXPORT_SYMBOL(register_capi_driver
);
594 * unregister_capi_driver() - unregister CAPI driver
595 * @driver: driver descriptor structure.
597 * Called by hardware driver to unregister itself from the CAPI subsystem.
600 void unregister_capi_driver(struct capi_driver
*driver
)
602 mutex_lock(&capi_drivers_lock
);
603 list_del(&driver
->list
);
604 mutex_unlock(&capi_drivers_lock
);
607 EXPORT_SYMBOL(unregister_capi_driver
);
609 /* ------------------------------------------------------------- */
610 /* -------- CAPI2.0 Interface ---------------------------------- */
611 /* ------------------------------------------------------------- */
614 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
616 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
617 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
620 u16
capi20_isinstalled(void)
622 u16 ret
= CAPI_REGNOTINSTALLED
;
625 mutex_lock(&capi_controller_lock
);
627 for (i
= 0; i
< CAPI_MAXCONTR
; i
++)
628 if (capi_controller
[i
] &&
629 capi_controller
[i
]->state
== CAPI_CTR_RUNNING
) {
634 mutex_unlock(&capi_controller_lock
);
639 EXPORT_SYMBOL(capi20_isinstalled
);
642 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
643 * @ap: CAPI application descriptor structure.
645 * Register an application's presence with CAPI.
646 * A unique application ID is assigned and stored in @ap->applid.
647 * After this function returns successfully, the message receive
648 * callback function @ap->recv_message() may be called at any time
649 * until capi20_release() has been called for the same @ap.
650 * Return value: CAPI result code
653 u16
capi20_register(struct capi20_appl
*ap
)
660 if (ap
->rparam
.datablklen
< 128)
661 return CAPI_LOGBLKSIZETOSMALL
;
664 ap
->nrecvdatapkt
= 0;
666 ap
->nsentdatapkt
= 0;
667 mutex_init(&ap
->recv_mtx
);
668 skb_queue_head_init(&ap
->recv_queue
);
669 INIT_WORK(&ap
->recv_work
, recv_handler
);
670 ap
->release_in_progress
= 0;
672 mutex_lock(&capi_controller_lock
);
674 for (applid
= 1; applid
<= CAPI_MAXAPPL
; applid
++) {
675 if (capi_applications
[applid
- 1] == NULL
)
678 if (applid
> CAPI_MAXAPPL
) {
679 mutex_unlock(&capi_controller_lock
);
680 return CAPI_TOOMANYAPPLS
;
684 capi_applications
[applid
- 1] = ap
;
686 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
687 if (!capi_controller
[i
] ||
688 capi_controller
[i
]->state
!= CAPI_CTR_RUNNING
)
690 register_appl(capi_controller
[i
], applid
, &ap
->rparam
);
693 mutex_unlock(&capi_controller_lock
);
695 if (showcapimsgs
& 1) {
696 printk(KERN_DEBUG
"kcapi: appl %d up\n", applid
);
702 EXPORT_SYMBOL(capi20_register
);
705 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
706 * @ap: CAPI application descriptor structure.
708 * Terminate an application's registration with CAPI.
709 * After this function returns successfully, the message receive
710 * callback function @ap->recv_message() will no longer be called.
711 * Return value: CAPI result code
714 u16
capi20_release(struct capi20_appl
*ap
)
718 DBG("applid %#x", ap
->applid
);
720 mutex_lock(&capi_controller_lock
);
722 ap
->release_in_progress
= 1;
723 capi_applications
[ap
->applid
- 1] = NULL
;
727 for (i
= 0; i
< CAPI_MAXCONTR
; i
++) {
728 if (!capi_controller
[i
] ||
729 capi_controller
[i
]->state
!= CAPI_CTR_RUNNING
)
731 release_appl(capi_controller
[i
], ap
->applid
);
734 mutex_unlock(&capi_controller_lock
);
736 flush_scheduled_work();
737 skb_queue_purge(&ap
->recv_queue
);
739 if (showcapimsgs
& 1) {
740 printk(KERN_DEBUG
"kcapi: appl %d down\n", ap
->applid
);
746 EXPORT_SYMBOL(capi20_release
);
749 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
750 * @ap: CAPI application descriptor structure.
751 * @skb: CAPI message.
753 * Transfer a single message to CAPI.
754 * Return value: CAPI result code
757 u16
capi20_put_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
759 struct capi_ctr
*ctr
;
763 DBG("applid %#x", ap
->applid
);
765 if (ncontrollers
== 0)
766 return CAPI_REGNOTINSTALLED
;
767 if ((ap
->applid
== 0) || ap
->release_in_progress
)
768 return CAPI_ILLAPPNR
;
770 || !capi_cmd_valid(CAPIMSG_COMMAND(skb
->data
))
771 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb
->data
)))
772 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL
;
775 * The controller reference is protected by the existence of the
776 * application passed to us. We assume that the caller properly
777 * synchronizes this service with capi20_release.
779 ctr
= get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb
->data
));
780 if (!ctr
|| ctr
->state
!= CAPI_CTR_RUNNING
)
781 return CAPI_REGNOTINSTALLED
;
783 return CAPI_SENDQUEUEFULL
;
785 cmd
= CAPIMSG_COMMAND(skb
->data
);
786 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
788 if (cmd
== CAPI_DATA_B3
&& subcmd
== CAPI_REQ
) {
791 if (ctr
->traceflag
> 2)
799 showctl
|= (ctr
->traceflag
& 1);
802 printk(KERN_DEBUG
"kcapi: put [%03d] id#%d %s len=%u\n",
803 CAPIMSG_CONTROLLER(skb
->data
),
804 CAPIMSG_APPID(skb
->data
),
805 capi_cmd2str(cmd
, subcmd
),
806 CAPIMSG_LEN(skb
->data
));
808 _cdebbuf
*cdb
= capi_message2str(skb
->data
);
810 printk(KERN_DEBUG
"kcapi: put [%03d] %s\n",
811 CAPIMSG_CONTROLLER(skb
->data
),
815 printk(KERN_DEBUG
"kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
816 CAPIMSG_CONTROLLER(skb
->data
),
817 CAPIMSG_APPID(skb
->data
),
818 capi_cmd2str(cmd
, subcmd
),
819 CAPIMSG_LEN(skb
->data
));
822 return ctr
->send_message(ctr
, skb
);
825 EXPORT_SYMBOL(capi20_put_message
);
828 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
829 * @contr: controller number.
830 * @buf: result buffer (64 bytes).
832 * Retrieve information about the manufacturer of the specified ISDN controller
833 * or (for @contr == 0) the driver itself.
834 * Return value: CAPI result code
837 u16
capi20_get_manufacturer(u32 contr
, u8
*buf
)
839 struct capi_ctr
*ctr
;
843 strlcpy(buf
, capi_manufakturer
, CAPI_MANUFACTURER_LEN
);
847 mutex_lock(&capi_controller_lock
);
849 ctr
= get_capi_ctr_by_nr(contr
);
850 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
851 strlcpy(buf
, ctr
->manu
, CAPI_MANUFACTURER_LEN
);
854 ret
= CAPI_REGNOTINSTALLED
;
856 mutex_unlock(&capi_controller_lock
);
860 EXPORT_SYMBOL(capi20_get_manufacturer
);
863 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
864 * @contr: controller number.
865 * @verp: result structure.
867 * Retrieve version information for the specified ISDN controller
868 * or (for @contr == 0) the driver itself.
869 * Return value: CAPI result code
872 u16
capi20_get_version(u32 contr
, struct capi_version
*verp
)
874 struct capi_ctr
*ctr
;
878 *verp
= driver_version
;
882 mutex_lock(&capi_controller_lock
);
884 ctr
= get_capi_ctr_by_nr(contr
);
885 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
886 memcpy(verp
, &ctr
->version
, sizeof(capi_version
));
889 ret
= CAPI_REGNOTINSTALLED
;
891 mutex_unlock(&capi_controller_lock
);
895 EXPORT_SYMBOL(capi20_get_version
);
898 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
899 * @contr: controller number.
900 * @serial: result buffer (8 bytes).
902 * Retrieve the serial number of the specified ISDN controller
903 * or (for @contr == 0) the driver itself.
904 * Return value: CAPI result code
907 u16
capi20_get_serial(u32 contr
, u8
*serial
)
909 struct capi_ctr
*ctr
;
913 strlcpy(serial
, driver_serial
, CAPI_SERIAL_LEN
);
917 mutex_lock(&capi_controller_lock
);
919 ctr
= get_capi_ctr_by_nr(contr
);
920 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
921 strlcpy(serial
, ctr
->serial
, CAPI_SERIAL_LEN
);
924 ret
= CAPI_REGNOTINSTALLED
;
926 mutex_unlock(&capi_controller_lock
);
930 EXPORT_SYMBOL(capi20_get_serial
);
933 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
934 * @contr: controller number.
935 * @profp: result structure.
937 * Retrieve capability information for the specified ISDN controller
938 * or (for @contr == 0) the number of installed controllers.
939 * Return value: CAPI result code
942 u16
capi20_get_profile(u32 contr
, struct capi_profile
*profp
)
944 struct capi_ctr
*ctr
;
948 profp
->ncontroller
= ncontrollers
;
952 mutex_lock(&capi_controller_lock
);
954 ctr
= get_capi_ctr_by_nr(contr
);
955 if (ctr
&& ctr
->state
== CAPI_CTR_RUNNING
) {
956 memcpy(profp
, &ctr
->profile
, sizeof(struct capi_profile
));
959 ret
= CAPI_REGNOTINSTALLED
;
961 mutex_unlock(&capi_controller_lock
);
965 EXPORT_SYMBOL(capi20_get_profile
);
967 /* Must be called with capi_controller_lock held. */
968 static int wait_on_ctr_state(struct capi_ctr
*ctr
, unsigned int state
)
973 ctr
= capi_ctr_get(ctr
);
978 prepare_to_wait(&ctr
->state_wait_queue
, &wait
,
981 if (ctr
->state
== state
)
983 if (ctr
->state
== CAPI_CTR_DETACHED
) {
987 if (signal_pending(current
)) {
992 mutex_unlock(&capi_controller_lock
);
994 mutex_lock(&capi_controller_lock
);
996 finish_wait(&ctr
->state_wait_queue
, &wait
);
1004 static int old_capi_manufacturer(unsigned int cmd
, void __user
*data
)
1006 avmb1_loadandconfigdef ldef
;
1007 avmb1_extcarddef cdef
;
1008 avmb1_resetdef rdef
;
1009 capicardparams cparams
;
1010 struct capi_ctr
*ctr
;
1011 struct capi_driver
*driver
= NULL
;
1013 struct list_head
*l
;
1018 case AVMB1_ADDCARD_WITH_TYPE
:
1019 if (cmd
== AVMB1_ADDCARD
) {
1020 if ((retval
= copy_from_user(&cdef
, data
,
1021 sizeof(avmb1_carddef
))))
1023 cdef
.cardtype
= AVM_CARDTYPE_B1
;
1025 if ((retval
= copy_from_user(&cdef
, data
,
1026 sizeof(avmb1_extcarddef
))))
1029 cparams
.port
= cdef
.port
;
1030 cparams
.irq
= cdef
.irq
;
1031 cparams
.cardnr
= cdef
.cardnr
;
1033 mutex_lock(&capi_drivers_lock
);
1035 switch (cdef
.cardtype
) {
1036 case AVM_CARDTYPE_B1
:
1037 list_for_each(l
, &capi_drivers
) {
1038 driver
= list_entry(l
, struct capi_driver
, list
);
1039 if (strcmp(driver
->name
, "b1isa") == 0)
1043 case AVM_CARDTYPE_T1
:
1044 list_for_each(l
, &capi_drivers
) {
1045 driver
= list_entry(l
, struct capi_driver
, list
);
1046 if (strcmp(driver
->name
, "t1isa") == 0)
1055 printk(KERN_ERR
"kcapi: driver not loaded.\n");
1057 } else if (!driver
->add_card
) {
1058 printk(KERN_ERR
"kcapi: driver has no add card function.\n");
1061 retval
= driver
->add_card(driver
, &cparams
);
1063 mutex_unlock(&capi_drivers_lock
);
1067 case AVMB1_LOAD_AND_CONFIG
:
1069 if (cmd
== AVMB1_LOAD
) {
1070 if (copy_from_user(&ldef
, data
,
1071 sizeof(avmb1_loaddef
)))
1073 ldef
.t4config
.len
= 0;
1074 ldef
.t4config
.data
= NULL
;
1076 if (copy_from_user(&ldef
, data
,
1077 sizeof(avmb1_loadandconfigdef
)))
1081 mutex_lock(&capi_controller_lock
);
1083 ctr
= get_capi_ctr_by_nr(ldef
.contr
);
1086 goto load_unlock_out
;
1089 if (ctr
->load_firmware
== NULL
) {
1090 printk(KERN_DEBUG
"kcapi: load: no load function\n");
1092 goto load_unlock_out
;
1095 if (ldef
.t4file
.len
<= 0) {
1096 printk(KERN_DEBUG
"kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef
.t4file
.len
);
1098 goto load_unlock_out
;
1100 if (ldef
.t4file
.data
== NULL
) {
1101 printk(KERN_DEBUG
"kcapi: load: invalid parameter: dataptr is 0\n");
1103 goto load_unlock_out
;
1106 ldata
.firmware
.user
= 1;
1107 ldata
.firmware
.data
= ldef
.t4file
.data
;
1108 ldata
.firmware
.len
= ldef
.t4file
.len
;
1109 ldata
.configuration
.user
= 1;
1110 ldata
.configuration
.data
= ldef
.t4config
.data
;
1111 ldata
.configuration
.len
= ldef
.t4config
.len
;
1113 if (ctr
->state
!= CAPI_CTR_DETECTED
) {
1114 printk(KERN_INFO
"kcapi: load: contr=%d not in detect state\n", ldef
.contr
);
1116 goto load_unlock_out
;
1118 ctr
->state
= CAPI_CTR_LOADING
;
1120 retval
= ctr
->load_firmware(ctr
, &ldata
);
1122 ctr
->state
= CAPI_CTR_DETECTED
;
1123 goto load_unlock_out
;
1126 retval
= wait_on_ctr_state(ctr
, CAPI_CTR_RUNNING
);
1129 mutex_unlock(&capi_controller_lock
);
1132 case AVMB1_RESETCARD
:
1133 if (copy_from_user(&rdef
, data
, sizeof(avmb1_resetdef
)))
1138 mutex_lock(&capi_controller_lock
);
1140 ctr
= get_capi_ctr_by_nr(rdef
.contr
);
1143 goto reset_unlock_out
;
1146 if (ctr
->state
== CAPI_CTR_DETECTED
)
1147 goto reset_unlock_out
;
1149 ctr
->reset_ctr(ctr
);
1151 retval
= wait_on_ctr_state(ctr
, CAPI_CTR_DETECTED
);
1154 mutex_unlock(&capi_controller_lock
);
1162 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1166 * Perform manufacturer specific command.
1167 * Return value: CAPI result code
1170 int capi20_manufacturer(unsigned int cmd
, void __user
*data
)
1172 struct capi_ctr
*ctr
;
1178 case AVMB1_LOAD_AND_CONFIG
:
1179 case AVMB1_RESETCARD
:
1180 case AVMB1_GET_CARDINFO
:
1181 case AVMB1_REMOVECARD
:
1182 return old_capi_manufacturer(cmd
, data
);
1184 case KCAPI_CMD_TRACE
:
1188 if (copy_from_user(&fdef
, data
, sizeof(kcapi_flagdef
)))
1191 mutex_lock(&capi_controller_lock
);
1193 ctr
= get_capi_ctr_by_nr(fdef
.contr
);
1195 ctr
->traceflag
= fdef
.flag
;
1196 printk(KERN_INFO
"kcapi: contr [%03d] set trace=%d\n",
1197 ctr
->cnr
, ctr
->traceflag
);
1202 mutex_unlock(&capi_controller_lock
);
1206 case KCAPI_CMD_ADDCARD
:
1208 struct list_head
*l
;
1209 struct capi_driver
*driver
= NULL
;
1210 capicardparams cparams
;
1213 if ((retval
= copy_from_user(&cdef
, data
, sizeof(cdef
))))
1216 cparams
.port
= cdef
.port
;
1217 cparams
.irq
= cdef
.irq
;
1218 cparams
.membase
= cdef
.membase
;
1219 cparams
.cardnr
= cdef
.cardnr
;
1220 cparams
.cardtype
= 0;
1221 cdef
.driver
[sizeof(cdef
.driver
)-1] = 0;
1223 mutex_lock(&capi_drivers_lock
);
1225 list_for_each(l
, &capi_drivers
) {
1226 driver
= list_entry(l
, struct capi_driver
, list
);
1227 if (strcmp(driver
->name
, cdef
.driver
) == 0)
1230 if (driver
== NULL
) {
1231 printk(KERN_ERR
"kcapi: driver \"%s\" not loaded.\n",
1234 } else if (!driver
->add_card
) {
1235 printk(KERN_ERR
"kcapi: driver \"%s\" has no add card function.\n", cdef
.driver
);
1238 retval
= driver
->add_card(driver
, &cparams
);
1240 mutex_unlock(&capi_drivers_lock
);
1245 printk(KERN_ERR
"kcapi: manufacturer command %d unknown.\n",
1253 EXPORT_SYMBOL(capi20_manufacturer
);
1255 /* ------------------------------------------------------------- */
1256 /* -------- Init & Cleanup ------------------------------------- */
1257 /* ------------------------------------------------------------- */
1260 * init / exit functions
1263 static struct notifier_block capictr_nb
= {
1264 .notifier_call
= notify_handler
,
1265 .priority
= INT_MAX
,
1268 static int __init
kcapi_init(void)
1272 register_capictr_notifier(&capictr_nb
);
1274 err
= cdebug_init();
1280 static void __exit
kcapi_exit(void)
1284 /* make sure all notifiers are finished */
1285 flush_scheduled_work();
1289 module_init(kcapi_init
);
1290 module_exit(kcapi_exit
);