Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2008  by Karsten Keil <kkeil@novell.com>
0004  */
0005 
0006 #include <linux/slab.h>
0007 #include <linux/types.h>
0008 #include <linux/stddef.h>
0009 #include <linux/module.h>
0010 #include <linux/spinlock.h>
0011 #include <linux/mISDNif.h>
0012 #include "core.h"
0013 
0014 static u_int debug;
0015 
0016 MODULE_AUTHOR("Karsten Keil");
0017 MODULE_LICENSE("GPL");
0018 module_param(debug, uint, S_IRUGO | S_IWUSR);
0019 
0020 static u64      device_ids;
0021 #define MAX_DEVICE_ID   63
0022 
0023 static LIST_HEAD(Bprotocols);
0024 static DEFINE_RWLOCK(bp_lock);
0025 
0026 static void mISDN_dev_release(struct device *dev)
0027 {
0028     /* nothing to do: the device is part of its parent's data structure */
0029 }
0030 
0031 static ssize_t id_show(struct device *dev,
0032                struct device_attribute *attr, char *buf)
0033 {
0034     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0035 
0036     if (!mdev)
0037         return -ENODEV;
0038     return sprintf(buf, "%d\n", mdev->id);
0039 }
0040 static DEVICE_ATTR_RO(id);
0041 
0042 static ssize_t nrbchan_show(struct device *dev,
0043                 struct device_attribute *attr, char *buf)
0044 {
0045     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0046 
0047     if (!mdev)
0048         return -ENODEV;
0049     return sprintf(buf, "%d\n", mdev->nrbchan);
0050 }
0051 static DEVICE_ATTR_RO(nrbchan);
0052 
0053 static ssize_t d_protocols_show(struct device *dev,
0054                 struct device_attribute *attr, char *buf)
0055 {
0056     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0057 
0058     if (!mdev)
0059         return -ENODEV;
0060     return sprintf(buf, "%d\n", mdev->Dprotocols);
0061 }
0062 static DEVICE_ATTR_RO(d_protocols);
0063 
0064 static ssize_t b_protocols_show(struct device *dev,
0065                 struct device_attribute *attr, char *buf)
0066 {
0067     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0068 
0069     if (!mdev)
0070         return -ENODEV;
0071     return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
0072 }
0073 static DEVICE_ATTR_RO(b_protocols);
0074 
0075 static ssize_t protocol_show(struct device *dev,
0076                  struct device_attribute *attr, char *buf)
0077 {
0078     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0079 
0080     if (!mdev)
0081         return -ENODEV;
0082     return sprintf(buf, "%d\n", mdev->D.protocol);
0083 }
0084 static DEVICE_ATTR_RO(protocol);
0085 
0086 static ssize_t name_show(struct device *dev,
0087              struct device_attribute *attr, char *buf)
0088 {
0089     strcpy(buf, dev_name(dev));
0090     return strlen(buf);
0091 }
0092 static DEVICE_ATTR_RO(name);
0093 
0094 #if 0 /* hangs */
0095 static ssize_t name_set(struct device *dev, struct device_attribute *attr,
0096             const char *buf, size_t count)
0097 {
0098     int err = 0;
0099     char *out = kmalloc(count + 1, GFP_KERNEL);
0100 
0101     if (!out)
0102         return -ENOMEM;
0103 
0104     memcpy(out, buf, count);
0105     if (count && out[count - 1] == '\n')
0106         out[--count] = 0;
0107     if (count)
0108         err = device_rename(dev, out);
0109     kfree(out);
0110 
0111     return (err < 0) ? err : count;
0112 }
0113 static DEVICE_ATTR_RW(name);
0114 #endif
0115 
0116 static ssize_t channelmap_show(struct device *dev,
0117                    struct device_attribute *attr, char *buf)
0118 {
0119     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0120     char *bp = buf;
0121     int i;
0122 
0123     for (i = 0; i <= mdev->nrbchan; i++)
0124         *bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';
0125 
0126     return bp - buf;
0127 }
0128 static DEVICE_ATTR_RO(channelmap);
0129 
0130 static struct attribute *mISDN_attrs[] = {
0131     &dev_attr_id.attr,
0132     &dev_attr_d_protocols.attr,
0133     &dev_attr_b_protocols.attr,
0134     &dev_attr_protocol.attr,
0135     &dev_attr_channelmap.attr,
0136     &dev_attr_nrbchan.attr,
0137     &dev_attr_name.attr,
0138     NULL,
0139 };
0140 ATTRIBUTE_GROUPS(mISDN);
0141 
0142 static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
0143 {
0144     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0145 
0146     if (!mdev)
0147         return 0;
0148 
0149     if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
0150         return -ENOMEM;
0151 
0152     return 0;
0153 }
0154 
0155 static void mISDN_class_release(struct class *cls)
0156 {
0157     /* do nothing, it's static */
0158 }
0159 
0160 static struct class mISDN_class = {
0161     .name = "mISDN",
0162     .owner = THIS_MODULE,
0163     .dev_uevent = mISDN_uevent,
0164     .dev_groups = mISDN_groups,
0165     .dev_release = mISDN_dev_release,
0166     .class_release = mISDN_class_release,
0167 };
0168 
0169 static int
0170 _get_mdevice(struct device *dev, const void *id)
0171 {
0172     struct mISDNdevice *mdev = dev_to_mISDN(dev);
0173 
0174     if (!mdev)
0175         return 0;
0176     if (mdev->id != *(const u_int *)id)
0177         return 0;
0178     return 1;
0179 }
0180 
0181 struct mISDNdevice
0182 *get_mdevice(u_int id)
0183 {
0184     return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
0185                           _get_mdevice));
0186 }
0187 
0188 static int
0189 _get_mdevice_count(struct device *dev, void *cnt)
0190 {
0191     *(int *)cnt += 1;
0192     return 0;
0193 }
0194 
0195 int
0196 get_mdevice_count(void)
0197 {
0198     int cnt = 0;
0199 
0200     class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
0201     return cnt;
0202 }
0203 
0204 static int
0205 get_free_devid(void)
0206 {
0207     u_int   i;
0208 
0209     for (i = 0; i <= MAX_DEVICE_ID; i++)
0210         if (!test_and_set_bit(i, (u_long *)&device_ids))
0211             break;
0212     if (i > MAX_DEVICE_ID)
0213         return -EBUSY;
0214     return i;
0215 }
0216 
0217 int
0218 mISDN_register_device(struct mISDNdevice *dev,
0219               struct device *parent, char *name)
0220 {
0221     int err;
0222 
0223     err = get_free_devid();
0224     if (err < 0)
0225         goto error1;
0226     dev->id = err;
0227 
0228     device_initialize(&dev->dev);
0229     if (name && name[0])
0230         dev_set_name(&dev->dev, "%s", name);
0231     else
0232         dev_set_name(&dev->dev, "mISDN%d", dev->id);
0233     if (debug & DEBUG_CORE)
0234         printk(KERN_DEBUG "mISDN_register %s %d\n",
0235                dev_name(&dev->dev), dev->id);
0236     err = create_stack(dev);
0237     if (err)
0238         goto error1;
0239 
0240     dev->dev.class = &mISDN_class;
0241     dev->dev.platform_data = dev;
0242     dev->dev.parent = parent;
0243     dev_set_drvdata(&dev->dev, dev);
0244 
0245     err = device_add(&dev->dev);
0246     if (err)
0247         goto error3;
0248     return 0;
0249 
0250 error3:
0251     delete_stack(dev);
0252     return err;
0253 error1:
0254     return err;
0255 
0256 }
0257 EXPORT_SYMBOL(mISDN_register_device);
0258 
0259 void
0260 mISDN_unregister_device(struct mISDNdevice *dev) {
0261     if (debug & DEBUG_CORE)
0262         printk(KERN_DEBUG "mISDN_unregister %s %d\n",
0263                dev_name(&dev->dev), dev->id);
0264     /* sysfs_remove_link(&dev->dev.kobj, "device"); */
0265     device_del(&dev->dev);
0266     dev_set_drvdata(&dev->dev, NULL);
0267 
0268     test_and_clear_bit(dev->id, (u_long *)&device_ids);
0269     delete_stack(dev);
0270     put_device(&dev->dev);
0271 }
0272 EXPORT_SYMBOL(mISDN_unregister_device);
0273 
0274 u_int
0275 get_all_Bprotocols(void)
0276 {
0277     struct Bprotocol    *bp;
0278     u_int   m = 0;
0279 
0280     read_lock(&bp_lock);
0281     list_for_each_entry(bp, &Bprotocols, list)
0282         m |= bp->Bprotocols;
0283     read_unlock(&bp_lock);
0284     return m;
0285 }
0286 
0287 struct Bprotocol *
0288 get_Bprotocol4mask(u_int m)
0289 {
0290     struct Bprotocol    *bp;
0291 
0292     read_lock(&bp_lock);
0293     list_for_each_entry(bp, &Bprotocols, list)
0294         if (bp->Bprotocols & m) {
0295             read_unlock(&bp_lock);
0296             return bp;
0297         }
0298     read_unlock(&bp_lock);
0299     return NULL;
0300 }
0301 
0302 struct Bprotocol *
0303 get_Bprotocol4id(u_int id)
0304 {
0305     u_int   m;
0306 
0307     if (id < ISDN_P_B_START || id > 63) {
0308         printk(KERN_WARNING "%s id not in range  %d\n",
0309                __func__, id);
0310         return NULL;
0311     }
0312     m = 1 << (id & ISDN_P_B_MASK);
0313     return get_Bprotocol4mask(m);
0314 }
0315 
0316 int
0317 mISDN_register_Bprotocol(struct Bprotocol *bp)
0318 {
0319     u_long          flags;
0320     struct Bprotocol    *old;
0321 
0322     if (debug & DEBUG_CORE)
0323         printk(KERN_DEBUG "%s: %s/%x\n", __func__,
0324                bp->name, bp->Bprotocols);
0325     old = get_Bprotocol4mask(bp->Bprotocols);
0326     if (old) {
0327         printk(KERN_WARNING
0328                "register duplicate protocol old %s/%x new %s/%x\n",
0329                old->name, old->Bprotocols, bp->name, bp->Bprotocols);
0330         return -EBUSY;
0331     }
0332     write_lock_irqsave(&bp_lock, flags);
0333     list_add_tail(&bp->list, &Bprotocols);
0334     write_unlock_irqrestore(&bp_lock, flags);
0335     return 0;
0336 }
0337 EXPORT_SYMBOL(mISDN_register_Bprotocol);
0338 
0339 void
0340 mISDN_unregister_Bprotocol(struct Bprotocol *bp)
0341 {
0342     u_long  flags;
0343 
0344     if (debug & DEBUG_CORE)
0345         printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
0346                bp->Bprotocols);
0347     write_lock_irqsave(&bp_lock, flags);
0348     list_del(&bp->list);
0349     write_unlock_irqrestore(&bp_lock, flags);
0350 }
0351 EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
0352 
0353 static const char *msg_no_channel = "<no channel>";
0354 static const char *msg_no_stack = "<no stack>";
0355 static const char *msg_no_stackdev = "<no stack device>";
0356 
0357 const char *mISDNDevName4ch(struct mISDNchannel *ch)
0358 {
0359     if (!ch)
0360         return msg_no_channel;
0361     if (!ch->st)
0362         return msg_no_stack;
0363     if (!ch->st->dev)
0364         return msg_no_stackdev;
0365     return dev_name(&ch->st->dev->dev);
0366 };
0367 EXPORT_SYMBOL(mISDNDevName4ch);
0368 
0369 static int
0370 mISDNInit(void)
0371 {
0372     int err;
0373 
0374     printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
0375            MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
0376     mISDN_init_clock(&debug);
0377     mISDN_initstack(&debug);
0378     err = class_register(&mISDN_class);
0379     if (err)
0380         goto error1;
0381     err = mISDN_inittimer(&debug);
0382     if (err)
0383         goto error2;
0384     err = Isdnl1_Init(&debug);
0385     if (err)
0386         goto error3;
0387     err = Isdnl2_Init(&debug);
0388     if (err)
0389         goto error4;
0390     err = misdn_sock_init(&debug);
0391     if (err)
0392         goto error5;
0393     return 0;
0394 
0395 error5:
0396     Isdnl2_cleanup();
0397 error4:
0398     Isdnl1_cleanup();
0399 error3:
0400     mISDN_timer_cleanup();
0401 error2:
0402     class_unregister(&mISDN_class);
0403 error1:
0404     return err;
0405 }
0406 
0407 static void mISDN_cleanup(void)
0408 {
0409     misdn_sock_cleanup();
0410     Isdnl2_cleanup();
0411     Isdnl1_cleanup();
0412     mISDN_timer_cleanup();
0413     class_unregister(&mISDN_class);
0414 
0415     printk(KERN_DEBUG "mISDNcore unloaded\n");
0416 }
0417 
0418 module_init(mISDNInit);
0419 module_exit(mISDN_cleanup);