Commit | Line | Data |
---|---|---|
1b2b03f8 KK |
1 | /* |
2 | * Copyright 2008 by Karsten Keil <kkeil@novell.com> | |
3 | * | |
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. | |
7 | * | |
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. | |
12 | * | |
13 | */ | |
14 | ||
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" | |
21 | ||
22 | static u_int debug; | |
23 | ||
24 | MODULE_AUTHOR("Karsten Keil"); | |
25 | MODULE_LICENSE("GPL"); | |
26 | module_param(debug, uint, S_IRUGO | S_IWUSR); | |
27 | ||
28 | static LIST_HEAD(devices); | |
5b834354 | 29 | static DEFINE_RWLOCK(device_lock); |
1b2b03f8 KK |
30 | static u64 device_ids; |
31 | #define MAX_DEVICE_ID 63 | |
32 | ||
33 | static LIST_HEAD(Bprotocols); | |
5b834354 | 34 | static DEFINE_RWLOCK(bp_lock); |
1b2b03f8 KK |
35 | |
36 | struct mISDNdevice | |
37 | *get_mdevice(u_int id) | |
38 | { | |
39 | struct mISDNdevice *dev; | |
40 | ||
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; | |
46 | } | |
47 | read_unlock(&device_lock); | |
48 | return NULL; | |
49 | } | |
50 | ||
51 | int | |
52 | get_mdevice_count(void) | |
53 | { | |
54 | struct mISDNdevice *dev; | |
55 | int cnt = 0; | |
56 | ||
57 | read_lock(&device_lock); | |
58 | list_for_each_entry(dev, &devices, D.list) | |
59 | cnt++; | |
60 | read_unlock(&device_lock); | |
61 | return cnt; | |
62 | } | |
63 | ||
64 | static int | |
65 | get_free_devid(void) | |
66 | { | |
67 | u_int i; | |
68 | ||
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; | |
73 | } | |
74 | ||
75 | int | |
76 | mISDN_register_device(struct mISDNdevice *dev, char *name) | |
77 | { | |
78 | u_long flags; | |
79 | int err; | |
80 | ||
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; | |
98 | } | |
99 | EXPORT_SYMBOL(mISDN_register_device); | |
100 | ||
101 | void | |
102 | mISDN_unregister_device(struct mISDNdevice *dev) { | |
103 | u_long flags; | |
104 | ||
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); | |
113 | } | |
114 | EXPORT_SYMBOL(mISDN_unregister_device); | |
115 | ||
116 | u_int | |
117 | get_all_Bprotocols(void) | |
118 | { | |
119 | struct Bprotocol *bp; | |
120 | u_int m = 0; | |
121 | ||
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; | |
127 | } | |
128 | ||
129 | struct Bprotocol * | |
130 | get_Bprotocol4mask(u_int m) | |
131 | { | |
132 | struct Bprotocol *bp; | |
133 | ||
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; | |
139 | } | |
140 | read_unlock(&bp_lock); | |
141 | return NULL; | |
142 | } | |
143 | ||
144 | struct Bprotocol * | |
145 | get_Bprotocol4id(u_int id) | |
146 | { | |
147 | u_int m; | |
148 | ||
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; | |
153 | } | |
154 | m = 1 << (id & ISDN_P_B_MASK); | |
155 | return get_Bprotocol4mask(m); | |
156 | } | |
157 | ||
158 | int | |
159 | mISDN_register_Bprotocol(struct Bprotocol *bp) | |
160 | { | |
161 | u_long flags; | |
162 | struct Bprotocol *old; | |
163 | ||
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; | |
173 | } | |
174 | write_lock_irqsave(&bp_lock, flags); | |
175 | list_add_tail(&bp->list, &Bprotocols); | |
176 | write_unlock_irqrestore(&bp_lock, flags); | |
177 | return 0; | |
178 | } | |
179 | EXPORT_SYMBOL(mISDN_register_Bprotocol); | |
180 | ||
181 | void | |
182 | mISDN_unregister_Bprotocol(struct Bprotocol *bp) | |
183 | { | |
184 | u_long flags; | |
185 | ||
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); | |
192 | } | |
193 | EXPORT_SYMBOL(mISDN_unregister_Bprotocol); | |
194 | ||
5b834354 | 195 | static int |
1b2b03f8 KK |
196 | mISDNInit(void) |
197 | { | |
198 | int err; | |
199 | ||
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; | |
210 | } | |
211 | err = Isdnl2_Init(&debug); | |
212 | if (err) { | |
213 | mISDN_timer_cleanup(); | |
214 | l1_cleanup(); | |
215 | goto error; | |
216 | } | |
217 | err = misdn_sock_init(&debug); | |
218 | if (err) { | |
219 | mISDN_timer_cleanup(); | |
220 | l1_cleanup(); | |
221 | Isdnl2_cleanup(); | |
222 | } | |
223 | error: | |
224 | return err; | |
225 | } | |
226 | ||
5b834354 | 227 | static void mISDN_cleanup(void) |
1b2b03f8 KK |
228 | { |
229 | misdn_sock_cleanup(); | |
230 | mISDN_timer_cleanup(); | |
231 | l1_cleanup(); | |
232 | Isdnl2_cleanup(); | |
233 | ||
234 | if (!list_empty(&devices)) | |
235 | printk(KERN_ERR "%s devices still registered\n", __func__); | |
236 | ||
237 | if (!list_empty(&Bprotocols)) | |
238 | printk(KERN_ERR "%s Bprotocols still registered\n", __func__); | |
239 | printk(KERN_DEBUG "mISDNcore unloaded\n"); | |
240 | } | |
241 | ||
242 | module_init(mISDNInit); | |
243 | module_exit(mISDN_cleanup); | |
244 |