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_DESCRIPTION("Modular ISDN core driver");
18 MODULE_LICENSE("GPL");
19 module_param(debug
, uint
, S_IRUGO
| S_IWUSR
);
21 static u64 device_ids
;
22 #define MAX_DEVICE_ID 63
24 static LIST_HEAD(Bprotocols
);
25 static DEFINE_RWLOCK(bp_lock
);
27 static void mISDN_dev_release(struct device
*dev
)
29 /* nothing to do: the device is part of its parent's data structure */
32 static ssize_t
id_show(struct device
*dev
,
33 struct device_attribute
*attr
, char *buf
)
35 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
39 return sprintf(buf
, "%d\n", mdev
->id
);
41 static DEVICE_ATTR_RO(id
);
43 static ssize_t
nrbchan_show(struct device
*dev
,
44 struct device_attribute
*attr
, char *buf
)
46 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
50 return sprintf(buf
, "%d\n", mdev
->nrbchan
);
52 static DEVICE_ATTR_RO(nrbchan
);
54 static ssize_t
d_protocols_show(struct device
*dev
,
55 struct device_attribute
*attr
, char *buf
)
57 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
61 return sprintf(buf
, "%d\n", mdev
->Dprotocols
);
63 static DEVICE_ATTR_RO(d_protocols
);
65 static ssize_t
b_protocols_show(struct device
*dev
,
66 struct device_attribute
*attr
, char *buf
)
68 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
72 return sprintf(buf
, "%d\n", mdev
->Bprotocols
| get_all_Bprotocols());
74 static DEVICE_ATTR_RO(b_protocols
);
76 static ssize_t
protocol_show(struct device
*dev
,
77 struct device_attribute
*attr
, char *buf
)
79 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
83 return sprintf(buf
, "%d\n", mdev
->D
.protocol
);
85 static DEVICE_ATTR_RO(protocol
);
87 static ssize_t
name_show(struct device
*dev
,
88 struct device_attribute
*attr
, char *buf
)
90 strcpy(buf
, dev_name(dev
));
93 static DEVICE_ATTR_RO(name
);
96 static ssize_t
name_set(struct device
*dev
, struct device_attribute
*attr
,
97 const char *buf
, size_t count
)
100 char *out
= kmalloc(count
+ 1, GFP_KERNEL
);
105 memcpy(out
, buf
, count
);
106 if (count
&& out
[count
- 1] == '\n')
109 err
= device_rename(dev
, out
);
112 return (err
< 0) ? err
: count
;
114 static DEVICE_ATTR_RW(name
);
117 static ssize_t
channelmap_show(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';
129 static DEVICE_ATTR_RO(channelmap
);
131 static struct attribute
*mISDN_attrs
[] = {
133 &dev_attr_d_protocols
.attr
,
134 &dev_attr_b_protocols
.attr
,
135 &dev_attr_protocol
.attr
,
136 &dev_attr_channelmap
.attr
,
137 &dev_attr_nrbchan
.attr
,
141 ATTRIBUTE_GROUPS(mISDN
);
143 static int mISDN_uevent(const struct device
*dev
, struct kobj_uevent_env
*env
)
145 const struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
150 if (add_uevent_var(env
, "nchans=%d", mdev
->nrbchan
))
156 static struct class mISDN_class
= {
158 .dev_uevent
= mISDN_uevent
,
159 .dev_groups
= mISDN_groups
,
160 .dev_release
= mISDN_dev_release
,
164 _get_mdevice(struct device
*dev
, const void *id
)
166 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
170 if (mdev
->id
!= *(const u_int
*)id
)
176 *get_mdevice(u_int id
)
178 return dev_to_mISDN(class_find_device(&mISDN_class
, NULL
, &id
,
183 _get_mdevice_count(struct device
*dev
, void *cnt
)
190 get_mdevice_count(void)
194 class_for_each_device(&mISDN_class
, NULL
, &cnt
, _get_mdevice_count
);
203 for (i
= 0; i
<= MAX_DEVICE_ID
; i
++)
204 if (!test_and_set_bit(i
, (u_long
*)&device_ids
))
206 if (i
> MAX_DEVICE_ID
)
212 mISDN_register_device(struct mISDNdevice
*dev
,
213 struct device
*parent
, char *name
)
217 err
= get_free_devid();
222 device_initialize(&dev
->dev
);
224 dev_set_name(&dev
->dev
, "%s", name
);
226 dev_set_name(&dev
->dev
, "mISDN%d", dev
->id
);
227 if (debug
& DEBUG_CORE
)
228 printk(KERN_DEBUG
"mISDN_register %s %d\n",
229 dev_name(&dev
->dev
), dev
->id
);
230 dev
->dev
.class = &mISDN_class
;
232 err
= create_stack(dev
);
236 dev
->dev
.platform_data
= dev
;
237 dev
->dev
.parent
= parent
;
238 dev_set_drvdata(&dev
->dev
, dev
);
240 err
= device_add(&dev
->dev
);
248 put_device(&dev
->dev
);
252 EXPORT_SYMBOL(mISDN_register_device
);
255 mISDN_unregister_device(struct mISDNdevice
*dev
) {
256 if (debug
& DEBUG_CORE
)
257 printk(KERN_DEBUG
"mISDN_unregister %s %d\n",
258 dev_name(&dev
->dev
), dev
->id
);
259 /* sysfs_remove_link(&dev->dev.kobj, "device"); */
260 device_del(&dev
->dev
);
261 dev_set_drvdata(&dev
->dev
, NULL
);
263 test_and_clear_bit(dev
->id
, (u_long
*)&device_ids
);
265 put_device(&dev
->dev
);
267 EXPORT_SYMBOL(mISDN_unregister_device
);
270 get_all_Bprotocols(void)
272 struct Bprotocol
*bp
;
276 list_for_each_entry(bp
, &Bprotocols
, list
)
278 read_unlock(&bp_lock
);
283 get_Bprotocol4mask(u_int m
)
285 struct Bprotocol
*bp
;
288 list_for_each_entry(bp
, &Bprotocols
, list
)
289 if (bp
->Bprotocols
& m
) {
290 read_unlock(&bp_lock
);
293 read_unlock(&bp_lock
);
298 get_Bprotocol4id(u_int id
)
302 if (id
< ISDN_P_B_START
|| id
> 63) {
303 printk(KERN_WARNING
"%s id not in range %d\n",
307 m
= 1 << (id
& ISDN_P_B_MASK
);
308 return get_Bprotocol4mask(m
);
312 mISDN_register_Bprotocol(struct Bprotocol
*bp
)
315 struct Bprotocol
*old
;
317 if (debug
& DEBUG_CORE
)
318 printk(KERN_DEBUG
"%s: %s/%x\n", __func__
,
319 bp
->name
, bp
->Bprotocols
);
320 old
= get_Bprotocol4mask(bp
->Bprotocols
);
323 "register duplicate protocol old %s/%x new %s/%x\n",
324 old
->name
, old
->Bprotocols
, bp
->name
, bp
->Bprotocols
);
327 write_lock_irqsave(&bp_lock
, flags
);
328 list_add_tail(&bp
->list
, &Bprotocols
);
329 write_unlock_irqrestore(&bp_lock
, flags
);
332 EXPORT_SYMBOL(mISDN_register_Bprotocol
);
335 mISDN_unregister_Bprotocol(struct Bprotocol
*bp
)
339 if (debug
& DEBUG_CORE
)
340 printk(KERN_DEBUG
"%s: %s/%x\n", __func__
, bp
->name
,
342 write_lock_irqsave(&bp_lock
, flags
);
344 write_unlock_irqrestore(&bp_lock
, flags
);
346 EXPORT_SYMBOL(mISDN_unregister_Bprotocol
);
348 static const char *msg_no_channel
= "<no channel>";
349 static const char *msg_no_stack
= "<no stack>";
350 static const char *msg_no_stackdev
= "<no stack device>";
352 const char *mISDNDevName4ch(struct mISDNchannel
*ch
)
355 return msg_no_channel
;
359 return msg_no_stackdev
;
360 return dev_name(&ch
->st
->dev
->dev
);
362 EXPORT_SYMBOL(mISDNDevName4ch
);
369 printk(KERN_INFO
"Modular ISDN core version %d.%d.%d\n",
370 MISDN_MAJOR_VERSION
, MISDN_MINOR_VERSION
, MISDN_RELEASE
);
371 mISDN_init_clock(&debug
);
372 mISDN_initstack(&debug
);
373 err
= class_register(&mISDN_class
);
376 err
= mISDN_inittimer(&debug
);
379 err
= Isdnl1_Init(&debug
);
382 err
= Isdnl2_Init(&debug
);
385 err
= misdn_sock_init(&debug
);
395 mISDN_timer_cleanup();
397 class_unregister(&mISDN_class
);
402 static void mISDN_cleanup(void)
404 misdn_sock_cleanup();
407 mISDN_timer_cleanup();
408 class_unregister(&mISDN_class
);
410 printk(KERN_DEBUG
"mISDNcore unloaded\n");
413 module_init(mISDNInit
);
414 module_exit(mISDN_cleanup
);