| 1 |
/* |
| 2 |
* Dynamic Span Interface for DAHDI |
| 3 |
* |
| 4 |
* Written by Mark Spencer <[email protected]> |
| 5 |
* |
| 6 |
* Copyright (C) 2001-2012, Digium, Inc. |
| 7 |
* |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* See http://www.asterisk.org for more information about |
| 14 |
* the Asterisk project. Please do not directly contact |
| 15 |
* any of the maintainers of this project for assistance; |
| 16 |
* the project provides a web site, mailing lists and IRC |
| 17 |
* channels for your use. |
| 18 |
* |
| 19 |
* This program is free software, distributed under the terms of |
| 20 |
* the GNU General Public License Version 2 as published by the |
| 21 |
* Free Software Foundation. See the LICENSE file included with |
| 22 |
* this program for more details. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <linux/kernel.h> |
| 26 |
#include <linux/errno.h> |
| 27 |
#include <linux/module.h> |
| 28 |
#include <linux/init.h> |
| 29 |
#include <linux/spinlock.h> |
| 30 |
#include <linux/slab.h> |
| 31 |
#include <linux/kmod.h> |
| 32 |
#include <linux/sched.h> |
| 33 |
#include <linux/interrupt.h> |
| 34 |
#include <linux/moduleparam.h> |
| 35 |
|
| 36 |
#include <dahdi/kernel.h> |
| 37 |
|
| 38 |
#ifndef DAHDI_SYNC_TICK |
| 39 |
#error "Dynamic support depends on DAHDI_SYNC_TICK being enabled." |
| 40 |
#endif |
| 41 |
|
| 42 |
/* |
| 43 |
* Tasklets provide better system interactive response at the cost of the |
| 44 |
* possibility of losing a frame of data at very infrequent intervals. If |
| 45 |
* you are more concerned with the performance of your machine, enable the |
| 46 |
* tasklets. If you are strict about absolutely no drops, then do not enable |
| 47 |
* tasklets. |
| 48 |
*/ |
| 49 |
|
| 50 |
#undef ENABLE_TASKLETS |
| 51 |
|
| 52 |
/* |
| 53 |
* Dynamic spans implemented using TDM over X with standard message |
| 54 |
* types. Message format is as follows: |
| 55 |
* |
| 56 |
* Byte #: Meaning |
| 57 |
* 0 Number of samples per channel |
| 58 |
* 1 Current flags on span |
| 59 |
* Bit 0: Yellow Alarm |
| 60 |
* Bit 1: Sig bits present |
| 61 |
* Bits 2-7: reserved for future use |
| 62 |
* 2-3 16-bit counter value for detecting drops, network byte order. |
| 63 |
* 4-5 Number of channels in the message, network byte order |
| 64 |
* 6... 16-bit words, containing sig bits for each |
| 65 |
* four channels, least significant 4 bits being |
| 66 |
* the least significant channel, network byte order. |
| 67 |
* the rest data for each channel, all samples per channel |
| 68 |
before moving to the next. |
| 69 |
*/ |
| 70 |
|
| 71 |
#define DAHDI_DYNAMIC_FLAG_YELLOW_ALARM (1 << 0) |
| 72 |
#define DAHDI_DYNAMIC_FLAG_SIGBITS_PRESENT (1 << 1) |
| 73 |
#define DAHDI_DYNAMIC_FLAG_LOOPBACK (1 << 2) |
| 74 |
|
| 75 |
#define ERR_NSAMP (1 << 16) |
| 76 |
#define ERR_NCHAN (1 << 17) |
| 77 |
#define ERR_LEN (1 << 18) |
| 78 |
|
| 79 |
static int dahdi_dynamic_init(void); |
| 80 |
static void dahdi_dynamic_cleanup(void); |
| 81 |
|
| 82 |
#ifdef ENABLE_TASKLETS |
| 83 |
static int taskletrun; |
| 84 |
static int taskletsched; |
| 85 |
static int taskletpending; |
| 86 |
static int taskletexec; |
| 87 |
static int txerrors; |
| 88 |
static struct tasklet_struct dahdi_dynamic_tlet; |
| 89 |
|
| 90 |
static void dahdi_dynamic_tasklet(unsigned long data); |
| 91 |
#else |
| 92 |
static struct tasklet_struct dahdi_dynamic_flush_tlet; |
| 93 |
#endif |
| 94 |
|
| 95 |
static DEFINE_MUTEX(dspan_mutex); |
| 96 |
static DEFINE_SPINLOCK(dspan_lock); |
| 97 |
static DEFINE_SPINLOCK(driver_lock); |
| 98 |
|
| 99 |
static LIST_HEAD(dspan_list); |
| 100 |
static LIST_HEAD(driver_list); |
| 101 |
|
| 102 |
static int debug = 0; |
| 103 |
|
| 104 |
static int hasmaster = 0; |
| 105 |
|
| 106 |
static void checkmaster(void) |
| 107 |
{ |
| 108 |
int newhasmaster=0; |
| 109 |
int best = 9999999; |
| 110 |
struct dahdi_dynamic *d, *master = NULL; |
| 111 |
|
| 112 |
rcu_read_lock(); |
| 113 |
|
| 114 |
list_for_each_entry_rcu(d, &dspan_list, list) { |
| 115 |
if (d->timing) { |
| 116 |
d->master = 0; |
| 117 |
if (!(d->span.alarms & DAHDI_ALARM_RED) && |
| 118 |
(d->timing < best)) { |
| 119 |
/* If not in alarm and they're |
| 120 |
a better timing source, use them */ |
| 121 |
master = d; |
| 122 |
best = d->timing; |
| 123 |
newhasmaster = 1; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
hasmaster = newhasmaster; |
| 129 |
/* Mark the new master if there is one */ |
| 130 |
if (master) |
| 131 |
master->master = 1; |
| 132 |
|
| 133 |
rcu_read_unlock(); |
| 134 |
|
| 135 |
if (master) |
| 136 |
printk(KERN_INFO "TDMoX: New master: %s\n", master->span.name); |
| 137 |
else |
| 138 |
printk(KERN_INFO "TDMoX: No master.\n"); |
| 139 |
} |
| 140 |
|
| 141 |
static void dahdi_dynamic_sendmessage(struct dahdi_dynamic *d) |
| 142 |
{ |
| 143 |
unsigned char *buf = d->msgbuf; |
| 144 |
unsigned short bits; |
| 145 |
int msglen = 0; |
| 146 |
int x; |
| 147 |
int offset; |
| 148 |
|
| 149 |
/* Byte 0: Number of samples per channel */ |
| 150 |
*buf = DAHDI_CHUNKSIZE; |
| 151 |
buf++; msglen++; |
| 152 |
|
| 153 |
/* Byte 1: Flags */ |
| 154 |
*buf = 0; |
| 155 |
if (d->span.alarms & DAHDI_ALARM_RED) |
| 156 |
*buf |= DAHDI_DYNAMIC_FLAG_YELLOW_ALARM; |
| 157 |
*buf |= DAHDI_DYNAMIC_FLAG_SIGBITS_PRESENT; |
| 158 |
buf++; msglen++; |
| 159 |
|
| 160 |
/* Bytes 2-3: Transmit counter */ |
| 161 |
*((unsigned short *)buf) = htons((unsigned short)d->txcnt); |
| 162 |
d->txcnt++; |
| 163 |
buf++; msglen++; |
| 164 |
buf++; msglen++; |
| 165 |
|
| 166 |
/* Bytes 4-5: Number of channels */ |
| 167 |
*((unsigned short *)buf) = htons((unsigned short)d->span.channels); |
| 168 |
buf++; msglen++; |
| 169 |
buf++; msglen++; |
| 170 |
bits = 0; |
| 171 |
offset = 0; |
| 172 |
for (x = 0; x < d->span.channels; x++) { |
| 173 |
offset = x % 4; |
| 174 |
bits |= (d->chans[x]->txsig & 0xf) << (offset << 2); |
| 175 |
if (offset == 3) { |
| 176 |
/* Write the bits when we have four channels */ |
| 177 |
*((unsigned short *)buf) = htons(bits); |
| 178 |
buf++; msglen++; |
| 179 |
buf++; msglen++; |
| 180 |
bits = 0; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (offset != 3) { |
| 185 |
/* Finish it off if it's not done already */ |
| 186 |
*((unsigned short *)buf) = htons(bits); |
| 187 |
buf++; msglen++; |
| 188 |
buf++; msglen++; |
| 189 |
} |
| 190 |
|
| 191 |
for (x = 0; x < d->span.channels; x++) { |
| 192 |
memcpy(buf, d->chans[x]->writechunk, DAHDI_CHUNKSIZE); |
| 193 |
buf += DAHDI_CHUNKSIZE; |
| 194 |
msglen += DAHDI_CHUNKSIZE; |
| 195 |
} |
| 196 |
|
| 197 |
d->driver->transmit(d, d->msgbuf, msglen); |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
static void __dahdi_dynamic_run(void) |
| 202 |
{ |
| 203 |
struct dahdi_dynamic *d; |
| 204 |
#ifdef ENABLE_TASKLETS |
| 205 |
struct dahdi_dynamic_driver *drv; |
| 206 |
#endif |
| 207 |
|
| 208 |
rcu_read_lock(); |
| 209 |
list_for_each_entry_rcu(d, &dspan_list, list) { |
| 210 |
dahdi_transmit(&d->span); |
| 211 |
/* Handle all transmissions now */ |
| 212 |
dahdi_dynamic_sendmessage(d); |
| 213 |
} |
| 214 |
|
| 215 |
#ifdef ENABLE_TASKLETS |
| 216 |
list_for_each_entry_rcu(drv, &driver_list, list) { |
| 217 |
/* Flush any traffic still pending in the driver */ |
| 218 |
if (drv->flush) { |
| 219 |
drv->flush(); |
| 220 |
} |
| 221 |
} |
| 222 |
#else |
| 223 |
/* If tasklets are not enabled, the above section will be called in |
| 224 |
* interrupt context and the flushing of each driver will be called in a |
| 225 |
* separate tasklet that only handles that. This is necessary since some |
| 226 |
* of the dynamic spans need to call functions that require interrupts |
| 227 |
* to be enabled but dahdi_transmit / ...sendmessage needs to be called |
| 228 |
* each time the masterspan is processed which happens with interrupts |
| 229 |
* disabled. |
| 230 |
* |
| 231 |
*/ |
| 232 |
tasklet_hi_schedule(&dahdi_dynamic_flush_tlet); |
| 233 |
#endif |
| 234 |
rcu_read_unlock(); |
| 235 |
} |
| 236 |
|
| 237 |
#ifdef ENABLE_TASKLETS |
| 238 |
static void dahdi_dynamic_run(void) |
| 239 |
{ |
| 240 |
if (likely(!taskletpending)) { |
| 241 |
taskletpending = 1; |
| 242 |
taskletsched++; |
| 243 |
tasklet_hi_schedule(&dahdi_dynamic_tlet); |
| 244 |
} else { |
| 245 |
txerrors++; |
| 246 |
} |
| 247 |
} |
| 248 |
#else |
| 249 |
#define dahdi_dynamic_run __dahdi_dynamic_run |
| 250 |
#endif |
| 251 |
|
| 252 |
static inline struct dahdi_dynamic *dynamic_from_span(struct dahdi_span *span) |
| 253 |
{ |
| 254 |
return container_of(span, struct dahdi_dynamic, span); |
| 255 |
} |
| 256 |
|
| 257 |
void dahdi_dynamic_receive(struct dahdi_span *span, unsigned char *msg, int msglen) |
| 258 |
{ |
| 259 |
struct dahdi_dynamic *dtd = dynamic_from_span(span); |
| 260 |
int newerr=0; |
| 261 |
int sflags; |
| 262 |
int xlen; |
| 263 |
int x, bits, sig; |
| 264 |
int nchans, master; |
| 265 |
int newalarm; |
| 266 |
unsigned short rxpos, rxcnt; |
| 267 |
|
| 268 |
rcu_read_lock(); |
| 269 |
|
| 270 |
if (unlikely(msglen < 6)) { |
| 271 |
rcu_read_unlock(); |
| 272 |
newerr = ERR_LEN; |
| 273 |
if (newerr != dtd->err) |
| 274 |
printk(KERN_NOTICE "Span %s: Insufficient samples for header (only %d)\n", span->name, msglen); |
| 275 |
dtd->err = newerr; |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
/* First, check the chunksize */ |
| 280 |
if (unlikely(*msg != DAHDI_CHUNKSIZE)) { |
| 281 |
rcu_read_unlock(); |
| 282 |
newerr = ERR_NSAMP | msg[0]; |
| 283 |
if (newerr != dtd->err) |
| 284 |
printk(KERN_NOTICE "Span %s: Expected %d samples, but receiving %d\n", span->name, DAHDI_CHUNKSIZE, msg[0]); |
| 285 |
dtd->err = newerr; |
| 286 |
return; |
| 287 |
} |
| 288 |
msg++; |
| 289 |
sflags = *msg; |
| 290 |
msg++; |
| 291 |
|
| 292 |
rxpos = ntohs(*((unsigned short *)msg)); |
| 293 |
msg++; |
| 294 |
msg++; |
| 295 |
|
| 296 |
nchans = ntohs(*((unsigned short *)msg)); |
| 297 |
if (unlikely(nchans != span->channels)) { |
| 298 |
rcu_read_unlock(); |
| 299 |
newerr = ERR_NCHAN | nchans; |
| 300 |
if (newerr != dtd->err) |
| 301 |
printk(KERN_NOTICE "Span %s: Expected %d channels, but receiving %d\n", span->name, span->channels, nchans); |
| 302 |
dtd->err = newerr; |
| 303 |
return; |
| 304 |
} |
| 305 |
msg++; |
| 306 |
msg++; |
| 307 |
|
| 308 |
/* Okay now we've accepted the header, lets check our message |
| 309 |
length... */ |
| 310 |
|
| 311 |
/* Start with header */ |
| 312 |
xlen = 6; |
| 313 |
/* Add samples of audio */ |
| 314 |
xlen += nchans * DAHDI_CHUNKSIZE; |
| 315 |
/* If RBS info is there, add that */ |
| 316 |
if (sflags & DAHDI_DYNAMIC_FLAG_SIGBITS_PRESENT) { |
| 317 |
/* Account for sigbits -- one short per 4 channels*/ |
| 318 |
xlen += ((nchans + 3) / 4) * 2; |
| 319 |
} |
| 320 |
|
| 321 |
if (unlikely(xlen != msglen)) { |
| 322 |
rcu_read_unlock(); |
| 323 |
newerr = ERR_LEN | xlen; |
| 324 |
if (newerr != dtd->err) |
| 325 |
printk(KERN_NOTICE "Span %s: Expected message size %d, but was %d instead\n", span->name, xlen, msglen); |
| 326 |
dtd->err = newerr; |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
bits = 0; |
| 331 |
|
| 332 |
/* Record sigbits if present */ |
| 333 |
if (sflags & DAHDI_DYNAMIC_FLAG_SIGBITS_PRESENT) { |
| 334 |
for (x=0;x<nchans;x++) { |
| 335 |
if (!(x%4)) { |
| 336 |
/* Get new bits */ |
| 337 |
bits = ntohs(*((unsigned short *)msg)); |
| 338 |
msg++; |
| 339 |
msg++; |
| 340 |
} |
| 341 |
|
| 342 |
/* Pick the right bits */ |
| 343 |
sig = (bits >> ((x % 4) << 2)) & 0xff; |
| 344 |
|
| 345 |
/* Update signalling if appropriate */ |
| 346 |
if (sig != span->chans[x]->rxsig) |
| 347 |
dahdi_rbsbits(span->chans[x], sig); |
| 348 |
|
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/* Record data for channels */ |
| 353 |
for (x=0;x<nchans;x++) { |
| 354 |
memcpy(span->chans[x]->readchunk, msg, DAHDI_CHUNKSIZE); |
| 355 |
msg += DAHDI_CHUNKSIZE; |
| 356 |
} |
| 357 |
|
| 358 |
master = dtd->master; |
| 359 |
|
| 360 |
rxcnt = dtd->rxcnt; |
| 361 |
dtd->rxcnt = rxpos+1; |
| 362 |
|
| 363 |
/* Keep track of last received packet */ |
| 364 |
dtd->rxjif = jiffies; |
| 365 |
|
| 366 |
rcu_read_unlock(); |
| 367 |
|
| 368 |
/* Check for Yellow alarm */ |
| 369 |
newalarm = span->alarms & ~(DAHDI_ALARM_YELLOW | DAHDI_ALARM_RED); |
| 370 |
if (sflags & DAHDI_DYNAMIC_FLAG_YELLOW_ALARM) |
| 371 |
newalarm |= DAHDI_ALARM_YELLOW; |
| 372 |
|
| 373 |
if (newalarm != span->alarms) { |
| 374 |
span->alarms = newalarm; |
| 375 |
dahdi_alarm_notify(span); |
| 376 |
checkmaster(); |
| 377 |
} |
| 378 |
|
| 379 |
/* note if we had a missing packet */ |
| 380 |
if (unlikely(rxpos != rxcnt)) |
| 381 |
printk(KERN_NOTICE "Span %s: Expected seq no %d, but received %d instead\n", span->name, rxcnt, rxpos); |
| 382 |
|
| 383 |
dahdi_ec_span(span); |
| 384 |
dahdi_receive(span); |
| 385 |
|
| 386 |
/* If this is our master span, then run everything */ |
| 387 |
if (master) |
| 388 |
dahdi_dynamic_run(); |
| 389 |
} |
| 390 |
EXPORT_SYMBOL(dahdi_dynamic_receive); |
| 391 |
|
| 392 |
/** |
| 393 |
* dahdi_dynamic_release() - Free the memory associated with the dahdi_dynamic. |
| 394 |
* @kref: Pointer to kref embedded in dahdi_dynamic structure. |
| 395 |
* |
| 396 |
*/ |
| 397 |
static void dahdi_dynamic_release(struct kref *kref) |
| 398 |
{ |
| 399 |
struct dahdi_dynamic *d = container_of(kref, struct dahdi_dynamic, |
| 400 |
kref); |
| 401 |
unsigned int x; |
| 402 |
|
| 403 |
WARN_ON(test_bit(DAHDI_FLAGBIT_REGISTERED, &d->span.flags)); |
| 404 |
|
| 405 |
kfree(d->msgbuf); |
| 406 |
|
| 407 |
for (x = 0; x < d->span.channels; x++) |
| 408 |
kfree(d->chans[x]); |
| 409 |
|
| 410 |
dahdi_free_device(d->ddev); |
| 411 |
kfree(d); |
| 412 |
} |
| 413 |
|
| 414 |
static inline int dynamic_put(struct dahdi_dynamic *d) |
| 415 |
{ |
| 416 |
return kref_put(&d->kref, dahdi_dynamic_release); |
| 417 |
} |
| 418 |
|
| 419 |
static inline void dynamic_get(struct dahdi_dynamic *d) |
| 420 |
{ |
| 421 |
kref_get(&d->kref); |
| 422 |
} |
| 423 |
|
| 424 |
static struct dahdi_dynamic *find_dynamic(struct dahdi_dynamic_span *dds) |
| 425 |
{ |
| 426 |
struct dahdi_dynamic *d = NULL, *found = NULL; |
| 427 |
|
| 428 |
rcu_read_lock(); |
| 429 |
list_for_each_entry_rcu(d, &dspan_list, list) { |
| 430 |
if (!strcmp(d->dname, dds->driver) && |
| 431 |
!strcmp(d->addr, dds->addr)) { |
| 432 |
dynamic_get(d); |
| 433 |
found = d; |
| 434 |
break; |
| 435 |
} |
| 436 |
} |
| 437 |
rcu_read_unlock(); |
| 438 |
|
| 439 |
return found; |
| 440 |
} |
| 441 |
|
| 442 |
static struct dahdi_dynamic_driver *find_driver(const char *name) |
| 443 |
{ |
| 444 |
struct dahdi_dynamic_driver *dtd, *found = NULL; |
| 445 |
|
| 446 |
rcu_read_lock(); |
| 447 |
list_for_each_entry_rcu(dtd, &driver_list, list) { |
| 448 |
/* here's our driver */ |
| 449 |
if (!strcmp(name, dtd->name)) { |
| 450 |
found = dtd; |
| 451 |
break; |
| 452 |
} |
| 453 |
} |
| 454 |
rcu_read_unlock(); |
| 455 |
|
| 456 |
return found; |
| 457 |
} |
| 458 |
|
| 459 |
static int _destroy_dynamic(struct dahdi_dynamic_span *dds) |
| 460 |
{ |
| 461 |
unsigned long flags; |
| 462 |
struct dahdi_dynamic *d; |
| 463 |
|
| 464 |
d = find_dynamic(dds); |
| 465 |
if (unlikely(!d)) |
| 466 |
return -EINVAL; |
| 467 |
|
| 468 |
/* We shouldn't have more than the two references at this point. If |
| 469 |
* we do, there are probably channels that are still opened. */ |
| 470 |
if (refcount_read(&d->kref.refcount) > 2) { |
| 471 |
dynamic_put(d); |
| 472 |
return -EBUSY; |
| 473 |
} |
| 474 |
|
| 475 |
if (d->pvt) { |
| 476 |
if (d->driver && d->driver->destroy) { |
| 477 |
if (!try_module_get(d->driver->owner)) { |
| 478 |
/* The driver for this device is in the |
| 479 |
* process of unloading. Leave this dynamic on |
| 480 |
* the list so it's cleaned up when the driver |
| 481 |
* unregisters. */ |
| 482 |
dynamic_put(d); |
| 483 |
return -ENXIO; |
| 484 |
} |
| 485 |
d->driver->destroy(d); |
| 486 |
module_put(d->driver->owner); |
| 487 |
} else { |
| 488 |
WARN_ON(1); |
| 489 |
} |
| 490 |
d->pvt = NULL; |
| 491 |
} |
| 492 |
|
| 493 |
dahdi_unregister_device(d->ddev); |
| 494 |
|
| 495 |
spin_lock_irqsave(&dspan_lock, flags); |
| 496 |
list_del_rcu(&d->list); |
| 497 |
spin_unlock_irqrestore(&dspan_lock, flags); |
| 498 |
synchronize_rcu(); |
| 499 |
|
| 500 |
/* One since we've removed the item from the list... */ |
| 501 |
dynamic_put(d); |
| 502 |
/* ...and one for find_dynamic. */ |
| 503 |
dynamic_put(d); |
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
static int destroy_dynamic(struct dahdi_dynamic_span *dds) |
| 508 |
{ |
| 509 |
int ret; |
| 510 |
mutex_lock(&dspan_mutex); |
| 511 |
ret = _destroy_dynamic(dds); |
| 512 |
mutex_unlock(&dspan_mutex); |
| 513 |
return ret; |
| 514 |
} |
| 515 |
|
| 516 |
static int dahdi_dynamic_rbsbits(struct dahdi_chan *chan, int bits) |
| 517 |
{ |
| 518 |
/* Don't have to do anything */ |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
static int dahdi_dynamic_open(struct dahdi_chan *chan) |
| 523 |
{ |
| 524 |
struct dahdi_dynamic *d = dynamic_from_span(chan->span); |
| 525 |
if (!try_module_get(d->driver->owner)) |
| 526 |
return -ENODEV; |
| 527 |
dynamic_get(d); |
| 528 |
return 0; |
| 529 |
} |
| 530 |
|
| 531 |
static int dahdi_dynamic_chanconfig(struct file *file, |
| 532 |
struct dahdi_chan *chan, int sigtype) |
| 533 |
{ |
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
static int dahdi_dynamic_close(struct dahdi_chan *chan) |
| 538 |
{ |
| 539 |
struct dahdi_dynamic *d = dynamic_from_span(chan->span); |
| 540 |
struct module *owner = d->driver->owner; |
| 541 |
dynamic_put(d); |
| 542 |
module_put(owner); |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
static void dahdi_dynamic_sync_tick(struct dahdi_span *span, int is_master) |
| 547 |
{ |
| 548 |
struct dahdi_dynamic *head; |
| 549 |
struct dahdi_dynamic *d = dynamic_from_span(span); |
| 550 |
|
| 551 |
if (hasmaster) |
| 552 |
return; |
| 553 |
|
| 554 |
rcu_read_lock(); |
| 555 |
head = list_entry(dspan_list.next, struct dahdi_dynamic, list); |
| 556 |
rcu_read_unlock(); |
| 557 |
|
| 558 |
if (d == head) |
| 559 |
dahdi_dynamic_run(); |
| 560 |
return; |
| 561 |
} |
| 562 |
|
| 563 |
static const struct dahdi_span_ops dynamic_ops = { |
| 564 |
.owner = THIS_MODULE, |
| 565 |
.rbsbits = dahdi_dynamic_rbsbits, |
| 566 |
.open = dahdi_dynamic_open, |
| 567 |
.close = dahdi_dynamic_close, |
| 568 |
.chanconfig = dahdi_dynamic_chanconfig, |
| 569 |
.sync_tick = dahdi_dynamic_sync_tick, |
| 570 |
}; |
| 571 |
|
| 572 |
static int _create_dynamic(struct dahdi_dynamic_span *dds) |
| 573 |
{ |
| 574 |
int res = 0; |
| 575 |
struct dahdi_dynamic *d; |
| 576 |
struct dahdi_dynamic_driver *dtd; |
| 577 |
unsigned long flags; |
| 578 |
int x; |
| 579 |
int bufsize; |
| 580 |
|
| 581 |
if (dds->numchans < 1) { |
| 582 |
printk(KERN_NOTICE "Can't be less than 1 channel (%d)!\n", |
| 583 |
dds->numchans); |
| 584 |
return -EINVAL; |
| 585 |
} |
| 586 |
if (dds->numchans >= ARRAY_SIZE(d->chans)) { |
| 587 |
printk(KERN_NOTICE "Can't create dynamic span with greater " |
| 588 |
"than %d channels. See dahdi_dynamic.c and increase " |
| 589 |
"DAHDI_DYNAMIC_MAX_CHANS\n", dds->numchans); |
| 590 |
return -EINVAL; |
| 591 |
} |
| 592 |
|
| 593 |
d = find_dynamic(dds); |
| 594 |
if (d) { |
| 595 |
dynamic_put(d); |
| 596 |
return -EEXIST; |
| 597 |
} |
| 598 |
|
| 599 |
d = kzalloc(sizeof(*d), GFP_KERNEL); |
| 600 |
if (!d) |
| 601 |
return -ENOMEM; |
| 602 |
kref_init(&d->kref); |
| 603 |
d->ddev = dahdi_create_device(); |
| 604 |
|
| 605 |
for (x = 0; x < dds->numchans; x++) { |
| 606 |
d->chans[x] = kzalloc(sizeof(*d->chans[x]), GFP_KERNEL); |
| 607 |
if (!d->chans[x]) { |
| 608 |
dynamic_put(d); |
| 609 |
return -ENOMEM; |
| 610 |
} |
| 611 |
d->span.channels++; |
| 612 |
} |
| 613 |
|
| 614 |
/* Allocate message buffer with sample space and header space */ |
| 615 |
bufsize = dds->numchans * DAHDI_CHUNKSIZE + dds->numchans / 4 + 48; |
| 616 |
|
| 617 |
d->msgbuf = kzalloc(bufsize, GFP_KERNEL); |
| 618 |
|
| 619 |
if (!d->msgbuf) { |
| 620 |
dynamic_put(d); |
| 621 |
return -ENOMEM; |
| 622 |
} |
| 623 |
|
| 624 |
/* Setup parameters properly assuming we're going to be okay. */ |
| 625 |
strlcpy(d->dname, dds->driver, sizeof(d->dname)); |
| 626 |
strlcpy(d->addr, dds->addr, sizeof(d->addr)); |
| 627 |
d->timing = dds->timing; |
| 628 |
snprintf(d->span.name, sizeof(d->span.name), "DYN/%s/%s", |
| 629 |
dds->driver, dds->addr); |
| 630 |
snprintf(d->span.desc, sizeof(d->span.desc), |
| 631 |
"Dynamic '%s' span at '%s'", dds->driver, dds->addr); |
| 632 |
d->span.deflaw = DAHDI_LAW_MULAW; |
| 633 |
d->span.flags |= DAHDI_FLAG_RBS; |
| 634 |
d->span.chans = d->chans; |
| 635 |
d->span.spantype = SPANTYPE_DIGITAL_DYNAMIC; |
| 636 |
d->span.linecompat = DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF | |
| 637 |
DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_CCS | |
| 638 |
DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CRC4 | DAHDI_CONFIG_NOTOPEN; |
| 639 |
d->span.ops = &dynamic_ops; |
| 640 |
for (x = 0; x < d->span.channels; x++) { |
| 641 |
sprintf(d->chans[x]->name, "DYN/%s/%s/%d", |
| 642 |
dds->driver, dds->addr, x+1); |
| 643 |
d->chans[x]->sigcap = DAHDI_SIG_EM | DAHDI_SIG_CLEAR | |
| 644 |
DAHDI_SIG_FXSLS | DAHDI_SIG_FXSKS | |
| 645 |
DAHDI_SIG_FXSGS | DAHDI_SIG_FXOLS | |
| 646 |
DAHDI_SIG_FXOKS | DAHDI_SIG_FXOGS | |
| 647 |
DAHDI_SIG_SF | DAHDI_SIG_DACS_RBS | |
| 648 |
DAHDI_SIG_CAS; |
| 649 |
d->chans[x]->chanpos = x + 1; |
| 650 |
d->chans[x]->pvt = d; |
| 651 |
} |
| 652 |
|
| 653 |
dtd = find_driver(dds->driver); |
| 654 |
if (!dtd) { |
| 655 |
request_module("dahdi_dynamic_%s", dds->driver); |
| 656 |
dtd = find_driver(dds->driver); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
if (!dtd) { |
| 661 |
printk(KERN_NOTICE "No such driver '%s' for dynamic span\n", |
| 662 |
dds->driver); |
| 663 |
dynamic_put(d); |
| 664 |
return -EINVAL; |
| 665 |
} |
| 666 |
|
| 667 |
if (!try_module_get(dtd->owner)) { |
| 668 |
dynamic_put(d); |
| 669 |
return -ENODEV; |
| 670 |
} |
| 671 |
|
| 672 |
/* Remember the driver. We also give our reference to the driver to |
| 673 |
* the dahdi_dyanmic here. Do not access dtd directly now. */ |
| 674 |
d->driver = dtd; |
| 675 |
|
| 676 |
/* Create the stuff */ |
| 677 |
res = dtd->create(d, d->addr); |
| 678 |
if (res) { |
| 679 |
printk(KERN_NOTICE "Driver '%s' (%s) rejected address '%s'\n", |
| 680 |
dtd->name, dtd->desc, d->addr); |
| 681 |
dynamic_put(d); |
| 682 |
module_put(dtd->owner); |
| 683 |
return res; |
| 684 |
} |
| 685 |
|
| 686 |
d->ddev->devicetype = d->span.name; |
| 687 |
d->ddev->hardware_id = d->span.name; |
| 688 |
dev_set_name(&d->ddev->dev, "dynamic:%s:%d", dds->driver, dtd->id++); |
| 689 |
list_add_tail(&d->span.device_node, &d->ddev->spans); |
| 690 |
/* Whee! We're created. Now register the span */ |
| 691 |
if (dahdi_register_device(d->ddev, d->dev)) { |
| 692 |
printk(KERN_NOTICE "Unable to register span '%s'\n", |
| 693 |
d->span.name); |
| 694 |
dynamic_put(d); |
| 695 |
module_put(dtd->owner); |
| 696 |
return -EINVAL; |
| 697 |
} |
| 698 |
|
| 699 |
x = d->span.spanno; |
| 700 |
|
| 701 |
/* Transfer our reference to the dspan_list. Do not touch d after |
| 702 |
* this point. It also must remain on the list while registered. */ |
| 703 |
spin_lock_irqsave(&dspan_lock, flags); |
| 704 |
list_add_rcu(&d->list, &dspan_list); |
| 705 |
spin_unlock_irqrestore(&dspan_lock, flags); |
| 706 |
|
| 707 |
checkmaster(); |
| 708 |
|
| 709 |
module_put(dtd->owner); |
| 710 |
return x; |
| 711 |
} |
| 712 |
|
| 713 |
static int create_dynamic(struct dahdi_dynamic_span *dds) |
| 714 |
{ |
| 715 |
int ret; |
| 716 |
mutex_lock(&dspan_mutex); |
| 717 |
ret = _create_dynamic(dds); |
| 718 |
mutex_unlock(&dspan_mutex); |
| 719 |
return ret; |
| 720 |
} |
| 721 |
|
| 722 |
#ifdef ENABLE_TASKLETS |
| 723 |
static void dahdi_dynamic_tasklet(unsigned long data) |
| 724 |
{ |
| 725 |
taskletrun++; |
| 726 |
if (taskletpending) { |
| 727 |
taskletexec++; |
| 728 |
__dahdi_dynamic_run(); |
| 729 |
} |
| 730 |
taskletpending = 0; |
| 731 |
} |
| 732 |
#else |
| 733 |
static void dahdi_dynamic_flush_tasklet(unsigned long data) |
| 734 |
{ |
| 735 |
struct dahdi_dynamic_driver *drv; |
| 736 |
|
| 737 |
rcu_read_lock(); |
| 738 |
list_for_each_entry_rcu(drv, &driver_list, list) { |
| 739 |
/* Flush any traffic still pending in the driver */ |
| 740 |
if (drv->flush) { |
| 741 |
drv->flush(); |
| 742 |
} |
| 743 |
} |
| 744 |
rcu_read_unlock(); |
| 745 |
} |
| 746 |
#endif |
| 747 |
|
| 748 |
static int dahdi_dynamic_ioctl(unsigned int cmd, unsigned long data) |
| 749 |
{ |
| 750 |
struct dahdi_dynamic_span dds; |
| 751 |
int res; |
| 752 |
switch(cmd) { |
| 753 |
case DAHDI_DYNAMIC_CREATE: |
| 754 |
if (copy_from_user(&dds, (__user const void *)data, |
| 755 |
sizeof(dds))) |
| 756 |
return -EFAULT; |
| 757 |
if (debug) |
| 758 |
printk(KERN_DEBUG "Dynamic Create\n"); |
| 759 |
res = create_dynamic(&dds); |
| 760 |
if (res < 0) |
| 761 |
return res; |
| 762 |
dds.spanno = res; |
| 763 |
/* Let them know the new span number */ |
| 764 |
if (copy_to_user((__user void *) data, &dds, sizeof(dds))) |
| 765 |
return -EFAULT; |
| 766 |
return 0; |
| 767 |
case DAHDI_DYNAMIC_DESTROY: |
| 768 |
if (copy_from_user(&dds, (__user const void *)data, |
| 769 |
sizeof(dds))) |
| 770 |
return -EFAULT; |
| 771 |
if (debug) |
| 772 |
printk(KERN_DEBUG "Dynamic Destroy\n"); |
| 773 |
return destroy_dynamic(&dds); |
| 774 |
} |
| 775 |
|
| 776 |
return -ENOTTY; |
| 777 |
} |
| 778 |
|
| 779 |
int dahdi_dynamic_register_driver(struct dahdi_dynamic_driver *dri) |
| 780 |
{ |
| 781 |
unsigned long flags; |
| 782 |
int res = 0; |
| 783 |
|
| 784 |
if (!dri->owner) |
| 785 |
return -EINVAL; |
| 786 |
|
| 787 |
if (find_driver(dri->name)) { |
| 788 |
res = -1; |
| 789 |
} else { |
| 790 |
spin_lock_irqsave(&driver_lock, flags); |
| 791 |
list_add_rcu(&dri->list, &driver_list); |
| 792 |
spin_unlock_irqrestore(&driver_lock, flags); |
| 793 |
} |
| 794 |
return res; |
| 795 |
} |
| 796 |
EXPORT_SYMBOL(dahdi_dynamic_register_driver); |
| 797 |
|
| 798 |
void dahdi_dynamic_unregister_driver(struct dahdi_dynamic_driver *dri) |
| 799 |
{ |
| 800 |
struct dahdi_dynamic *d, *n; |
| 801 |
unsigned long flags; |
| 802 |
|
| 803 |
mutex_lock(&dspan_mutex); |
| 804 |
|
| 805 |
list_for_each_entry_safe(d, n, &dspan_list, list) { |
| 806 |
if (d->driver == dri) { |
| 807 |
if (d->pvt) { |
| 808 |
if (d->driver && d->driver->destroy) |
| 809 |
d->driver->destroy(d); |
| 810 |
else |
| 811 |
WARN_ON(1); |
| 812 |
} |
| 813 |
dahdi_unregister_device(d->ddev); |
| 814 |
spin_lock_irqsave(&dspan_lock, flags); |
| 815 |
list_del_rcu(&d->list); |
| 816 |
spin_unlock_irqrestore(&dspan_lock, flags); |
| 817 |
synchronize_rcu(); |
| 818 |
d->driver = NULL; |
| 819 |
dynamic_put(d); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
spin_lock_irqsave(&driver_lock, flags); |
| 824 |
list_del_rcu(&dri->list); |
| 825 |
spin_unlock_irqrestore(&driver_lock, flags); |
| 826 |
synchronize_rcu(); |
| 827 |
|
| 828 |
mutex_unlock(&dspan_mutex); |
| 829 |
} |
| 830 |
EXPORT_SYMBOL(dahdi_dynamic_unregister_driver); |
| 831 |
|
| 832 |
static struct timer_list alarmcheck; |
| 833 |
|
| 834 |
static void check_for_red_alarm(unsigned long ignored) |
| 835 |
{ |
| 836 |
int newalarm; |
| 837 |
int alarmchanged = 0; |
| 838 |
struct dahdi_dynamic *d; |
| 839 |
|
| 840 |
rcu_read_lock(); |
| 841 |
list_for_each_entry_rcu(d, &dspan_list, list) { |
| 842 |
newalarm = d->span.alarms & ~DAHDI_ALARM_RED; |
| 843 |
/* If nothing received for a second, consider that RED ALARM */ |
| 844 |
if ((jiffies - d->rxjif) > 1 * HZ) { |
| 845 |
newalarm |= DAHDI_ALARM_RED; |
| 846 |
if (d->span.alarms != newalarm) { |
| 847 |
d->span.alarms = newalarm; |
| 848 |
dahdi_alarm_notify(&d->span); |
| 849 |
alarmchanged++; |
| 850 |
} |
| 851 |
} |
| 852 |
} |
| 853 |
rcu_read_unlock(); |
| 854 |
|
| 855 |
if (alarmchanged) |
| 856 |
checkmaster(); |
| 857 |
|
| 858 |
/* Do the next one */ |
| 859 |
mod_timer(&alarmcheck, jiffies + 1 * HZ); |
| 860 |
} |
| 861 |
|
| 862 |
static const struct dahdi_dynamic_ops dahdi_dynamic_ops = { |
| 863 |
.owner = THIS_MODULE, |
| 864 |
.ioctl = dahdi_dynamic_ioctl, |
| 865 |
}; |
| 866 |
|
| 867 |
static int dahdi_dynamic_init(void) |
| 868 |
{ |
| 869 |
/* Start process to check for RED ALARM */ |
| 870 |
init_timer(&alarmcheck); |
| 871 |
alarmcheck.expires = 0; |
| 872 |
alarmcheck.data = 0; |
| 873 |
alarmcheck.function = check_for_red_alarm; |
| 874 |
/* Check once per second */ |
| 875 |
mod_timer(&alarmcheck, jiffies + 1 * HZ); |
| 876 |
#ifdef ENABLE_TASKLETS |
| 877 |
tasklet_init(&dahdi_dynamic_tlet, dahdi_dynamic_tasklet, 0); |
| 878 |
#else |
| 879 |
tasklet_init(&dahdi_dynamic_flush_tlet, dahdi_dynamic_flush_tasklet, 0); |
| 880 |
#endif |
| 881 |
dahdi_set_dynamic_ops(&dahdi_dynamic_ops); |
| 882 |
|
| 883 |
printk(KERN_INFO "DAHDI Dynamic Span support LOADED\n"); |
| 884 |
return 0; |
| 885 |
} |
| 886 |
|
| 887 |
static void dahdi_dynamic_cleanup(void) |
| 888 |
{ |
| 889 |
dahdi_set_dynamic_ops(NULL); |
| 890 |
|
| 891 |
#ifdef ENABLE_TASKLETS |
| 892 |
if (taskletpending) { |
| 893 |
tasklet_disable(&dahdi_dynamic_tlet); |
| 894 |
tasklet_kill(&dahdi_dynamic_tlet); |
| 895 |
} |
| 896 |
#else |
| 897 |
tasklet_disable(&dahdi_dynamic_flush_tlet); |
| 898 |
tasklet_kill(&dahdi_dynamic_flush_tlet); |
| 899 |
#endif |
| 900 |
del_timer_sync(&alarmcheck); |
| 901 |
/* Must call again in case it was running before and rescheduled |
| 902 |
* itself. */ |
| 903 |
del_timer(&alarmcheck); |
| 904 |
printk(KERN_INFO "DAHDI Dynamic Span support unloaded\n"); |
| 905 |
} |
| 906 |
|
| 907 |
module_param(debug, int, 0600); |
| 908 |
|
| 909 |
MODULE_DESCRIPTION("DAHDI Dynamic Span Support"); |
| 910 |
MODULE_AUTHOR("Mark Spencer <[email protected]>"); |
| 911 |
MODULE_LICENSE("GPL v2"); |
| 912 |
|
| 913 |
module_init(dahdi_dynamic_init); |
| 914 |
module_exit(dahdi_dynamic_cleanup); |