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>
24 MODULE_AUTHOR("Karsten Keil");
25 MODULE_LICENSE("GPL");
26 module_param(debug, uint, S_IRUGO | S_IWUSR);
28 static u64 device_ids;
29 #define MAX_DEVICE_ID 63
31 static LIST_HEAD(Bprotocols);
32 static DEFINE_RWLOCK(bp_lock);
34 static void mISDN_dev_release(struct device *dev)
36 /* nothing to do: the device is part of its parent's data structure */
39 static ssize_t _show_id(struct device *dev,
40 struct device_attribute *attr, char *buf)
42 struct mISDNdevice *mdev = dev_to_mISDN(dev);
46 return sprintf(buf, "%d\n", mdev->id);
49 static ssize_t _show_nrbchan(struct device *dev,
50 struct device_attribute *attr, char *buf)
52 struct mISDNdevice *mdev = dev_to_mISDN(dev);
56 return sprintf(buf, "%d\n", mdev->nrbchan);
59 static ssize_t _show_d_protocols(struct device *dev,
60 struct device_attribute *attr, char *buf)
62 struct mISDNdevice *mdev = dev_to_mISDN(dev);
66 return sprintf(buf, "%d\n", mdev->Dprotocols);
69 static ssize_t _show_b_protocols(struct device *dev,
70 struct device_attribute *attr, char *buf)
72 struct mISDNdevice *mdev = dev_to_mISDN(dev);
76 return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
79 static ssize_t _show_protocol(struct device *dev,
80 struct device_attribute *attr, char *buf)
82 struct mISDNdevice *mdev = dev_to_mISDN(dev);
86 return sprintf(buf, "%d\n", mdev->D.protocol);
89 static ssize_t _show_name(struct device *dev,
90 struct device_attribute *attr, char *buf)
92 strcpy(buf, dev_name(dev));
97 static ssize_t _set_name(struct device *dev, struct device_attribute *attr,
98 const char *buf, size_t count)
101 char *out = kmalloc(count + 1, GFP_KERNEL);
106 memcpy(out, buf, count);
107 if (count && out[count - 1] == '\n')
110 err = device_rename(dev, out);
113 return (err < 0) ? err : count;
117 static ssize_t _show_channelmap(struct device *dev,
118 struct device_attribute *attr, char *buf)
120 struct mISDNdevice *mdev = dev_to_mISDN(dev);
124 for (i = 0; i <= mdev->nrbchan; i++)
125 *bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';
130 static struct device_attribute mISDN_dev_attrs[] = {
131 __ATTR(id, S_IRUGO, _show_id, NULL),
132 __ATTR(d_protocols, S_IRUGO, _show_d_protocols, NULL),
133 __ATTR(b_protocols, S_IRUGO, _show_b_protocols, NULL),
134 __ATTR(protocol, S_IRUGO, _show_protocol, NULL),
135 __ATTR(channelmap, S_IRUGO, _show_channelmap, NULL),
136 __ATTR(nrbchan, S_IRUGO, _show_nrbchan, NULL),
137 __ATTR(name, S_IRUGO, _show_name, NULL),
138 /* __ATTR(name, S_IRUGO|S_IWUSR, _show_name, _set_name), */
142 #ifdef CONFIG_HOTPLUG
143 static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
145 struct mISDNdevice *mdev = dev_to_mISDN(dev);
150 if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
157 static void mISDN_class_release(struct class *cls)
159 /* do nothing, it's static */
162 static struct class mISDN_class = {
164 .owner = THIS_MODULE,
165 #ifdef CONFIG_HOTPLUG
166 .dev_uevent = mISDN_uevent,
168 .dev_attrs = mISDN_dev_attrs,
169 .dev_release = mISDN_dev_release,
170 .class_release = mISDN_class_release,
174 _get_mdevice(struct device *dev, void *id)
176 struct mISDNdevice *mdev = dev_to_mISDN(dev);
180 if (mdev->id != *(u_int *)id)
186 *get_mdevice(u_int id)
188 return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
193 _get_mdevice_count(struct device *dev, void *cnt)
200 get_mdevice_count(void)
204 class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
213 for (i = 0; i <= MAX_DEVICE_ID; i++)
214 if (!test_and_set_bit(i, (u_long *)&device_ids))
216 if (i > MAX_DEVICE_ID)
222 mISDN_register_device(struct mISDNdevice *dev,
223 struct device *parent, char *name)
227 err = get_free_devid();
232 device_initialize(&dev->dev);
234 dev_set_name(&dev->dev, "%s", name);
236 dev_set_name(&dev->dev, "mISDN%d", dev->id);
237 if (debug & DEBUG_CORE)
238 printk(KERN_DEBUG "mISDN_register %s %d\n",
239 dev_name(&dev->dev), dev->id);
240 err = create_stack(dev);
244 dev->dev.class = &mISDN_class;
245 dev->dev.platform_data = dev;
246 dev->dev.parent = parent;
247 dev_set_drvdata(&dev->dev, dev);
249 err = device_add(&dev->dev);
261 EXPORT_SYMBOL(mISDN_register_device);
264 mISDN_unregister_device(struct mISDNdevice *dev) {
265 if (debug & DEBUG_CORE)
266 printk(KERN_DEBUG "mISDN_unregister %s %d\n",
267 dev_name(&dev->dev), dev->id);
268 /* sysfs_remove_link(&dev->dev.kobj, "device"); */
269 device_del(&dev->dev);
270 dev_set_drvdata(&dev->dev, NULL);
272 test_and_clear_bit(dev->id, (u_long *)&device_ids);
274 put_device(&dev->dev);
276 EXPORT_SYMBOL(mISDN_unregister_device);
279 get_all_Bprotocols(void)
281 struct Bprotocol *bp;
285 list_for_each_entry(bp, &Bprotocols, list)
287 read_unlock(&bp_lock);
292 get_Bprotocol4mask(u_int m)
294 struct Bprotocol *bp;
297 list_for_each_entry(bp, &Bprotocols, list)
298 if (bp->Bprotocols & m) {
299 read_unlock(&bp_lock);
302 read_unlock(&bp_lock);
307 get_Bprotocol4id(u_int id)
311 if (id < ISDN_P_B_START || id > 63) {
312 printk(KERN_WARNING "%s id not in range %d\n",
316 m = 1 << (id & ISDN_P_B_MASK);
317 return get_Bprotocol4mask(m);
321 mISDN_register_Bprotocol(struct Bprotocol *bp)
324 struct Bprotocol *old;
326 if (debug & DEBUG_CORE)
327 printk(KERN_DEBUG "%s: %s/%x\n", __func__,
328 bp->name, bp->Bprotocols);
329 old = get_Bprotocol4mask(bp->Bprotocols);
332 "register duplicate protocol old %s/%x new %s/%x\n",
333 old->name, old->Bprotocols, bp->name, bp->Bprotocols);
336 write_lock_irqsave(&bp_lock, flags);
337 list_add_tail(&bp->list, &Bprotocols);
338 write_unlock_irqrestore(&bp_lock, flags);
341 EXPORT_SYMBOL(mISDN_register_Bprotocol);
344 mISDN_unregister_Bprotocol(struct Bprotocol *bp)
348 if (debug & DEBUG_CORE)
349 printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
351 write_lock_irqsave(&bp_lock, flags);
353 write_unlock_irqrestore(&bp_lock, flags);
355 EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
362 printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
363 MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
364 mISDN_init_clock(&debug);
365 mISDN_initstack(&debug);
366 err = class_register(&mISDN_class);
369 err = mISDN_inittimer(&debug);
372 err = l1_init(&debug);
375 err = Isdnl2_Init(&debug);
378 err = misdn_sock_init(&debug);
388 mISDN_timer_cleanup();
390 class_unregister(&mISDN_class);
395 static void mISDN_cleanup(void)
397 misdn_sock_cleanup();
400 mISDN_timer_cleanup();
401 class_unregister(&mISDN_class);
403 printk(KERN_DEBUG "mISDNcore unloaded\n");
406 module_init(mISDNInit);
407 module_exit(mISDN_cleanup);