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