core.c 8.8 KB
Newer Older
K
Karsten Keil 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright 2008  by Karsten Keil <kkeil@novell.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

15
#include <linux/slab.h>
K
Karsten Keil 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/mISDNif.h>
#include "core.h"

static u_int debug;

MODULE_AUTHOR("Karsten Keil");
MODULE_LICENSE("GPL");
module_param(debug, uint, S_IRUGO | S_IWUSR);

static u64		device_ids;
#define MAX_DEVICE_ID	63

static LIST_HEAD(Bprotocols);
33
static DEFINE_RWLOCK(bp_lock);
K
Karsten Keil 已提交
34

35 36 37 38 39 40
static void mISDN_dev_release(struct device *dev)
{
	/* nothing to do: the device is part of its parent's data structure */
}

static ssize_t _show_id(struct device *dev,
41
			struct device_attribute *attr, char *buf)
42 43 44 45 46 47 48 49 50
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);

	if (!mdev)
		return -ENODEV;
	return sprintf(buf, "%d\n", mdev->id);
}

static ssize_t _show_nrbchan(struct device *dev,
51
			     struct device_attribute *attr, char *buf)
52 53 54 55 56 57 58 59 60
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);

	if (!mdev)
		return -ENODEV;
	return sprintf(buf, "%d\n", mdev->nrbchan);
}

static ssize_t _show_d_protocols(struct device *dev,
61
				 struct device_attribute *attr, char *buf)
62 63 64 65 66 67 68 69 70
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);

	if (!mdev)
		return -ENODEV;
	return sprintf(buf, "%d\n", mdev->Dprotocols);
}

static ssize_t _show_b_protocols(struct device *dev,
71
				 struct device_attribute *attr, char *buf)
72 73 74 75 76 77 78 79 80
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);

	if (!mdev)
		return -ENODEV;
	return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
}

static ssize_t _show_protocol(struct device *dev,
81
			      struct device_attribute *attr, char *buf)
82 83 84 85 86 87 88 89 90
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);

	if (!mdev)
		return -ENODEV;
	return sprintf(buf, "%d\n", mdev->D.protocol);
}

static ssize_t _show_name(struct device *dev,
91
			  struct device_attribute *attr, char *buf)
92 93 94 95 96 97 98
{
	strcpy(buf, dev_name(dev));
	return strlen(buf);
}

#if 0 /* hangs */
static ssize_t _set_name(struct device *dev, struct device_attribute *attr,
99
			 const char *buf, size_t count)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
{
	int err = 0;
	char *out = kmalloc(count + 1, GFP_KERNEL);

	if (!out)
		return -ENOMEM;

	memcpy(out, buf, count);
	if (count && out[count - 1] == '\n')
		out[--count] = 0;
	if (count)
		err = device_rename(dev, out);
	kfree(out);

	return (err < 0) ? err : count;
}
#endif

static ssize_t _show_channelmap(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);
	char *bp = buf;
	int i;

	for (i = 0; i <= mdev->nrbchan; i++)
		*bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';

	return bp - buf;
}

static struct device_attribute mISDN_dev_attrs[] = {
	__ATTR(id,          S_IRUGO,         _show_id,          NULL),
	__ATTR(d_protocols, S_IRUGO,         _show_d_protocols, NULL),
	__ATTR(b_protocols, S_IRUGO,         _show_b_protocols, NULL),
	__ATTR(protocol,    S_IRUGO,         _show_protocol,    NULL),
	__ATTR(channelmap,  S_IRUGO,         _show_channelmap,  NULL),
	__ATTR(nrbchan,     S_IRUGO,         _show_nrbchan,     NULL),
	__ATTR(name,        S_IRUGO,         _show_name,        NULL),
139
/*	__ATTR(name,        S_IRUGO | S_IWUSR, _show_name,      _set_name), */
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	{}
};

static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);

	if (!mdev)
		return 0;

	if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
		return -ENOMEM;

	return 0;
}

static void mISDN_class_release(struct class *cls)
{
	/* do nothing, it's static */
}

static struct class mISDN_class = {
	.name = "mISDN",
	.owner = THIS_MODULE,
	.dev_uevent = mISDN_uevent,
	.dev_attrs = mISDN_dev_attrs,
	.dev_release = mISDN_dev_release,
	.class_release = mISDN_class_release,
};

static int
_get_mdevice(struct device *dev, void *id)
{
	struct mISDNdevice *mdev = dev_to_mISDN(dev);

	if (!mdev)
		return 0;
	if (mdev->id != *(u_int *)id)
		return 0;
	return 1;
}

K
Karsten Keil 已提交
182 183 184
struct mISDNdevice
*get_mdevice(u_int id)
{
185
	return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
186
					      _get_mdevice));
187
}
K
Karsten Keil 已提交
188

189 190 191 192 193
static int
_get_mdevice_count(struct device *dev, void *cnt)
{
	*(int *)cnt += 1;
	return 0;
K
Karsten Keil 已提交
194 195 196 197 198
}

int
get_mdevice_count(void)
{
199
	int cnt = 0;
K
Karsten Keil 已提交
200

201
	class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
K
Karsten Keil 已提交
202 203 204 205 206 207 208 209 210 211
	return cnt;
}

static int
get_free_devid(void)
{
	u_int	i;

	for (i = 0; i <= MAX_DEVICE_ID; i++)
		if (!test_and_set_bit(i, (u_long *)&device_ids))
212 213
			break;
	if (i > MAX_DEVICE_ID)
214
		return -EBUSY;
215
	return i;
K
Karsten Keil 已提交
216 217 218
}

int
219
mISDN_register_device(struct mISDNdevice *dev,
220
		      struct device *parent, char *name)
K
Karsten Keil 已提交
221 222 223
{
	int	err;

224 225
	err = get_free_devid();
	if (err < 0)
226
		goto error1;
227
	dev->id = err;
228 229

	device_initialize(&dev->dev);
K
Karsten Keil 已提交
230
	if (name && name[0])
231
		dev_set_name(&dev->dev, "%s", name);
K
Karsten Keil 已提交
232
	else
233
		dev_set_name(&dev->dev, "mISDN%d", dev->id);
K
Karsten Keil 已提交
234 235
	if (debug & DEBUG_CORE)
		printk(KERN_DEBUG "mISDN_register %s %d\n",
236
		       dev_name(&dev->dev), dev->id);
K
Karsten Keil 已提交
237 238
	err = create_stack(dev);
	if (err)
239 240 241 242 243 244 245 246 247 248
		goto error1;

	dev->dev.class = &mISDN_class;
	dev->dev.platform_data = dev;
	dev->dev.parent = parent;
	dev_set_drvdata(&dev->dev, dev);

	err = device_add(&dev->dev);
	if (err)
		goto error3;
K
Karsten Keil 已提交
249
	return 0;
250 251 252 253 254 255 256

error3:
	delete_stack(dev);
	return err;
error1:
	return err;

K
Karsten Keil 已提交
257 258 259 260 261 262 263
}
EXPORT_SYMBOL(mISDN_register_device);

void
mISDN_unregister_device(struct mISDNdevice *dev) {
	if (debug & DEBUG_CORE)
		printk(KERN_DEBUG "mISDN_unregister %s %d\n",
264
		       dev_name(&dev->dev), dev->id);
265 266 267 268
	/* sysfs_remove_link(&dev->dev.kobj, "device"); */
	device_del(&dev->dev);
	dev_set_drvdata(&dev->dev, NULL);

K
Karsten Keil 已提交
269 270
	test_and_clear_bit(dev->id, (u_long *)&device_ids);
	delete_stack(dev);
271
	put_device(&dev->dev);
K
Karsten Keil 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
}
EXPORT_SYMBOL(mISDN_unregister_device);

u_int
get_all_Bprotocols(void)
{
	struct Bprotocol	*bp;
	u_int	m = 0;

	read_lock(&bp_lock);
	list_for_each_entry(bp, &Bprotocols, list)
		m |= bp->Bprotocols;
	read_unlock(&bp_lock);
	return m;
}

struct Bprotocol *
get_Bprotocol4mask(u_int m)
{
	struct Bprotocol	*bp;

	read_lock(&bp_lock);
	list_for_each_entry(bp, &Bprotocols, list)
		if (bp->Bprotocols & m) {
			read_unlock(&bp_lock);
			return bp;
		}
	read_unlock(&bp_lock);
	return NULL;
}

struct Bprotocol *
get_Bprotocol4id(u_int id)
{
	u_int	m;

	if (id < ISDN_P_B_START || id > 63) {
		printk(KERN_WARNING "%s id not in range  %d\n",
310
		       __func__, id);
K
Karsten Keil 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324
		return NULL;
	}
	m = 1 << (id & ISDN_P_B_MASK);
	return get_Bprotocol4mask(m);
}

int
mISDN_register_Bprotocol(struct Bprotocol *bp)
{
	u_long			flags;
	struct Bprotocol	*old;

	if (debug & DEBUG_CORE)
		printk(KERN_DEBUG "%s: %s/%x\n", __func__,
325
		       bp->name, bp->Bprotocols);
K
Karsten Keil 已提交
326 327 328
	old = get_Bprotocol4mask(bp->Bprotocols);
	if (old) {
		printk(KERN_WARNING
329 330
		       "register duplicate protocol old %s/%x new %s/%x\n",
		       old->name, old->Bprotocols, bp->name, bp->Bprotocols);
K
Karsten Keil 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		return -EBUSY;
	}
	write_lock_irqsave(&bp_lock, flags);
	list_add_tail(&bp->list, &Bprotocols);
	write_unlock_irqrestore(&bp_lock, flags);
	return 0;
}
EXPORT_SYMBOL(mISDN_register_Bprotocol);

void
mISDN_unregister_Bprotocol(struct Bprotocol *bp)
{
	u_long	flags;

	if (debug & DEBUG_CORE)
		printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
347
		       bp->Bprotocols);
K
Karsten Keil 已提交
348 349 350 351 352 353
	write_lock_irqsave(&bp_lock, flags);
	list_del(&bp->list);
	write_unlock_irqrestore(&bp_lock, flags);
}
EXPORT_SYMBOL(mISDN_unregister_Bprotocol);

K
Karsten Keil 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
static const char *msg_no_channel = "<no channel>";
static const char *msg_no_stack = "<no stack>";
static const char *msg_no_stackdev = "<no stack device>";

const char *mISDNDevName4ch(struct mISDNchannel *ch)
{
	if (!ch)
		return msg_no_channel;
	if (!ch->st)
		return msg_no_stack;
	if (!ch->st->dev)
		return msg_no_stackdev;
	return dev_name(&ch->st->dev->dev);
};
EXPORT_SYMBOL(mISDNDevName4ch);

370
static int
K
Karsten Keil 已提交
371 372 373 374 375
mISDNInit(void)
{
	int	err;

	printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
376
	       MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
377
	mISDN_init_clock(&debug);
K
Karsten Keil 已提交
378
	mISDN_initstack(&debug);
379 380 381
	err = class_register(&mISDN_class);
	if (err)
		goto error1;
K
Karsten Keil 已提交
382 383
	err = mISDN_inittimer(&debug);
	if (err)
384
		goto error2;
K
Karsten Keil 已提交
385
	err = l1_init(&debug);
386 387
	if (err)
		goto error3;
K
Karsten Keil 已提交
388
	err = Isdnl2_Init(&debug);
389 390
	if (err)
		goto error4;
K
Karsten Keil 已提交
391
	err = misdn_sock_init(&debug);
392 393 394 395 396 397 398 399 400 401 402 403 404
	if (err)
		goto error5;
	return 0;

error5:
	Isdnl2_cleanup();
error4:
	l1_cleanup();
error3:
	mISDN_timer_cleanup();
error2:
	class_unregister(&mISDN_class);
error1:
K
Karsten Keil 已提交
405 406 407
	return err;
}

408
static void mISDN_cleanup(void)
K
Karsten Keil 已提交
409 410 411
{
	misdn_sock_cleanup();
	Isdnl2_cleanup();
412 413 414
	l1_cleanup();
	mISDN_timer_cleanup();
	class_unregister(&mISDN_class);
K
Karsten Keil 已提交
415 416 417 418 419 420

	printk(KERN_DEBUG "mISDNcore unloaded\n");
}

module_init(mISDNInit);
module_exit(mISDN_cleanup);