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