2 * Telephony registration for Linux
4 * (c) Copyright 1999 Red Hat Software Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Author: Alan Cox, <alan@redhat.com>
13 * Fixes: Mar 01 2000 Thomas Sparr, <thomas.l.sparr@telia.com>
14 * phone_register_device now works with unit!=PHONE_UNIT_ANY
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/phonedev.h>
25 #include <linux/init.h>
26 #include <linux/smp_lock.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
30 #include <linux/kmod.h>
31 #include <linux/sem.h>
32 #include <linux/mutex.h>
34 #define PHONE_NUM_DEVICES 256
40 static struct phone_device
*phone_device
[PHONE_NUM_DEVICES
];
41 static DEFINE_MUTEX(phone_lock
);
44 * Open a phone device.
47 static int phone_open(struct inode
*inode
, struct file
*file
)
49 unsigned int minor
= iminor(inode
);
51 struct phone_device
*p
;
52 const struct file_operations
*old_fops
, *new_fops
= NULL
;
54 if (minor
>= PHONE_NUM_DEVICES
)
58 mutex_lock(&phone_lock
);
59 p
= phone_device
[minor
];
61 new_fops
= fops_get(p
->f_op
);
63 mutex_unlock(&phone_lock
);
64 request_module("char-major-%d-%d", PHONE_MAJOR
, minor
);
65 mutex_lock(&phone_lock
);
66 p
= phone_device
[minor
];
67 if (p
== NULL
|| (new_fops
= fops_get(p
->f_op
)) == NULL
)
73 old_fops
= file
->f_op
;
74 file
->f_op
= new_fops
;
76 err
= p
->open(p
, file
); /* Tell the device it is open */
79 file
->f_op
= fops_get(old_fops
);
83 mutex_unlock(&phone_lock
);
89 * Telephony For Linux device drivers request registration here.
92 int phone_register_device(struct phone_device
*p
, int unit
)
99 end
= PHONE_NUM_DEVICES
- 1;
101 if (unit
!= PHONE_UNIT_ANY
) {
103 end
= unit
+ 1; /* enter the loop at least one time */
106 mutex_lock(&phone_lock
);
107 for (i
= base
; i
< end
; i
++) {
108 if (phone_device
[i
] == NULL
) {
111 mutex_unlock(&phone_lock
);
115 mutex_unlock(&phone_lock
);
120 * Unregister an unused Telephony for linux device
123 void phone_unregister_device(struct phone_device
*pfd
)
125 mutex_lock(&phone_lock
);
126 if (likely(phone_device
[pfd
->minor
] == pfd
))
127 phone_device
[pfd
->minor
] = NULL
;
128 mutex_unlock(&phone_lock
);
132 static const struct file_operations phone_fops
=
134 .owner
= THIS_MODULE
,
139 * Board init functions
144 * Initialise Telephony for linux
147 static int __init
telephony_init(void)
149 printk(KERN_INFO
"Linux telephony interface: v1.00\n");
150 if (register_chrdev(PHONE_MAJOR
, "telephony", &phone_fops
)) {
151 printk("phonedev: unable to get major %d\n", PHONE_MAJOR
);
158 static void __exit
telephony_exit(void)
160 unregister_chrdev(PHONE_MAJOR
, "telephony");
163 module_init(telephony_init
);
164 module_exit(telephony_exit
);
166 MODULE_LICENSE("GPL");
168 EXPORT_SYMBOL(phone_register_device
);
169 EXPORT_SYMBOL(phone_unregister_device
);