1 /*
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 
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>
21 #include "core.h"
22 
23 static u_int debug;
24 
25 MODULE_AUTHOR("Karsten Keil");
26 MODULE_LICENSE("GPL");
27 module_param(debug, uint, S_IRUGO | S_IWUSR);
28 
29 static u64		device_ids;
30 #define MAX_DEVICE_ID	63
31 
32 static LIST_HEAD(Bprotocols);
33 static DEFINE_RWLOCK(bp_lock);
34 
mISDN_dev_release(struct device * dev)35 static void mISDN_dev_release(struct device *dev)
36 {
37 	/* nothing to do: the device is part of its parent's data structure */
38 }
39 
id_show(struct device * dev,struct device_attribute * attr,char * buf)40 static ssize_t id_show(struct device *dev,
41 		       struct device_attribute *attr, char *buf)
42 {
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 static DEVICE_ATTR_RO(id);
50 
nrbchan_show(struct device * dev,struct device_attribute * attr,char * buf)51 static ssize_t nrbchan_show(struct device *dev,
52 			    struct device_attribute *attr, char *buf)
53 {
54 	struct mISDNdevice *mdev = dev_to_mISDN(dev);
55 
56 	if (!mdev)
57 		return -ENODEV;
58 	return sprintf(buf, "%d\n", mdev->nrbchan);
59 }
60 static DEVICE_ATTR_RO(nrbchan);
61 
d_protocols_show(struct device * dev,struct device_attribute * attr,char * buf)62 static ssize_t d_protocols_show(struct device *dev,
63 				struct device_attribute *attr, char *buf)
64 {
65 	struct mISDNdevice *mdev = dev_to_mISDN(dev);
66 
67 	if (!mdev)
68 		return -ENODEV;
69 	return sprintf(buf, "%d\n", mdev->Dprotocols);
70 }
71 static DEVICE_ATTR_RO(d_protocols);
72 
b_protocols_show(struct device * dev,struct device_attribute * attr,char * buf)73 static ssize_t b_protocols_show(struct device *dev,
74 				struct device_attribute *attr, char *buf)
75 {
76 	struct mISDNdevice *mdev = dev_to_mISDN(dev);
77 
78 	if (!mdev)
79 		return -ENODEV;
80 	return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
81 }
82 static DEVICE_ATTR_RO(b_protocols);
83 
protocol_show(struct device * dev,struct device_attribute * attr,char * buf)84 static ssize_t protocol_show(struct device *dev,
85 			     struct device_attribute *attr, char *buf)
86 {
87 	struct mISDNdevice *mdev = dev_to_mISDN(dev);
88 
89 	if (!mdev)
90 		return -ENODEV;
91 	return sprintf(buf, "%d\n", mdev->D.protocol);
92 }
93 static DEVICE_ATTR_RO(protocol);
94 
name_show(struct device * dev,struct device_attribute * attr,char * buf)95 static ssize_t name_show(struct device *dev,
96 			 struct device_attribute *attr, char *buf)
97 {
98 	strcpy(buf, dev_name(dev));
99 	return strlen(buf);
100 }
101 static DEVICE_ATTR_RO(name);
102 
103 #if 0 /* hangs */
104 static ssize_t name_set(struct device *dev, struct device_attribute *attr,
105 			const char *buf, size_t count)
106 {
107 	int err = 0;
108 	char *out = kmalloc(count + 1, GFP_KERNEL);
109 
110 	if (!out)
111 		return -ENOMEM;
112 
113 	memcpy(out, buf, count);
114 	if (count && out[count - 1] == '\n')
115 		out[--count] = 0;
116 	if (count)
117 		err = device_rename(dev, out);
118 	kfree(out);
119 
120 	return (err < 0) ? err : count;
121 }
122 static DEVICE_ATTR_RW(name);
123 #endif
124 
channelmap_show(struct device * dev,struct device_attribute * attr,char * buf)125 static ssize_t channelmap_show(struct device *dev,
126 			       struct device_attribute *attr, char *buf)
127 {
128 	struct mISDNdevice *mdev = dev_to_mISDN(dev);
129 	char *bp = buf;
130 	int i;
131 
132 	for (i = 0; i <= mdev->nrbchan; i++)
133 		*bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';
134 
135 	return bp - buf;
136 }
137 static DEVICE_ATTR_RO(channelmap);
138 
139 static struct attribute *mISDN_attrs[] = {
140 	&dev_attr_id.attr,
141 	&dev_attr_d_protocols.attr,
142 	&dev_attr_b_protocols.attr,
143 	&dev_attr_protocol.attr,
144 	&dev_attr_channelmap.attr,
145 	&dev_attr_nrbchan.attr,
146 	&dev_attr_name.attr,
147 	NULL,
148 };
149 ATTRIBUTE_GROUPS(mISDN);
150 
mISDN_uevent(struct device * dev,struct kobj_uevent_env * env)151 static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
152 {
153 	struct mISDNdevice *mdev = dev_to_mISDN(dev);
154 
155 	if (!mdev)
156 		return 0;
157 
158 	if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
159 		return -ENOMEM;
160 
161 	return 0;
162 }
163 
mISDN_class_release(struct class * cls)164 static void mISDN_class_release(struct class *cls)
165 {
166 	/* do nothing, it's static */
167 }
168 
169 static struct class mISDN_class = {
170 	.name = "mISDN",
171 	.owner = THIS_MODULE,
172 	.dev_uevent = mISDN_uevent,
173 	.dev_groups = mISDN_groups,
174 	.dev_release = mISDN_dev_release,
175 	.class_release = mISDN_class_release,
176 };
177 
178 static int
_get_mdevice(struct device * dev,const void * id)179 _get_mdevice(struct device *dev, const void *id)
180 {
181 	struct mISDNdevice *mdev = dev_to_mISDN(dev);
182 
183 	if (!mdev)
184 		return 0;
185 	if (mdev->id != *(const u_int *)id)
186 		return 0;
187 	return 1;
188 }
189 
190 struct mISDNdevice
get_mdevice(u_int id)191 *get_mdevice(u_int id)
192 {
193 	return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
194 					      _get_mdevice));
195 }
196 
197 static int
_get_mdevice_count(struct device * dev,void * cnt)198 _get_mdevice_count(struct device *dev, void *cnt)
199 {
200 	*(int *)cnt += 1;
201 	return 0;
202 }
203 
204 int
get_mdevice_count(void)205 get_mdevice_count(void)
206 {
207 	int cnt = 0;
208 
209 	class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
210 	return cnt;
211 }
212 
213 static int
get_free_devid(void)214 get_free_devid(void)
215 {
216 	u_int	i;
217 
218 	for (i = 0; i <= MAX_DEVICE_ID; i++)
219 		if (!test_and_set_bit(i, (u_long *)&device_ids))
220 			break;
221 	if (i > MAX_DEVICE_ID)
222 		return -EBUSY;
223 	return i;
224 }
225 
226 int
mISDN_register_device(struct mISDNdevice * dev,struct device * parent,char * name)227 mISDN_register_device(struct mISDNdevice *dev,
228 		      struct device *parent, char *name)
229 {
230 	int	err;
231 
232 	err = get_free_devid();
233 	if (err < 0)
234 		return err;
235 	dev->id = err;
236 
237 	device_initialize(&dev->dev);
238 	if (name && name[0])
239 		dev_set_name(&dev->dev, "%s", name);
240 	else
241 		dev_set_name(&dev->dev, "mISDN%d", dev->id);
242 	if (debug & DEBUG_CORE)
243 		printk(KERN_DEBUG "mISDN_register %s %d\n",
244 		       dev_name(&dev->dev), dev->id);
245 	dev->dev.class = &mISDN_class;
246 
247 	err = create_stack(dev);
248 	if (err)
249 		goto error1;
250 
251 	dev->dev.platform_data = dev;
252 	dev->dev.parent = parent;
253 	dev_set_drvdata(&dev->dev, dev);
254 
255 	err = device_add(&dev->dev);
256 	if (err)
257 		goto error3;
258 	return 0;
259 
260 error3:
261 	delete_stack(dev);
262 error1:
263 	put_device(&dev->dev);
264 	return err;
265 
266 }
267 EXPORT_SYMBOL(mISDN_register_device);
268 
269 void
mISDN_unregister_device(struct mISDNdevice * dev)270 mISDN_unregister_device(struct mISDNdevice *dev) {
271 	if (debug & DEBUG_CORE)
272 		printk(KERN_DEBUG "mISDN_unregister %s %d\n",
273 		       dev_name(&dev->dev), dev->id);
274 	/* sysfs_remove_link(&dev->dev.kobj, "device"); */
275 	device_del(&dev->dev);
276 	dev_set_drvdata(&dev->dev, NULL);
277 
278 	test_and_clear_bit(dev->id, (u_long *)&device_ids);
279 	delete_stack(dev);
280 	put_device(&dev->dev);
281 }
282 EXPORT_SYMBOL(mISDN_unregister_device);
283 
284 u_int
get_all_Bprotocols(void)285 get_all_Bprotocols(void)
286 {
287 	struct Bprotocol	*bp;
288 	u_int	m = 0;
289 
290 	read_lock(&bp_lock);
291 	list_for_each_entry(bp, &Bprotocols, list)
292 		m |= bp->Bprotocols;
293 	read_unlock(&bp_lock);
294 	return m;
295 }
296 
297 struct Bprotocol *
get_Bprotocol4mask(u_int m)298 get_Bprotocol4mask(u_int m)
299 {
300 	struct Bprotocol	*bp;
301 
302 	read_lock(&bp_lock);
303 	list_for_each_entry(bp, &Bprotocols, list)
304 		if (bp->Bprotocols & m) {
305 			read_unlock(&bp_lock);
306 			return bp;
307 		}
308 	read_unlock(&bp_lock);
309 	return NULL;
310 }
311 
312 struct Bprotocol *
get_Bprotocol4id(u_int id)313 get_Bprotocol4id(u_int id)
314 {
315 	u_int	m;
316 
317 	if (id < ISDN_P_B_START || id > 63) {
318 		printk(KERN_WARNING "%s id not in range  %d\n",
319 		       __func__, id);
320 		return NULL;
321 	}
322 	m = 1 << (id & ISDN_P_B_MASK);
323 	return get_Bprotocol4mask(m);
324 }
325 
326 int
mISDN_register_Bprotocol(struct Bprotocol * bp)327 mISDN_register_Bprotocol(struct Bprotocol *bp)
328 {
329 	u_long			flags;
330 	struct Bprotocol	*old;
331 
332 	if (debug & DEBUG_CORE)
333 		printk(KERN_DEBUG "%s: %s/%x\n", __func__,
334 		       bp->name, bp->Bprotocols);
335 	old = get_Bprotocol4mask(bp->Bprotocols);
336 	if (old) {
337 		printk(KERN_WARNING
338 		       "register duplicate protocol old %s/%x new %s/%x\n",
339 		       old->name, old->Bprotocols, bp->name, bp->Bprotocols);
340 		return -EBUSY;
341 	}
342 	write_lock_irqsave(&bp_lock, flags);
343 	list_add_tail(&bp->list, &Bprotocols);
344 	write_unlock_irqrestore(&bp_lock, flags);
345 	return 0;
346 }
347 EXPORT_SYMBOL(mISDN_register_Bprotocol);
348 
349 void
mISDN_unregister_Bprotocol(struct Bprotocol * bp)350 mISDN_unregister_Bprotocol(struct Bprotocol *bp)
351 {
352 	u_long	flags;
353 
354 	if (debug & DEBUG_CORE)
355 		printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
356 		       bp->Bprotocols);
357 	write_lock_irqsave(&bp_lock, flags);
358 	list_del(&bp->list);
359 	write_unlock_irqrestore(&bp_lock, flags);
360 }
361 EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
362 
363 static const char *msg_no_channel = "<no channel>";
364 static const char *msg_no_stack = "<no stack>";
365 static const char *msg_no_stackdev = "<no stack device>";
366 
mISDNDevName4ch(struct mISDNchannel * ch)367 const char *mISDNDevName4ch(struct mISDNchannel *ch)
368 {
369 	if (!ch)
370 		return msg_no_channel;
371 	if (!ch->st)
372 		return msg_no_stack;
373 	if (!ch->st->dev)
374 		return msg_no_stackdev;
375 	return dev_name(&ch->st->dev->dev);
376 };
377 EXPORT_SYMBOL(mISDNDevName4ch);
378 
379 static int
mISDNInit(void)380 mISDNInit(void)
381 {
382 	int	err;
383 
384 	printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
385 	       MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
386 	mISDN_init_clock(&debug);
387 	mISDN_initstack(&debug);
388 	err = class_register(&mISDN_class);
389 	if (err)
390 		goto error1;
391 	err = mISDN_inittimer(&debug);
392 	if (err)
393 		goto error2;
394 	err = Isdnl1_Init(&debug);
395 	if (err)
396 		goto error3;
397 	err = Isdnl2_Init(&debug);
398 	if (err)
399 		goto error4;
400 	err = misdn_sock_init(&debug);
401 	if (err)
402 		goto error5;
403 	return 0;
404 
405 error5:
406 	Isdnl2_cleanup();
407 error4:
408 	Isdnl1_cleanup();
409 error3:
410 	mISDN_timer_cleanup();
411 error2:
412 	class_unregister(&mISDN_class);
413 error1:
414 	return err;
415 }
416 
mISDN_cleanup(void)417 static void mISDN_cleanup(void)
418 {
419 	misdn_sock_cleanup();
420 	Isdnl2_cleanup();
421 	Isdnl1_cleanup();
422 	mISDN_timer_cleanup();
423 	class_unregister(&mISDN_class);
424 
425 	printk(KERN_DEBUG "mISDNcore unloaded\n");
426 }
427 
428 module_init(mISDNInit);
429 module_exit(mISDN_cleanup);
430