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>
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
);
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
);
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
);
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
);
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
);
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
));
98 static ssize_t
_set_name(struct device
*dev
, struct device_attribute
*attr
,
99 const char *buf
, size_t count
)
102 char *out
= kmalloc(count
+ 1, GFP_KERNEL
);
107 memcpy(out
, buf
, count
);
108 if (count
&& out
[count
- 1] == '\n')
111 err
= device_rename(dev
, out
);
114 return (err
< 0) ? err
: count
;
118 static ssize_t
_show_channelmap(struct device
*dev
,
119 struct device_attribute
*attr
, char *buf
)
121 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
125 for (i
= 0; i
<= mdev
->nrbchan
; i
++)
126 *bp
++ = test_channelmap(i
, mdev
->channelmap
) ? '1' : '0';
131 static struct device_attribute mISDN_dev_attrs
[] = {
132 __ATTR(id
, S_IRUGO
, _show_id
, NULL
),
133 __ATTR(d_protocols
, S_IRUGO
, _show_d_protocols
, NULL
),
134 __ATTR(b_protocols
, S_IRUGO
, _show_b_protocols
, NULL
),
135 __ATTR(protocol
, S_IRUGO
, _show_protocol
, NULL
),
136 __ATTR(channelmap
, S_IRUGO
, _show_channelmap
, NULL
),
137 __ATTR(nrbchan
, S_IRUGO
, _show_nrbchan
, NULL
),
138 __ATTR(name
, S_IRUGO
, _show_name
, NULL
),
139 /* __ATTR(name, S_IRUGO | S_IWUSR, _show_name, _set_name), */
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
))
156 static void mISDN_class_release(struct class *cls
)
158 /* do nothing, it's static */
161 static struct class mISDN_class
= {
163 .owner
= THIS_MODULE
,
164 .dev_uevent
= mISDN_uevent
,
165 .dev_attrs
= mISDN_dev_attrs
,
166 .dev_release
= mISDN_dev_release
,
167 .class_release
= mISDN_class_release
,
171 _get_mdevice(struct device
*dev
, const void *id
)
173 struct mISDNdevice
*mdev
= dev_to_mISDN(dev
);
177 if (mdev
->id
!= *(const u_int
*)id
)
183 *get_mdevice(u_int id
)
185 return dev_to_mISDN(class_find_device(&mISDN_class
, NULL
, &id
,
190 _get_mdevice_count(struct device
*dev
, void *cnt
)
197 get_mdevice_count(void)
201 class_for_each_device(&mISDN_class
, NULL
, &cnt
, _get_mdevice_count
);
210 for (i
= 0; i
<= MAX_DEVICE_ID
; i
++)
211 if (!test_and_set_bit(i
, (u_long
*)&device_ids
))
213 if (i
> MAX_DEVICE_ID
)
219 mISDN_register_device(struct mISDNdevice
*dev
,
220 struct device
*parent
, char *name
)
224 err
= get_free_devid();
229 device_initialize(&dev
->dev
);
231 dev_set_name(&dev
->dev
, "%s", name
);
233 dev_set_name(&dev
->dev
, "mISDN%d", dev
->id
);
234 if (debug
& DEBUG_CORE
)
235 printk(KERN_DEBUG
"mISDN_register %s %d\n",
236 dev_name(&dev
->dev
), dev
->id
);
237 err
= create_stack(dev
);
241 dev
->dev
.class = &mISDN_class
;
242 dev
->dev
.platform_data
= dev
;
243 dev
->dev
.parent
= parent
;
244 dev_set_drvdata(&dev
->dev
, dev
);
246 err
= device_add(&dev
->dev
);
258 EXPORT_SYMBOL(mISDN_register_device
);
261 mISDN_unregister_device(struct mISDNdevice
*dev
) {
262 if (debug
& DEBUG_CORE
)
263 printk(KERN_DEBUG
"mISDN_unregister %s %d\n",
264 dev_name(&dev
->dev
), dev
->id
);
265 /* sysfs_remove_link(&dev->dev.kobj, "device"); */
266 device_del(&dev
->dev
);
267 dev_set_drvdata(&dev
->dev
, NULL
);
269 test_and_clear_bit(dev
->id
, (u_long
*)&device_ids
);
271 put_device(&dev
->dev
);
273 EXPORT_SYMBOL(mISDN_unregister_device
);
276 get_all_Bprotocols(void)
278 struct Bprotocol
*bp
;
282 list_for_each_entry(bp
, &Bprotocols
, list
)
284 read_unlock(&bp_lock
);
289 get_Bprotocol4mask(u_int m
)
291 struct Bprotocol
*bp
;
294 list_for_each_entry(bp
, &Bprotocols
, list
)
295 if (bp
->Bprotocols
& m
) {
296 read_unlock(&bp_lock
);
299 read_unlock(&bp_lock
);
304 get_Bprotocol4id(u_int id
)
308 if (id
< ISDN_P_B_START
|| id
> 63) {
309 printk(KERN_WARNING
"%s id not in range %d\n",
313 m
= 1 << (id
& ISDN_P_B_MASK
);
314 return get_Bprotocol4mask(m
);
318 mISDN_register_Bprotocol(struct Bprotocol
*bp
)
321 struct Bprotocol
*old
;
323 if (debug
& DEBUG_CORE
)
324 printk(KERN_DEBUG
"%s: %s/%x\n", __func__
,
325 bp
->name
, bp
->Bprotocols
);
326 old
= get_Bprotocol4mask(bp
->Bprotocols
);
329 "register duplicate protocol old %s/%x new %s/%x\n",
330 old
->name
, old
->Bprotocols
, bp
->name
, bp
->Bprotocols
);
333 write_lock_irqsave(&bp_lock
, flags
);
334 list_add_tail(&bp
->list
, &Bprotocols
);
335 write_unlock_irqrestore(&bp_lock
, flags
);
338 EXPORT_SYMBOL(mISDN_register_Bprotocol
);
341 mISDN_unregister_Bprotocol(struct Bprotocol
*bp
)
345 if (debug
& DEBUG_CORE
)
346 printk(KERN_DEBUG
"%s: %s/%x\n", __func__
, bp
->name
,
348 write_lock_irqsave(&bp_lock
, flags
);
350 write_unlock_irqrestore(&bp_lock
, flags
);
352 EXPORT_SYMBOL(mISDN_unregister_Bprotocol
);
354 static const char *msg_no_channel
= "<no channel>";
355 static const char *msg_no_stack
= "<no stack>";
356 static const char *msg_no_stackdev
= "<no stack device>";
358 const char *mISDNDevName4ch(struct mISDNchannel
*ch
)
361 return msg_no_channel
;
365 return msg_no_stackdev
;
366 return dev_name(&ch
->st
->dev
->dev
);
368 EXPORT_SYMBOL(mISDNDevName4ch
);
375 printk(KERN_INFO
"Modular ISDN core version %d.%d.%d\n",
376 MISDN_MAJOR_VERSION
, MISDN_MINOR_VERSION
, MISDN_RELEASE
);
377 mISDN_init_clock(&debug
);
378 mISDN_initstack(&debug
);
379 err
= class_register(&mISDN_class
);
382 err
= mISDN_inittimer(&debug
);
385 err
= l1_init(&debug
);
388 err
= Isdnl2_Init(&debug
);
391 err
= misdn_sock_init(&debug
);
401 mISDN_timer_cleanup();
403 class_unregister(&mISDN_class
);
408 static void mISDN_cleanup(void)
410 misdn_sock_cleanup();
413 mISDN_timer_cleanup();
414 class_unregister(&mISDN_class
);
416 printk(KERN_DEBUG
"mISDNcore unloaded\n");
419 module_init(mISDNInit
);
420 module_exit(mISDN_cleanup
);