blob: da30c5cb96096b6ff366c4473b9199ae20a72919 [file] [log] [blame]
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001/*
2 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
3 *
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.
7 *
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.
12 *
13 */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020016#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"
22
23static u_int debug;
24
25MODULE_AUTHOR("Karsten Keil");
26MODULE_LICENSE("GPL");
27module_param(debug, uint, S_IRUGO | S_IWUSR);
28
Karsten Keil1b2b03f2008-07-27 01:54:58 +020029static u64 device_ids;
30#define MAX_DEVICE_ID 63
31
32static LIST_HEAD(Bprotocols);
Hannes Eder5b834352008-12-12 21:15:17 -080033static DEFINE_RWLOCK(bp_lock);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020034
Matthias Urlichsb36b6542008-08-16 00:09:24 +020035static void mISDN_dev_release(struct device *dev)
36{
37 /* nothing to do: the device is part of its parent's data structure */
38}
39
40static ssize_t _show_id(struct device *dev,
Joe Perches475be4d2012-02-19 19:52:38 -080041 struct device_attribute *attr, char *buf)
Matthias Urlichsb36b6542008-08-16 00:09:24 +020042{
43 struct mISDNdevice *mdev = dev_to_mISDN(dev);
44
45 if (!mdev)
46 return -ENODEV;
47 return sprintf(buf, "%d\n", mdev->id);
48}
49
50static ssize_t _show_nrbchan(struct device *dev,
Joe Perches475be4d2012-02-19 19:52:38 -080051 struct device_attribute *attr, char *buf)
Matthias Urlichsb36b6542008-08-16 00:09:24 +020052{
53 struct mISDNdevice *mdev = dev_to_mISDN(dev);
54
55 if (!mdev)
56 return -ENODEV;
57 return sprintf(buf, "%d\n", mdev->nrbchan);
58}
59
60static ssize_t _show_d_protocols(struct device *dev,
Joe Perches475be4d2012-02-19 19:52:38 -080061 struct device_attribute *attr, char *buf)
Matthias Urlichsb36b6542008-08-16 00:09:24 +020062{
63 struct mISDNdevice *mdev = dev_to_mISDN(dev);
64
65 if (!mdev)
66 return -ENODEV;
67 return sprintf(buf, "%d\n", mdev->Dprotocols);
68}
69
70static ssize_t _show_b_protocols(struct device *dev,
Joe Perches475be4d2012-02-19 19:52:38 -080071 struct device_attribute *attr, char *buf)
Matthias Urlichsb36b6542008-08-16 00:09:24 +020072{
73 struct mISDNdevice *mdev = dev_to_mISDN(dev);
74
75 if (!mdev)
76 return -ENODEV;
77 return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
78}
79
80static ssize_t _show_protocol(struct device *dev,
Joe Perches475be4d2012-02-19 19:52:38 -080081 struct device_attribute *attr, char *buf)
Matthias Urlichsb36b6542008-08-16 00:09:24 +020082{
83 struct mISDNdevice *mdev = dev_to_mISDN(dev);
84
85 if (!mdev)
86 return -ENODEV;
87 return sprintf(buf, "%d\n", mdev->D.protocol);
88}
89
90static ssize_t _show_name(struct device *dev,
Joe Perches475be4d2012-02-19 19:52:38 -080091 struct device_attribute *attr, char *buf)
Matthias Urlichsb36b6542008-08-16 00:09:24 +020092{
93 strcpy(buf, dev_name(dev));
94 return strlen(buf);
95}
96
97#if 0 /* hangs */
98static ssize_t _set_name(struct device *dev, struct device_attribute *attr,
Joe Perches475be4d2012-02-19 19:52:38 -080099 const char *buf, size_t count)
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200100{
101 int err = 0;
102 char *out = kmalloc(count + 1, GFP_KERNEL);
103
104 if (!out)
105 return -ENOMEM;
106
107 memcpy(out, buf, count);
108 if (count && out[count - 1] == '\n')
109 out[--count] = 0;
110 if (count)
111 err = device_rename(dev, out);
112 kfree(out);
113
114 return (err < 0) ? err : count;
115}
116#endif
117
118static ssize_t _show_channelmap(struct device *dev,
119 struct device_attribute *attr, char *buf)
120{
121 struct mISDNdevice *mdev = dev_to_mISDN(dev);
122 char *bp = buf;
123 int i;
124
125 for (i = 0; i <= mdev->nrbchan; i++)
126 *bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';
127
128 return bp - buf;
129}
130
131static 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),
Joe Perches475be4d2012-02-19 19:52:38 -0800139/* __ATTR(name, S_IRUGO | S_IWUSR, _show_name, _set_name), */
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200140 {}
141};
142
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200143static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
144{
145 struct mISDNdevice *mdev = dev_to_mISDN(dev);
146
147 if (!mdev)
148 return 0;
149
150 if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
151 return -ENOMEM;
152
153 return 0;
154}
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200155
156static void mISDN_class_release(struct class *cls)
157{
158 /* do nothing, it's static */
159}
160
161static struct class mISDN_class = {
162 .name = "mISDN",
163 .owner = THIS_MODULE,
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200164 .dev_uevent = mISDN_uevent,
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200165 .dev_attrs = mISDN_dev_attrs,
166 .dev_release = mISDN_dev_release,
167 .class_release = mISDN_class_release,
168};
169
170static int
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100171_get_mdevice(struct device *dev, const void *id)
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200172{
173 struct mISDNdevice *mdev = dev_to_mISDN(dev);
174
175 if (!mdev)
176 return 0;
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100177 if (mdev->id != *(const u_int *)id)
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200178 return 0;
179 return 1;
180}
181
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200182struct mISDNdevice
183*get_mdevice(u_int id)
184{
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200185 return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
Joe Perches475be4d2012-02-19 19:52:38 -0800186 _get_mdevice));
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200187}
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200188
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200189static int
190_get_mdevice_count(struct device *dev, void *cnt)
191{
192 *(int *)cnt += 1;
193 return 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200194}
195
196int
197get_mdevice_count(void)
198{
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200199 int cnt = 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200200
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200201 class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200202 return cnt;
203}
204
205static int
206get_free_devid(void)
207{
208 u_int i;
209
210 for (i = 0; i <= MAX_DEVICE_ID; i++)
211 if (!test_and_set_bit(i, (u_long *)&device_ids))
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200212 break;
213 if (i > MAX_DEVICE_ID)
Roel Kluinddacd142009-05-22 11:04:53 +0000214 return -EBUSY;
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200215 return i;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200216}
217
218int
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200219mISDN_register_device(struct mISDNdevice *dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800220 struct device *parent, char *name)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200221{
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200222 int err;
223
Roel Kluinddacd142009-05-22 11:04:53 +0000224 err = get_free_devid();
225 if (err < 0)
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200226 goto error1;
Roel Kluinddacd142009-05-22 11:04:53 +0000227 dev->id = err;
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200228
229 device_initialize(&dev->dev);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200230 if (name && name[0])
Matthias Urlichs837468d2008-08-16 00:04:33 +0200231 dev_set_name(&dev->dev, "%s", name);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200232 else
Matthias Urlichs837468d2008-08-16 00:04:33 +0200233 dev_set_name(&dev->dev, "mISDN%d", dev->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200234 if (debug & DEBUG_CORE)
235 printk(KERN_DEBUG "mISDN_register %s %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800236 dev_name(&dev->dev), dev->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200237 err = create_stack(dev);
238 if (err)
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200239 goto error1;
240
241 dev->dev.class = &mISDN_class;
242 dev->dev.platform_data = dev;
243 dev->dev.parent = parent;
244 dev_set_drvdata(&dev->dev, dev);
245
246 err = device_add(&dev->dev);
247 if (err)
248 goto error3;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200249 return 0;
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200250
251error3:
252 delete_stack(dev);
253 return err;
254error1:
255 return err;
256
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200257}
258EXPORT_SYMBOL(mISDN_register_device);
259
260void
261mISDN_unregister_device(struct mISDNdevice *dev) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200262 if (debug & DEBUG_CORE)
263 printk(KERN_DEBUG "mISDN_unregister %s %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800264 dev_name(&dev->dev), dev->id);
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200265 /* sysfs_remove_link(&dev->dev.kobj, "device"); */
266 device_del(&dev->dev);
267 dev_set_drvdata(&dev->dev, NULL);
268
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200269 test_and_clear_bit(dev->id, (u_long *)&device_ids);
270 delete_stack(dev);
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200271 put_device(&dev->dev);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200272}
273EXPORT_SYMBOL(mISDN_unregister_device);
274
275u_int
276get_all_Bprotocols(void)
277{
278 struct Bprotocol *bp;
279 u_int m = 0;
280
281 read_lock(&bp_lock);
282 list_for_each_entry(bp, &Bprotocols, list)
283 m |= bp->Bprotocols;
284 read_unlock(&bp_lock);
285 return m;
286}
287
288struct Bprotocol *
289get_Bprotocol4mask(u_int m)
290{
291 struct Bprotocol *bp;
292
293 read_lock(&bp_lock);
294 list_for_each_entry(bp, &Bprotocols, list)
295 if (bp->Bprotocols & m) {
296 read_unlock(&bp_lock);
297 return bp;
298 }
299 read_unlock(&bp_lock);
300 return NULL;
301}
302
303struct Bprotocol *
304get_Bprotocol4id(u_int id)
305{
306 u_int m;
307
308 if (id < ISDN_P_B_START || id > 63) {
309 printk(KERN_WARNING "%s id not in range %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800310 __func__, id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200311 return NULL;
312 }
313 m = 1 << (id & ISDN_P_B_MASK);
314 return get_Bprotocol4mask(m);
315}
316
317int
318mISDN_register_Bprotocol(struct Bprotocol *bp)
319{
320 u_long flags;
321 struct Bprotocol *old;
322
323 if (debug & DEBUG_CORE)
324 printk(KERN_DEBUG "%s: %s/%x\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800325 bp->name, bp->Bprotocols);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200326 old = get_Bprotocol4mask(bp->Bprotocols);
327 if (old) {
328 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800329 "register duplicate protocol old %s/%x new %s/%x\n",
330 old->name, old->Bprotocols, bp->name, bp->Bprotocols);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200331 return -EBUSY;
332 }
333 write_lock_irqsave(&bp_lock, flags);
334 list_add_tail(&bp->list, &Bprotocols);
335 write_unlock_irqrestore(&bp_lock, flags);
336 return 0;
337}
338EXPORT_SYMBOL(mISDN_register_Bprotocol);
339
340void
341mISDN_unregister_Bprotocol(struct Bprotocol *bp)
342{
343 u_long flags;
344
345 if (debug & DEBUG_CORE)
346 printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
Joe Perches475be4d2012-02-19 19:52:38 -0800347 bp->Bprotocols);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200348 write_lock_irqsave(&bp_lock, flags);
349 list_del(&bp->list);
350 write_unlock_irqrestore(&bp_lock, flags);
351}
352EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
353
Karsten Keilf45ebf32012-05-04 04:15:35 +0000354static const char *msg_no_channel = "<no channel>";
355static const char *msg_no_stack = "<no stack>";
356static const char *msg_no_stackdev = "<no stack device>";
357
358const char *mISDNDevName4ch(struct mISDNchannel *ch)
359{
360 if (!ch)
361 return msg_no_channel;
362 if (!ch->st)
363 return msg_no_stack;
364 if (!ch->st->dev)
365 return msg_no_stackdev;
366 return dev_name(&ch->st->dev->dev);
367};
368EXPORT_SYMBOL(mISDNDevName4ch);
369
Hannes Eder5b834352008-12-12 21:15:17 -0800370static int
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200371mISDNInit(void)
372{
373 int err;
374
375 printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800376 MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
Andreas Eversberg3bd69ad2008-09-06 09:03:46 +0200377 mISDN_init_clock(&debug);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200378 mISDN_initstack(&debug);
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200379 err = class_register(&mISDN_class);
380 if (err)
381 goto error1;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200382 err = mISDN_inittimer(&debug);
383 if (err)
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200384 goto error2;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200385 err = l1_init(&debug);
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200386 if (err)
387 goto error3;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200388 err = Isdnl2_Init(&debug);
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200389 if (err)
390 goto error4;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200391 err = misdn_sock_init(&debug);
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200392 if (err)
393 goto error5;
394 return 0;
395
396error5:
397 Isdnl2_cleanup();
398error4:
399 l1_cleanup();
400error3:
401 mISDN_timer_cleanup();
402error2:
403 class_unregister(&mISDN_class);
404error1:
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200405 return err;
406}
407
Hannes Eder5b834352008-12-12 21:15:17 -0800408static void mISDN_cleanup(void)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200409{
410 misdn_sock_cleanup();
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200411 Isdnl2_cleanup();
Matthias Urlichsb36b6542008-08-16 00:09:24 +0200412 l1_cleanup();
413 mISDN_timer_cleanup();
414 class_unregister(&mISDN_class);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200415
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200416 printk(KERN_DEBUG "mISDNcore unloaded\n");
417}
418
419module_init(mISDNInit);
420module_exit(mISDN_cleanup);