GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / isdn / mISDN / core.c
blob39742bfe2dae12c0c4bed3b737e33cc65d2afea7
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/slab.h>
16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/module.h>
19 #include <linux/spinlock.h>
20 #include <linux/mISDNif.h>
21 #include "core.h"
23 static u_int debug;
25 MODULE_AUTHOR("Karsten Keil");
26 MODULE_LICENSE("GPL");
27 module_param(debug, uint, S_IRUGO | S_IWUSR);
29 static u64 device_ids;
30 #define MAX_DEVICE_ID 63
32 static LIST_HEAD(Bprotocols);
33 static DEFINE_RWLOCK(bp_lock);
35 static void mISDN_dev_release(struct device *dev)
37 /* nothing to do: the device is part of its parent's data structure */
40 static ssize_t _show_id(struct device *dev,
41 struct device_attribute *attr, char *buf)
43 struct mISDNdevice *mdev = dev_to_mISDN(dev);
45 if (!mdev)
46 return -ENODEV;
47 return sprintf(buf, "%d\n", mdev->id);
50 static ssize_t _show_nrbchan(struct device *dev,
51 struct device_attribute *attr, char *buf)
53 struct mISDNdevice *mdev = dev_to_mISDN(dev);
55 if (!mdev)
56 return -ENODEV;
57 return sprintf(buf, "%d\n", mdev->nrbchan);
60 static ssize_t _show_d_protocols(struct device *dev,
61 struct device_attribute *attr, char *buf)
63 struct mISDNdevice *mdev = dev_to_mISDN(dev);
65 if (!mdev)
66 return -ENODEV;
67 return sprintf(buf, "%d\n", mdev->Dprotocols);
70 static ssize_t _show_b_protocols(struct device *dev,
71 struct device_attribute *attr, char *buf)
73 struct mISDNdevice *mdev = dev_to_mISDN(dev);
75 if (!mdev)
76 return -ENODEV;
77 return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
80 static ssize_t _show_protocol(struct device *dev,
81 struct device_attribute *attr, char *buf)
83 struct mISDNdevice *mdev = dev_to_mISDN(dev);
85 if (!mdev)
86 return -ENODEV;
87 return sprintf(buf, "%d\n", mdev->D.protocol);
90 static ssize_t _show_name(struct device *dev,
91 struct device_attribute *attr, char *buf)
93 strcpy(buf, dev_name(dev));
94 return strlen(buf);
98 static ssize_t _show_channelmap(struct device *dev,
99 struct device_attribute *attr, char *buf)
101 struct mISDNdevice *mdev = dev_to_mISDN(dev);
102 char *bp = buf;
103 int i;
105 for (i = 0; i <= mdev->nrbchan; i++)
106 *bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';
108 return bp - buf;
111 static struct device_attribute mISDN_dev_attrs[] = {
112 __ATTR(id, S_IRUGO, _show_id, NULL),
113 __ATTR(d_protocols, S_IRUGO, _show_d_protocols, NULL),
114 __ATTR(b_protocols, S_IRUGO, _show_b_protocols, NULL),
115 __ATTR(protocol, S_IRUGO, _show_protocol, NULL),
116 __ATTR(channelmap, S_IRUGO, _show_channelmap, NULL),
117 __ATTR(nrbchan, S_IRUGO, _show_nrbchan, NULL),
118 __ATTR(name, S_IRUGO, _show_name, NULL),
119 /* __ATTR(name, S_IRUGO|S_IWUSR, _show_name, _set_name), */
123 #ifdef CONFIG_HOTPLUG
124 static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
126 struct mISDNdevice *mdev = dev_to_mISDN(dev);
128 if (!mdev)
129 return 0;
131 if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
132 return -ENOMEM;
134 return 0;
136 #endif
138 static void mISDN_class_release(struct class *cls)
140 /* do nothing, it's static */
143 static struct class mISDN_class = {
144 .name = "mISDN",
145 .owner = THIS_MODULE,
146 #ifdef CONFIG_HOTPLUG
147 .dev_uevent = mISDN_uevent,
148 #endif
149 .dev_attrs = mISDN_dev_attrs,
150 .dev_release = mISDN_dev_release,
151 .class_release = mISDN_class_release,
154 static int
155 _get_mdevice(struct device *dev, void *id)
157 struct mISDNdevice *mdev = dev_to_mISDN(dev);
159 if (!mdev)
160 return 0;
161 if (mdev->id != *(u_int *)id)
162 return 0;
163 return 1;
166 struct mISDNdevice
167 *get_mdevice(u_int id)
169 return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
170 _get_mdevice));
173 static int
174 _get_mdevice_count(struct device *dev, void *cnt)
176 *(int *)cnt += 1;
177 return 0;
181 get_mdevice_count(void)
183 int cnt = 0;
185 class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
186 return cnt;
189 static int
190 get_free_devid(void)
192 u_int i;
194 for (i = 0; i <= MAX_DEVICE_ID; i++)
195 if (!test_and_set_bit(i, (u_long *)&device_ids))
196 break;
197 if (i > MAX_DEVICE_ID)
198 return -EBUSY;
199 return i;
203 mISDN_register_device(struct mISDNdevice *dev,
204 struct device *parent, char *name)
206 int err;
208 err = get_free_devid();
209 if (err < 0)
210 goto error1;
211 dev->id = err;
213 device_initialize(&dev->dev);
214 if (name && name[0])
215 dev_set_name(&dev->dev, "%s", name);
216 else
217 dev_set_name(&dev->dev, "mISDN%d", dev->id);
218 if (debug & DEBUG_CORE)
219 printk(KERN_DEBUG "mISDN_register %s %d\n",
220 dev_name(&dev->dev), dev->id);
221 err = create_stack(dev);
222 if (err)
223 goto error1;
225 dev->dev.class = &mISDN_class;
226 dev->dev.platform_data = dev;
227 dev->dev.parent = parent;
228 dev_set_drvdata(&dev->dev, dev);
230 err = device_add(&dev->dev);
231 if (err)
232 goto error3;
233 return 0;
235 error3:
236 delete_stack(dev);
237 return err;
238 error1:
239 return err;
242 EXPORT_SYMBOL(mISDN_register_device);
244 void
245 mISDN_unregister_device(struct mISDNdevice *dev) {
246 if (debug & DEBUG_CORE)
247 printk(KERN_DEBUG "mISDN_unregister %s %d\n",
248 dev_name(&dev->dev), dev->id);
249 /* sysfs_remove_link(&dev->dev.kobj, "device"); */
250 device_del(&dev->dev);
251 dev_set_drvdata(&dev->dev, NULL);
253 test_and_clear_bit(dev->id, (u_long *)&device_ids);
254 delete_stack(dev);
255 put_device(&dev->dev);
257 EXPORT_SYMBOL(mISDN_unregister_device);
259 u_int
260 get_all_Bprotocols(void)
262 struct Bprotocol *bp;
263 u_int m = 0;
265 read_lock(&bp_lock);
266 list_for_each_entry(bp, &Bprotocols, list)
267 m |= bp->Bprotocols;
268 read_unlock(&bp_lock);
269 return m;
272 struct Bprotocol *
273 get_Bprotocol4mask(u_int m)
275 struct Bprotocol *bp;
277 read_lock(&bp_lock);
278 list_for_each_entry(bp, &Bprotocols, list)
279 if (bp->Bprotocols & m) {
280 read_unlock(&bp_lock);
281 return bp;
283 read_unlock(&bp_lock);
284 return NULL;
287 struct Bprotocol *
288 get_Bprotocol4id(u_int id)
290 u_int m;
292 if (id < ISDN_P_B_START || id > 63) {
293 printk(KERN_WARNING "%s id not in range %d\n",
294 __func__, id);
295 return NULL;
297 m = 1 << (id & ISDN_P_B_MASK);
298 return get_Bprotocol4mask(m);
302 mISDN_register_Bprotocol(struct Bprotocol *bp)
304 u_long flags;
305 struct Bprotocol *old;
307 if (debug & DEBUG_CORE)
308 printk(KERN_DEBUG "%s: %s/%x\n", __func__,
309 bp->name, bp->Bprotocols);
310 old = get_Bprotocol4mask(bp->Bprotocols);
311 if (old) {
312 printk(KERN_WARNING
313 "register duplicate protocol old %s/%x new %s/%x\n",
314 old->name, old->Bprotocols, bp->name, bp->Bprotocols);
315 return -EBUSY;
317 write_lock_irqsave(&bp_lock, flags);
318 list_add_tail(&bp->list, &Bprotocols);
319 write_unlock_irqrestore(&bp_lock, flags);
320 return 0;
322 EXPORT_SYMBOL(mISDN_register_Bprotocol);
324 void
325 mISDN_unregister_Bprotocol(struct Bprotocol *bp)
327 u_long flags;
329 if (debug & DEBUG_CORE)
330 printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
331 bp->Bprotocols);
332 write_lock_irqsave(&bp_lock, flags);
333 list_del(&bp->list);
334 write_unlock_irqrestore(&bp_lock, flags);
336 EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
338 static int
339 mISDNInit(void)
341 int err;
343 printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
344 MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
345 mISDN_init_clock(&debug);
346 mISDN_initstack(&debug);
347 err = class_register(&mISDN_class);
348 if (err)
349 goto error1;
350 err = mISDN_inittimer(&debug);
351 if (err)
352 goto error2;
353 err = l1_init(&debug);
354 if (err)
355 goto error3;
356 err = Isdnl2_Init(&debug);
357 if (err)
358 goto error4;
359 err = misdn_sock_init(&debug);
360 if (err)
361 goto error5;
362 return 0;
364 error5:
365 Isdnl2_cleanup();
366 error4:
367 l1_cleanup();
368 error3:
369 mISDN_timer_cleanup();
370 error2:
371 class_unregister(&mISDN_class);
372 error1:
373 return err;
376 static void mISDN_cleanup(void)
378 misdn_sock_cleanup();
379 Isdnl2_cleanup();
380 l1_cleanup();
381 mISDN_timer_cleanup();
382 class_unregister(&mISDN_class);
384 printk(KERN_DEBUG "mISDNcore unloaded\n");
387 module_init(mISDNInit);
388 module_exit(mISDN_cleanup);