Blackfin arch: give sys_strace proper entry markings
[linux-2.6/btrfs-unstable.git] / drivers / isdn / mISDN / core.c
blob33068177b7c9486da1866344d2e25654ec2ad7d1
1 /*
2 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/types.h>
16 #include <linux/stddef.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/mISDNif.h>
20 #include "core.h"
22 static u_int debug;
24 MODULE_AUTHOR("Karsten Keil");
25 MODULE_LICENSE("GPL");
26 module_param(debug, uint, S_IRUGO | S_IWUSR);
28 static LIST_HEAD(devices);
29 DEFINE_RWLOCK(device_lock);
30 static u64 device_ids;
31 #define MAX_DEVICE_ID 63
33 static LIST_HEAD(Bprotocols);
34 DEFINE_RWLOCK(bp_lock);
36 struct mISDNdevice
37 *get_mdevice(u_int id)
39 struct mISDNdevice *dev;
41 read_lock(&device_lock);
42 list_for_each_entry(dev, &devices, D.list)
43 if (dev->id == id) {
44 read_unlock(&device_lock);
45 return dev;
47 read_unlock(&device_lock);
48 return NULL;
51 int
52 get_mdevice_count(void)
54 struct mISDNdevice *dev;
55 int cnt = 0;
57 read_lock(&device_lock);
58 list_for_each_entry(dev, &devices, D.list)
59 cnt++;
60 read_unlock(&device_lock);
61 return cnt;
64 static int
65 get_free_devid(void)
67 u_int i;
69 for (i = 0; i <= MAX_DEVICE_ID; i++)
70 if (!test_and_set_bit(i, (u_long *)&device_ids))
71 return i;
72 return -1;
75 int
76 mISDN_register_device(struct mISDNdevice *dev, char *name)
78 u_long flags;
79 int err;
81 dev->id = get_free_devid();
82 if (dev->id < 0)
83 return -EBUSY;
84 if (name && name[0])
85 strcpy(dev->name, name);
86 else
87 sprintf(dev->name, "mISDN%d", dev->id);
88 if (debug & DEBUG_CORE)
89 printk(KERN_DEBUG "mISDN_register %s %d\n",
90 dev->name, dev->id);
91 err = create_stack(dev);
92 if (err)
93 return err;
94 write_lock_irqsave(&device_lock, flags);
95 list_add_tail(&dev->D.list, &devices);
96 write_unlock_irqrestore(&device_lock, flags);
97 return 0;
99 EXPORT_SYMBOL(mISDN_register_device);
101 void
102 mISDN_unregister_device(struct mISDNdevice *dev) {
103 u_long flags;
105 if (debug & DEBUG_CORE)
106 printk(KERN_DEBUG "mISDN_unregister %s %d\n",
107 dev->name, dev->id);
108 write_lock_irqsave(&device_lock, flags);
109 list_del(&dev->D.list);
110 write_unlock_irqrestore(&device_lock, flags);
111 test_and_clear_bit(dev->id, (u_long *)&device_ids);
112 delete_stack(dev);
114 EXPORT_SYMBOL(mISDN_unregister_device);
116 u_int
117 get_all_Bprotocols(void)
119 struct Bprotocol *bp;
120 u_int m = 0;
122 read_lock(&bp_lock);
123 list_for_each_entry(bp, &Bprotocols, list)
124 m |= bp->Bprotocols;
125 read_unlock(&bp_lock);
126 return m;
129 struct Bprotocol *
130 get_Bprotocol4mask(u_int m)
132 struct Bprotocol *bp;
134 read_lock(&bp_lock);
135 list_for_each_entry(bp, &Bprotocols, list)
136 if (bp->Bprotocols & m) {
137 read_unlock(&bp_lock);
138 return bp;
140 read_unlock(&bp_lock);
141 return NULL;
144 struct Bprotocol *
145 get_Bprotocol4id(u_int id)
147 u_int m;
149 if (id < ISDN_P_B_START || id > 63) {
150 printk(KERN_WARNING "%s id not in range %d\n",
151 __func__, id);
152 return NULL;
154 m = 1 << (id & ISDN_P_B_MASK);
155 return get_Bprotocol4mask(m);
159 mISDN_register_Bprotocol(struct Bprotocol *bp)
161 u_long flags;
162 struct Bprotocol *old;
164 if (debug & DEBUG_CORE)
165 printk(KERN_DEBUG "%s: %s/%x\n", __func__,
166 bp->name, bp->Bprotocols);
167 old = get_Bprotocol4mask(bp->Bprotocols);
168 if (old) {
169 printk(KERN_WARNING
170 "register duplicate protocol old %s/%x new %s/%x\n",
171 old->name, old->Bprotocols, bp->name, bp->Bprotocols);
172 return -EBUSY;
174 write_lock_irqsave(&bp_lock, flags);
175 list_add_tail(&bp->list, &Bprotocols);
176 write_unlock_irqrestore(&bp_lock, flags);
177 return 0;
179 EXPORT_SYMBOL(mISDN_register_Bprotocol);
181 void
182 mISDN_unregister_Bprotocol(struct Bprotocol *bp)
184 u_long flags;
186 if (debug & DEBUG_CORE)
187 printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
188 bp->Bprotocols);
189 write_lock_irqsave(&bp_lock, flags);
190 list_del(&bp->list);
191 write_unlock_irqrestore(&bp_lock, flags);
193 EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
196 mISDNInit(void)
198 int err;
200 printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
201 MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
202 mISDN_initstack(&debug);
203 err = mISDN_inittimer(&debug);
204 if (err)
205 goto error;
206 err = l1_init(&debug);
207 if (err) {
208 mISDN_timer_cleanup();
209 goto error;
211 err = Isdnl2_Init(&debug);
212 if (err) {
213 mISDN_timer_cleanup();
214 l1_cleanup();
215 goto error;
217 err = misdn_sock_init(&debug);
218 if (err) {
219 mISDN_timer_cleanup();
220 l1_cleanup();
221 Isdnl2_cleanup();
223 error:
224 return err;
227 void mISDN_cleanup(void)
229 misdn_sock_cleanup();
230 mISDN_timer_cleanup();
231 l1_cleanup();
232 Isdnl2_cleanup();
234 if (!list_empty(&devices))
235 printk(KERN_ERR "%s devices still registered\n", __func__);
237 if (!list_empty(&Bprotocols))
238 printk(KERN_ERR "%s Bprotocols still registered\n", __func__);
239 printk(KERN_DEBUG "mISDNcore unloaded\n");
242 module_init(mISDNInit);
243 module_exit(mISDN_cleanup);