1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
6 #include <linux/slab.h>
7 #include <linux/types.h>
8 #include <linux/stddef.h>
9 #include <linux/module.h>
10 #include <linux/spinlock.h>
11 #include <linux/mISDNif.h>
16 MODULE_AUTHOR("Karsten Keil");
17 MODULE_LICENSE("GPL");
18 module_param(debug, uint, S_IRUGO | S_IWUSR);
20 static u64 device_ids;
21 #define MAX_DEVICE_ID 63
23 static LIST_HEAD(Bprotocols);
24 static DEFINE_RWLOCK(bp_lock);
26 static void mISDN_dev_release(struct device *dev)
28 /* nothing to do: the device is part of its parent's data structure */
31 static ssize_t id_show(struct device *dev,
32 struct device_attribute *attr, char *buf)
34 struct mISDNdevice *mdev = dev_to_mISDN(dev);
38 return sprintf(buf, "%d\n", mdev->id);
40 static DEVICE_ATTR_RO(id);
42 static ssize_t nrbchan_show(struct device *dev,
43 struct device_attribute *attr, char *buf)
45 struct mISDNdevice *mdev = dev_to_mISDN(dev);
49 return sprintf(buf, "%d\n", mdev->nrbchan);
51 static DEVICE_ATTR_RO(nrbchan);
53 static ssize_t d_protocols_show(struct device *dev,
54 struct device_attribute *attr, char *buf)
56 struct mISDNdevice *mdev = dev_to_mISDN(dev);
60 return sprintf(buf, "%d\n", mdev->Dprotocols);
62 static DEVICE_ATTR_RO(d_protocols);
64 static ssize_t b_protocols_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
67 struct mISDNdevice *mdev = dev_to_mISDN(dev);
71 return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
73 static DEVICE_ATTR_RO(b_protocols);
75 static ssize_t protocol_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
78 struct mISDNdevice *mdev = dev_to_mISDN(dev);
82 return sprintf(buf, "%d\n", mdev->D.protocol);
84 static DEVICE_ATTR_RO(protocol);
86 static ssize_t name_show(struct device *dev,
87 struct device_attribute *attr, char *buf)
89 strcpy(buf, dev_name(dev));
92 static DEVICE_ATTR_RO(name);
95 static ssize_t name_set(struct device *dev, struct device_attribute *attr,
96 const char *buf, size_t count)
99 char *out = kmalloc(count + 1, GFP_KERNEL);
104 memcpy(out, buf, count);
105 if (count && out[count - 1] == '\n')
108 err = device_rename(dev, out);
111 return (err < 0) ? err : count;
113 static DEVICE_ATTR_RW(name);
116 static ssize_t channelmap_show(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 struct mISDNdevice *mdev = dev_to_mISDN(dev);
123 for (i = 0; i <= mdev->nrbchan; i++)
124 *bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';
128 static DEVICE_ATTR_RO(channelmap);
130 static struct attribute *mISDN_attrs[] = {
132 &dev_attr_d_protocols.attr,
133 &dev_attr_b_protocols.attr,
134 &dev_attr_protocol.attr,
135 &dev_attr_channelmap.attr,
136 &dev_attr_nrbchan.attr,
140 ATTRIBUTE_GROUPS(mISDN);
142 static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
144 struct mISDNdevice *mdev = dev_to_mISDN(dev);
149 if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
155 static void mISDN_class_release(struct class *cls)
157 /* do nothing, it's static */
160 static struct class mISDN_class = {
162 .owner = THIS_MODULE,
163 .dev_uevent = mISDN_uevent,
164 .dev_groups = mISDN_groups,
165 .dev_release = mISDN_dev_release,
166 .class_release = mISDN_class_release,
170 _get_mdevice(struct device *dev, const void *id)
172 struct mISDNdevice *mdev = dev_to_mISDN(dev);
176 if (mdev->id != *(const u_int *)id)
182 *get_mdevice(u_int id)
184 return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
189 _get_mdevice_count(struct device *dev, void *cnt)
196 get_mdevice_count(void)
200 class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
209 for (i = 0; i <= MAX_DEVICE_ID; i++)
210 if (!test_and_set_bit(i, (u_long *)&device_ids))
212 if (i > MAX_DEVICE_ID)
218 mISDN_register_device(struct mISDNdevice *dev,
219 struct device *parent, char *name)
223 err = get_free_devid();
228 device_initialize(&dev->dev);
230 dev_set_name(&dev->dev, "%s", name);
232 dev_set_name(&dev->dev, "mISDN%d", dev->id);
233 if (debug & DEBUG_CORE)
234 printk(KERN_DEBUG "mISDN_register %s %d\n",
235 dev_name(&dev->dev), dev->id);
236 err = create_stack(dev);
240 dev->dev.class = &mISDN_class;
241 dev->dev.platform_data = dev;
242 dev->dev.parent = parent;
243 dev_set_drvdata(&dev->dev, dev);
245 err = device_add(&dev->dev);
257 EXPORT_SYMBOL(mISDN_register_device);
260 mISDN_unregister_device(struct mISDNdevice *dev) {
261 if (debug & DEBUG_CORE)
262 printk(KERN_DEBUG "mISDN_unregister %s %d\n",
263 dev_name(&dev->dev), dev->id);
264 /* sysfs_remove_link(&dev->dev.kobj, "device"); */
265 device_del(&dev->dev);
266 dev_set_drvdata(&dev->dev, NULL);
268 test_and_clear_bit(dev->id, (u_long *)&device_ids);
270 put_device(&dev->dev);
272 EXPORT_SYMBOL(mISDN_unregister_device);
275 get_all_Bprotocols(void)
277 struct Bprotocol *bp;
281 list_for_each_entry(bp, &Bprotocols, list)
283 read_unlock(&bp_lock);
288 get_Bprotocol4mask(u_int m)
290 struct Bprotocol *bp;
293 list_for_each_entry(bp, &Bprotocols, list)
294 if (bp->Bprotocols & m) {
295 read_unlock(&bp_lock);
298 read_unlock(&bp_lock);
303 get_Bprotocol4id(u_int id)
307 if (id < ISDN_P_B_START || id > 63) {
308 printk(KERN_WARNING "%s id not in range %d\n",
312 m = 1 << (id & ISDN_P_B_MASK);
313 return get_Bprotocol4mask(m);
317 mISDN_register_Bprotocol(struct Bprotocol *bp)
320 struct Bprotocol *old;
322 if (debug & DEBUG_CORE)
323 printk(KERN_DEBUG "%s: %s/%x\n", __func__,
324 bp->name, bp->Bprotocols);
325 old = get_Bprotocol4mask(bp->Bprotocols);
328 "register duplicate protocol old %s/%x new %s/%x\n",
329 old->name, old->Bprotocols, bp->name, bp->Bprotocols);
332 write_lock_irqsave(&bp_lock, flags);
333 list_add_tail(&bp->list, &Bprotocols);
334 write_unlock_irqrestore(&bp_lock, flags);
337 EXPORT_SYMBOL(mISDN_register_Bprotocol);
340 mISDN_unregister_Bprotocol(struct Bprotocol *bp)
344 if (debug & DEBUG_CORE)
345 printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
347 write_lock_irqsave(&bp_lock, flags);
349 write_unlock_irqrestore(&bp_lock, flags);
351 EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
353 static const char *msg_no_channel = "<no channel>";
354 static const char *msg_no_stack = "<no stack>";
355 static const char *msg_no_stackdev = "<no stack device>";
357 const char *mISDNDevName4ch(struct mISDNchannel *ch)
360 return msg_no_channel;
364 return msg_no_stackdev;
365 return dev_name(&ch->st->dev->dev);
367 EXPORT_SYMBOL(mISDNDevName4ch);
374 printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
375 MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
376 mISDN_init_clock(&debug);
377 mISDN_initstack(&debug);
378 err = class_register(&mISDN_class);
381 err = mISDN_inittimer(&debug);
384 err = Isdnl1_Init(&debug);
387 err = Isdnl2_Init(&debug);
390 err = misdn_sock_init(&debug);
400 mISDN_timer_cleanup();
402 class_unregister(&mISDN_class);
407 static void mISDN_cleanup(void)
409 misdn_sock_cleanup();
412 mISDN_timer_cleanup();
413 class_unregister(&mISDN_class);
415 printk(KERN_DEBUG "mISDNcore unloaded\n");
418 module_init(mISDNInit);
419 module_exit(mISDN_cleanup);