linux/drivers/isdn/i4l/isdn_common.c
<<
>>
Prefs
   1/* $Id: isdn_common.c,v 1.1.2.3 2004/02/10 01:07:13 keil Exp $
   2 *
   3 * Linux ISDN subsystem, common used functions (linklevel).
   4 *
   5 * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
   6 * Copyright 1995,96    Thinking Objects Software GmbH Wuerzburg
   7 * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
   8 *
   9 * This software may be used and distributed according to the terms
  10 * of the GNU General Public License, incorporated herein by reference.
  11 *
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/poll.h>
  17#include <linux/slab.h>
  18#include <linux/vmalloc.h>
  19#include <linux/isdn.h>
  20#include <linux/mutex.h>
  21#include "isdn_common.h"
  22#include "isdn_tty.h"
  23#include "isdn_net.h"
  24#include "isdn_ppp.h"
  25#ifdef CONFIG_ISDN_AUDIO
  26#include "isdn_audio.h"
  27#endif
  28#ifdef CONFIG_ISDN_DIVERSION_MODULE
  29#define CONFIG_ISDN_DIVERSION
  30#endif
  31#ifdef CONFIG_ISDN_DIVERSION
  32#include <linux/isdn_divertif.h>
  33#endif /* CONFIG_ISDN_DIVERSION */
  34#include "isdn_v110.h"
  35
  36/* Debugflags */
  37#undef ISDN_DEBUG_STATCALLB
  38
  39MODULE_DESCRIPTION("ISDN4Linux: link layer");
  40MODULE_AUTHOR("Fritz Elfert");
  41MODULE_LICENSE("GPL");
  42
  43isdn_dev *dev;
  44
  45static DEFINE_MUTEX(isdn_mutex);
  46static char *isdn_revision = "$Revision: 1.1.2.3 $";
  47
  48extern char *isdn_net_revision;
  49#ifdef CONFIG_ISDN_PPP
  50extern char *isdn_ppp_revision;
  51#else
  52static char *isdn_ppp_revision = ": none $";
  53#endif
  54#ifdef CONFIG_ISDN_AUDIO
  55extern char *isdn_audio_revision;
  56#else
  57static char *isdn_audio_revision = ": none $";
  58#endif
  59extern char *isdn_v110_revision;
  60
  61#ifdef CONFIG_ISDN_DIVERSION
  62static isdn_divert_if *divert_if; /* = NULL */
  63#endif /* CONFIG_ISDN_DIVERSION */
  64
  65
  66static int isdn_writebuf_stub(int, int, const u_char __user *, int);
  67static void set_global_features(void);
  68static int isdn_wildmat(char *s, char *p);
  69static int isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding);
  70
  71static inline void
  72isdn_lock_driver(isdn_driver_t *drv)
  73{
  74        try_module_get(drv->interface->owner);
  75        drv->locks++;
  76}
  77
  78void
  79isdn_lock_drivers(void)
  80{
  81        int i;
  82
  83        for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
  84                if (!dev->drv[i])
  85                        continue;
  86                isdn_lock_driver(dev->drv[i]);
  87        }
  88}
  89
  90static inline void
  91isdn_unlock_driver(isdn_driver_t *drv)
  92{
  93        if (drv->locks > 0) {
  94                drv->locks--;
  95                module_put(drv->interface->owner);
  96        }
  97}
  98
  99void
 100isdn_unlock_drivers(void)
 101{
 102        int i;
 103
 104        for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
 105                if (!dev->drv[i])
 106                        continue;
 107                isdn_unlock_driver(dev->drv[i]);
 108        }
 109}
 110
 111#if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
 112void
 113isdn_dumppkt(char *s, u_char *p, int len, int dumplen)
 114{
 115        int dumpc;
 116
 117        printk(KERN_DEBUG "%s(%d) ", s, len);
 118        for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
 119                printk(" %02x", *p++);
 120        printk("\n");
 121}
 122#endif
 123
 124/*
 125 * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
 126 * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
 127 */
 128static int
 129isdn_star(char *s, char *p)
 130{
 131        while (isdn_wildmat(s, p)) {
 132                if (*++s == '\0')
 133                        return (2);
 134        }
 135        return (0);
 136}
 137
 138/*
 139 * Shell-type Pattern-matching for incoming caller-Ids
 140 * This function gets a string in s and checks, if it matches the pattern
 141 * given in p.
 142 *
 143 * Return:
 144 *   0 = match.
 145 *   1 = no match.
 146 *   2 = no match. Would eventually match, if s would be longer.
 147 *
 148 * Possible Patterns:
 149 *
 150 * '?'     matches one character
 151 * '*'     matches zero or more characters
 152 * [xyz]   matches the set of characters in brackets.
 153 * [^xyz]  matches any single character not in the set of characters
 154 */
 155
 156static int
 157isdn_wildmat(char *s, char *p)
 158{
 159        register int last;
 160        register int matched;
 161        register int reverse;
 162        register int nostar = 1;
 163
 164        if (!(*s) && !(*p))
 165                return (1);
 166        for (; *p; s++, p++)
 167                switch (*p) {
 168                case '\\':
 169                        /*
 170                         * Literal match with following character,
 171                         * fall through.
 172                         */
 173                        p++;
 174                default:
 175                        if (*s != *p)
 176                                return (*s == '\0') ? 2 : 1;
 177                                        continue;
 178                case '?':
 179                        /* Match anything. */
 180                        if (*s == '\0')
 181                                return (2);
 182                        continue;
 183                case '*':
 184                        nostar = 0;
 185                        /* Trailing star matches everything. */
 186                        return (*++p ? isdn_star(s, p) : 0);
 187                case '[':
 188                        /* [^....] means inverse character class. */
 189                        if ((reverse = (p[1] == '^')))
 190                                p++;
 191                        for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
 192                                /* This next line requires a good C compiler. */
 193                                if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
 194                                        matched = 1;
 195                        if (matched == reverse)
 196                                return (1);
 197                        continue;
 198                }
 199        return (*s == '\0') ? 0 : nostar;
 200}
 201
 202int isdn_msncmp(const char *msn1, const char *msn2)
 203{
 204        char TmpMsn1[ISDN_MSNLEN];
 205        char TmpMsn2[ISDN_MSNLEN];
 206        char *p;
 207
 208        for (p = TmpMsn1; *msn1 && *msn1 != ':';)  // Strip off a SPID
 209                *p++ = *msn1++;
 210        *p = '\0';
 211
 212        for (p = TmpMsn2; *msn2 && *msn2 != ':';)  // Strip off a SPID
 213                *p++ = *msn2++;
 214        *p = '\0';
 215
 216        return isdn_wildmat(TmpMsn1, TmpMsn2);
 217}
 218
 219int
 220isdn_dc2minor(int di, int ch)
 221{
 222        int i;
 223        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 224                if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
 225                        return i;
 226        return -1;
 227}
 228
 229static int isdn_timer_cnt1 = 0;
 230static int isdn_timer_cnt2 = 0;
 231static int isdn_timer_cnt3 = 0;
 232
 233static void
 234isdn_timer_funct(ulong dummy)
 235{
 236        int tf = dev->tflags;
 237        if (tf & ISDN_TIMER_FAST) {
 238                if (tf & ISDN_TIMER_MODEMREAD)
 239                        isdn_tty_readmodem();
 240                if (tf & ISDN_TIMER_MODEMPLUS)
 241                        isdn_tty_modem_escape();
 242                if (tf & ISDN_TIMER_MODEMXMIT)
 243                        isdn_tty_modem_xmit();
 244        }
 245        if (tf & ISDN_TIMER_SLOW) {
 246                if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
 247                        isdn_timer_cnt1 = 0;
 248                        if (tf & ISDN_TIMER_NETDIAL)
 249                                isdn_net_dial();
 250                }
 251                if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
 252                        isdn_timer_cnt2 = 0;
 253                        if (tf & ISDN_TIMER_NETHANGUP)
 254                                isdn_net_autohup();
 255                        if (++isdn_timer_cnt3 >= ISDN_TIMER_RINGING) {
 256                                isdn_timer_cnt3 = 0;
 257                                if (tf & ISDN_TIMER_MODEMRING)
 258                                        isdn_tty_modem_ring();
 259                        }
 260                        if (tf & ISDN_TIMER_CARRIER)
 261                                isdn_tty_carrier_timeout();
 262                }
 263        }
 264        if (tf)
 265                mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
 266}
 267
 268void
 269isdn_timer_ctrl(int tf, int onoff)
 270{
 271        unsigned long flags;
 272        int old_tflags;
 273
 274        spin_lock_irqsave(&dev->timerlock, flags);
 275        if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
 276                /* If the slow-timer wasn't activated until now */
 277                isdn_timer_cnt1 = 0;
 278                isdn_timer_cnt2 = 0;
 279        }
 280        old_tflags = dev->tflags;
 281        if (onoff)
 282                dev->tflags |= tf;
 283        else
 284                dev->tflags &= ~tf;
 285        if (dev->tflags && !old_tflags)
 286                mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
 287        spin_unlock_irqrestore(&dev->timerlock, flags);
 288}
 289
 290/*
 291 * Receive a packet from B-Channel. (Called from low-level-module)
 292 */
 293static void
 294isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
 295{
 296        int i;
 297
 298        if ((i = isdn_dc2minor(di, channel)) == -1) {
 299                dev_kfree_skb(skb);
 300                return;
 301        }
 302        /* Update statistics */
 303        dev->ibytes[i] += skb->len;
 304
 305        /* First, try to deliver data to network-device */
 306        if (isdn_net_rcv_skb(i, skb))
 307                return;
 308
 309        /* V.110 handling
 310         * makes sense for async streams only, so it is
 311         * called after possible net-device delivery.
 312         */
 313        if (dev->v110[i]) {
 314                atomic_inc(&dev->v110use[i]);
 315                skb = isdn_v110_decode(dev->v110[i], skb);
 316                atomic_dec(&dev->v110use[i]);
 317                if (!skb)
 318                        return;
 319        }
 320
 321        /* No network-device found, deliver to tty or raw-channel */
 322        if (skb->len) {
 323                if (isdn_tty_rcv_skb(i, di, channel, skb))
 324                        return;
 325                wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
 326        } else
 327                dev_kfree_skb(skb);
 328}
 329
 330/*
 331 * Intercept command from Linklevel to Lowlevel.
 332 * If layer 2 protocol is V.110 and this is not supported by current
 333 * lowlevel-driver, use driver's transparent mode and handle V.110 in
 334 * linklevel instead.
 335 */
 336int
 337isdn_command(isdn_ctrl *cmd)
 338{
 339        if (cmd->driver == -1) {
 340                printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
 341                return (1);
 342        }
 343        if (!dev->drv[cmd->driver]) {
 344                printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d] NULL\n",
 345                       cmd->command, cmd->driver);
 346                return (1);
 347        }
 348        if (!dev->drv[cmd->driver]->interface) {
 349                printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d]->interface NULL\n",
 350                       cmd->command, cmd->driver);
 351                return (1);
 352        }
 353        if (cmd->command == ISDN_CMD_SETL2) {
 354                int idx = isdn_dc2minor(cmd->driver, cmd->arg & 255);
 355                unsigned long l2prot = (cmd->arg >> 8) & 255;
 356                unsigned long features = (dev->drv[cmd->driver]->interface->features
 357                                          >> ISDN_FEATURE_L2_SHIFT) &
 358                        ISDN_FEATURE_L2_MASK;
 359                unsigned long l2_feature = (1 << l2prot);
 360
 361                switch (l2prot) {
 362                case ISDN_PROTO_L2_V11096:
 363                case ISDN_PROTO_L2_V11019:
 364                case ISDN_PROTO_L2_V11038:
 365                        /* If V.110 requested, but not supported by
 366                         * HL-driver, set emulator-flag and change
 367                         * Layer-2 to transparent
 368                         */
 369                        if (!(features & l2_feature)) {
 370                                dev->v110emu[idx] = l2prot;
 371                                cmd->arg = (cmd->arg & 255) |
 372                                        (ISDN_PROTO_L2_TRANS << 8);
 373                        } else
 374                                dev->v110emu[idx] = 0;
 375                }
 376        }
 377        return dev->drv[cmd->driver]->interface->command(cmd);
 378}
 379
 380void
 381isdn_all_eaz(int di, int ch)
 382{
 383        isdn_ctrl cmd;
 384
 385        if (di < 0)
 386                return;
 387        cmd.driver = di;
 388        cmd.arg = ch;
 389        cmd.command = ISDN_CMD_SETEAZ;
 390        cmd.parm.num[0] = '\0';
 391        isdn_command(&cmd);
 392}
 393
 394/*
 395 * Begin of a CAPI like LL<->HL interface, currently used only for
 396 * supplementary service (CAPI 2.0 part III)
 397 */
 398#include <linux/isdn/capicmd.h>
 399
 400static int
 401isdn_capi_rec_hl_msg(capi_msg *cm)
 402{
 403        switch (cm->Command) {
 404        case CAPI_FACILITY:
 405                /* in the moment only handled in tty */
 406                return (isdn_tty_capi_facility(cm));
 407        default:
 408                return (-1);
 409        }
 410}
 411
 412static int
 413isdn_status_callback(isdn_ctrl *c)
 414{
 415        int di;
 416        u_long flags;
 417        int i;
 418        int r;
 419        int retval = 0;
 420        isdn_ctrl cmd;
 421        isdn_net_dev *p;
 422
 423        di = c->driver;
 424        i = isdn_dc2minor(di, c->arg);
 425        switch (c->command) {
 426        case ISDN_STAT_BSENT:
 427                if (i < 0)
 428                        return -1;
 429                if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 430                        return 0;
 431                if (isdn_net_stat_callback(i, c))
 432                        return 0;
 433                if (isdn_v110_stat_callback(i, c))
 434                        return 0;
 435                if (isdn_tty_stat_callback(i, c))
 436                        return 0;
 437                wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
 438                break;
 439        case ISDN_STAT_STAVAIL:
 440                dev->drv[di]->stavail += c->arg;
 441                wake_up_interruptible(&dev->drv[di]->st_waitq);
 442                break;
 443        case ISDN_STAT_RUN:
 444                dev->drv[di]->flags |= DRV_FLAG_RUNNING;
 445                for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 446                        if (dev->drvmap[i] == di)
 447                                isdn_all_eaz(di, dev->chanmap[i]);
 448                set_global_features();
 449                break;
 450        case ISDN_STAT_STOP:
 451                dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
 452                break;
 453        case ISDN_STAT_ICALL:
 454                if (i < 0)
 455                        return -1;
 456#ifdef ISDN_DEBUG_STATCALLB
 457                printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
 458#endif
 459                if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
 460                        cmd.driver = di;
 461                        cmd.arg = c->arg;
 462                        cmd.command = ISDN_CMD_HANGUP;
 463                        isdn_command(&cmd);
 464                        return 0;
 465                }
 466                /* Try to find a network-interface which will accept incoming call */
 467                r = ((c->command == ISDN_STAT_ICALLW) ? 0 : isdn_net_find_icall(di, c->arg, i, &c->parm.setup));
 468                switch (r) {
 469                case 0:
 470                        /* No network-device replies.
 471                         * Try ttyI's.
 472                         * These return 0 on no match, 1 on match and
 473                         * 3 on eventually match, if CID is longer.
 474                         */
 475                        if (c->command == ISDN_STAT_ICALL)
 476                                if ((retval = isdn_tty_find_icall(di, c->arg, &c->parm.setup))) return (retval);
 477#ifdef CONFIG_ISDN_DIVERSION
 478                        if (divert_if)
 479                                if ((retval = divert_if->stat_callback(c)))
 480                                        return (retval); /* processed */
 481#endif /* CONFIG_ISDN_DIVERSION */
 482                        if ((!retval) && (dev->drv[di]->flags & DRV_FLAG_REJBUS)) {
 483                                /* No tty responding */
 484                                cmd.driver = di;
 485                                cmd.arg = c->arg;
 486                                cmd.command = ISDN_CMD_HANGUP;
 487                                isdn_command(&cmd);
 488                                retval = 2;
 489                        }
 490                        break;
 491                case 1:
 492                        /* Schedule connection-setup */
 493                        isdn_net_dial();
 494                        cmd.driver = di;
 495                        cmd.arg = c->arg;
 496                        cmd.command = ISDN_CMD_ACCEPTD;
 497                        for (p = dev->netdev; p; p = p->next)
 498                                if (p->local->isdn_channel == cmd.arg)
 499                                {
 500                                        strcpy(cmd.parm.setup.eazmsn, p->local->msn);
 501                                        isdn_command(&cmd);
 502                                        retval = 1;
 503                                        break;
 504                                }
 505                        break;
 506
 507                case 2: /* For calling back, first reject incoming call ... */
 508                case 3: /* Interface found, but down, reject call actively  */
 509                        retval = 2;
 510                        printk(KERN_INFO "isdn: Rejecting Call\n");
 511                        cmd.driver = di;
 512                        cmd.arg = c->arg;
 513                        cmd.command = ISDN_CMD_HANGUP;
 514                        isdn_command(&cmd);
 515                        if (r == 3)
 516                                break;
 517                        /* Fall through */
 518                case 4:
 519                        /* ... then start callback. */
 520                        isdn_net_dial();
 521                        break;
 522                case 5:
 523                        /* Number would eventually match, if longer */
 524                        retval = 3;
 525                        break;
 526                }
 527#ifdef ISDN_DEBUG_STATCALLB
 528                printk(KERN_DEBUG "ICALL: ret=%d\n", retval);
 529#endif
 530                return retval;
 531                break;
 532        case ISDN_STAT_CINF:
 533                if (i < 0)
 534                        return -1;
 535#ifdef ISDN_DEBUG_STATCALLB
 536                printk(KERN_DEBUG "CINF: %ld %s\n", c->arg, c->parm.num);
 537#endif
 538                if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 539                        return 0;
 540                if (strcmp(c->parm.num, "0"))
 541                        isdn_net_stat_callback(i, c);
 542                isdn_tty_stat_callback(i, c);
 543                break;
 544        case ISDN_STAT_CAUSE:
 545#ifdef ISDN_DEBUG_STATCALLB
 546                printk(KERN_DEBUG "CAUSE: %ld %s\n", c->arg, c->parm.num);
 547#endif
 548                printk(KERN_INFO "isdn: %s,ch%ld cause: %s\n",
 549                       dev->drvid[di], c->arg, c->parm.num);
 550                isdn_tty_stat_callback(i, c);
 551#ifdef CONFIG_ISDN_DIVERSION
 552                if (divert_if)
 553                        divert_if->stat_callback(c);
 554#endif /* CONFIG_ISDN_DIVERSION */
 555                break;
 556        case ISDN_STAT_DISPLAY:
 557#ifdef ISDN_DEBUG_STATCALLB
 558                printk(KERN_DEBUG "DISPLAY: %ld %s\n", c->arg, c->parm.display);
 559#endif
 560                isdn_tty_stat_callback(i, c);
 561#ifdef CONFIG_ISDN_DIVERSION
 562                if (divert_if)
 563                        divert_if->stat_callback(c);
 564#endif /* CONFIG_ISDN_DIVERSION */
 565                break;
 566        case ISDN_STAT_DCONN:
 567                if (i < 0)
 568                        return -1;
 569#ifdef ISDN_DEBUG_STATCALLB
 570                printk(KERN_DEBUG "DCONN: %ld\n", c->arg);
 571#endif
 572                if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 573                        return 0;
 574                /* Find any net-device, waiting for D-channel setup */
 575                if (isdn_net_stat_callback(i, c))
 576                        break;
 577                isdn_v110_stat_callback(i, c);
 578                /* Find any ttyI, waiting for D-channel setup */
 579                if (isdn_tty_stat_callback(i, c)) {
 580                        cmd.driver = di;
 581                        cmd.arg = c->arg;
 582                        cmd.command = ISDN_CMD_ACCEPTB;
 583                        isdn_command(&cmd);
 584                        break;
 585                }
 586                break;
 587        case ISDN_STAT_DHUP:
 588                if (i < 0)
 589                        return -1;
 590#ifdef ISDN_DEBUG_STATCALLB
 591                printk(KERN_DEBUG "DHUP: %ld\n", c->arg);
 592#endif
 593                if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 594                        return 0;
 595                dev->drv[di]->online &= ~(1 << (c->arg));
 596                isdn_info_update();
 597                /* Signal hangup to network-devices */
 598                if (isdn_net_stat_callback(i, c))
 599                        break;
 600                isdn_v110_stat_callback(i, c);
 601                if (isdn_tty_stat_callback(i, c))
 602                        break;
 603#ifdef CONFIG_ISDN_DIVERSION
 604                if (divert_if)
 605                        divert_if->stat_callback(c);
 606#endif /* CONFIG_ISDN_DIVERSION */
 607                break;
 608                break;
 609        case ISDN_STAT_BCONN:
 610                if (i < 0)
 611                        return -1;
 612#ifdef ISDN_DEBUG_STATCALLB
 613                printk(KERN_DEBUG "BCONN: %ld\n", c->arg);
 614#endif
 615                /* Signal B-channel-connect to network-devices */
 616                if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 617                        return 0;
 618                dev->drv[di]->online |= (1 << (c->arg));
 619                isdn_info_update();
 620                if (isdn_net_stat_callback(i, c))
 621                        break;
 622                isdn_v110_stat_callback(i, c);
 623                if (isdn_tty_stat_callback(i, c))
 624                        break;
 625                break;
 626        case ISDN_STAT_BHUP:
 627                if (i < 0)
 628                        return -1;
 629#ifdef ISDN_DEBUG_STATCALLB
 630                printk(KERN_DEBUG "BHUP: %ld\n", c->arg);
 631#endif
 632                if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 633                        return 0;
 634                dev->drv[di]->online &= ~(1 << (c->arg));
 635                isdn_info_update();
 636#ifdef CONFIG_ISDN_X25
 637                /* Signal hangup to network-devices */
 638                if (isdn_net_stat_callback(i, c))
 639                        break;
 640#endif
 641                isdn_v110_stat_callback(i, c);
 642                if (isdn_tty_stat_callback(i, c))
 643                        break;
 644                break;
 645        case ISDN_STAT_NODCH:
 646                if (i < 0)
 647                        return -1;
 648#ifdef ISDN_DEBUG_STATCALLB
 649                printk(KERN_DEBUG "NODCH: %ld\n", c->arg);
 650#endif
 651                if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 652                        return 0;
 653                if (isdn_net_stat_callback(i, c))
 654                        break;
 655                if (isdn_tty_stat_callback(i, c))
 656                        break;
 657                break;
 658        case ISDN_STAT_ADDCH:
 659                spin_lock_irqsave(&dev->lock, flags);
 660                if (isdn_add_channels(dev->drv[di], di, c->arg, 1)) {
 661                        spin_unlock_irqrestore(&dev->lock, flags);
 662                        return -1;
 663                }
 664                spin_unlock_irqrestore(&dev->lock, flags);
 665                isdn_info_update();
 666                break;
 667        case ISDN_STAT_DISCH:
 668                spin_lock_irqsave(&dev->lock, flags);
 669                for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 670                        if ((dev->drvmap[i] == di) &&
 671                            (dev->chanmap[i] == c->arg)) {
 672                                if (c->parm.num[0])
 673                                        dev->usage[i] &= ~ISDN_USAGE_DISABLED;
 674                                else
 675                                        if (USG_NONE(dev->usage[i])) {
 676                                                dev->usage[i] |= ISDN_USAGE_DISABLED;
 677                                        }
 678                                        else
 679                                                retval = -1;
 680                                break;
 681                        }
 682                spin_unlock_irqrestore(&dev->lock, flags);
 683                isdn_info_update();
 684                break;
 685        case ISDN_STAT_UNLOAD:
 686                while (dev->drv[di]->locks > 0) {
 687                        isdn_unlock_driver(dev->drv[di]);
 688                }
 689                spin_lock_irqsave(&dev->lock, flags);
 690                isdn_tty_stat_callback(i, c);
 691                for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 692                        if (dev->drvmap[i] == di) {
 693                                dev->drvmap[i] = -1;
 694                                dev->chanmap[i] = -1;
 695                                dev->usage[i] &= ~ISDN_USAGE_DISABLED;
 696                        }
 697                dev->drivers--;
 698                dev->channels -= dev->drv[di]->channels;
 699                kfree(dev->drv[di]->rcverr);
 700                kfree(dev->drv[di]->rcvcount);
 701                for (i = 0; i < dev->drv[di]->channels; i++)
 702                        skb_queue_purge(&dev->drv[di]->rpqueue[i]);
 703                kfree(dev->drv[di]->rpqueue);
 704                kfree(dev->drv[di]->rcv_waitq);
 705                kfree(dev->drv[di]);
 706                dev->drv[di] = NULL;
 707                dev->drvid[di][0] = '\0';
 708                isdn_info_update();
 709                set_global_features();
 710                spin_unlock_irqrestore(&dev->lock, flags);
 711                return 0;
 712        case ISDN_STAT_L1ERR:
 713                break;
 714        case CAPI_PUT_MESSAGE:
 715                return (isdn_capi_rec_hl_msg(&c->parm.cmsg));
 716#ifdef CONFIG_ISDN_TTY_FAX
 717        case ISDN_STAT_FAXIND:
 718                isdn_tty_stat_callback(i, c);
 719                break;
 720#endif
 721#ifdef CONFIG_ISDN_AUDIO
 722        case ISDN_STAT_AUDIO:
 723                isdn_tty_stat_callback(i, c);
 724                break;
 725#endif
 726#ifdef CONFIG_ISDN_DIVERSION
 727        case ISDN_STAT_PROT:
 728        case ISDN_STAT_REDIR:
 729                if (divert_if)
 730                        return (divert_if->stat_callback(c));
 731#endif /* CONFIG_ISDN_DIVERSION */
 732        default:
 733                return -1;
 734        }
 735        return 0;
 736}
 737
 738/*
 739 * Get integer from char-pointer, set pointer to end of number
 740 */
 741int
 742isdn_getnum(char **p)
 743{
 744        int v = -1;
 745
 746        while (*p[0] >= '0' && *p[0] <= '9')
 747                v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
 748        return v;
 749}
 750
 751#define DLE 0x10
 752
 753/*
 754 * isdn_readbchan() tries to get data from the read-queue.
 755 * It MUST be called with interrupts off.
 756 *
 757 * Be aware that this is not an atomic operation when sleep != 0, even though
 758 * interrupts are turned off! Well, like that we are currently only called
 759 * on behalf of a read system call on raw device files (which are documented
 760 * to be dangerous and for debugging purpose only). The inode semaphore
 761 * takes care that this is not called for the same minor device number while
 762 * we are sleeping, but access is not serialized against simultaneous read()
 763 * from the corresponding ttyI device. Can other ugly events, like changes
 764 * of the mapping (di,ch)<->minor, happen during the sleep? --he
 765 */
 766int
 767isdn_readbchan(int di, int channel, u_char *buf, u_char *fp, int len, wait_queue_head_t *sleep)
 768{
 769        int count;
 770        int count_pull;
 771        int count_put;
 772        int dflag;
 773        struct sk_buff *skb;
 774        u_char *cp;
 775
 776        if (!dev->drv[di])
 777                return 0;
 778        if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
 779                if (sleep)
 780                        interruptible_sleep_on(sleep);
 781                else
 782                        return 0;
 783        }
 784        if (len > dev->drv[di]->rcvcount[channel])
 785                len = dev->drv[di]->rcvcount[channel];
 786        cp = buf;
 787        count = 0;
 788        while (len) {
 789                if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
 790                        break;
 791#ifdef CONFIG_ISDN_AUDIO
 792                if (ISDN_AUDIO_SKB_LOCK(skb))
 793                        break;
 794                ISDN_AUDIO_SKB_LOCK(skb) = 1;
 795                if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
 796                        char *p = skb->data;
 797                        unsigned long DLEmask = (1 << channel);
 798
 799                        dflag = 0;
 800                        count_pull = count_put = 0;
 801                        while ((count_pull < skb->len) && (len > 0)) {
 802                                len--;
 803                                if (dev->drv[di]->DLEflag & DLEmask) {
 804                                        *cp++ = DLE;
 805                                        dev->drv[di]->DLEflag &= ~DLEmask;
 806                                } else {
 807                                        *cp++ = *p;
 808                                        if (*p == DLE) {
 809                                                dev->drv[di]->DLEflag |= DLEmask;
 810                                                (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
 811                                        }
 812                                        p++;
 813                                        count_pull++;
 814                                }
 815                                count_put++;
 816                        }
 817                        if (count_pull >= skb->len)
 818                                dflag = 1;
 819                } else {
 820#endif
 821                        /* No DLE's in buff, so simply copy it */
 822                        dflag = 1;
 823                        if ((count_pull = skb->len) > len) {
 824                                count_pull = len;
 825                                dflag = 0;
 826                        }
 827                        count_put = count_pull;
 828                        skb_copy_from_linear_data(skb, cp, count_put);
 829                        cp += count_put;
 830                        len -= count_put;
 831#ifdef CONFIG_ISDN_AUDIO
 832                }
 833#endif
 834                count += count_put;
 835                if (fp) {
 836                        memset(fp, 0, count_put);
 837                        fp += count_put;
 838                }
 839                if (dflag) {
 840                        /* We got all the data in this buff.
 841                         * Now we can dequeue it.
 842                         */
 843                        if (fp)
 844                                *(fp - 1) = 0xff;
 845#ifdef CONFIG_ISDN_AUDIO
 846                        ISDN_AUDIO_SKB_LOCK(skb) = 0;
 847#endif
 848                        skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
 849                        dev_kfree_skb(skb);
 850                } else {
 851                        /* Not yet emptied this buff, so it
 852                         * must stay in the queue, for further calls
 853                         * but we pull off the data we got until now.
 854                         */
 855                        skb_pull(skb, count_pull);
 856#ifdef CONFIG_ISDN_AUDIO
 857                        ISDN_AUDIO_SKB_LOCK(skb) = 0;
 858#endif
 859                }
 860                dev->drv[di]->rcvcount[channel] -= count_put;
 861        }
 862        return count;
 863}
 864
 865/*
 866 * isdn_readbchan_tty() tries to get data from the read-queue.
 867 * It MUST be called with interrupts off.
 868 *
 869 * Be aware that this is not an atomic operation when sleep != 0, even though
 870 * interrupts are turned off! Well, like that we are currently only called
 871 * on behalf of a read system call on raw device files (which are documented
 872 * to be dangerous and for debugging purpose only). The inode semaphore
 873 * takes care that this is not called for the same minor device number while
 874 * we are sleeping, but access is not serialized against simultaneous read()
 875 * from the corresponding ttyI device. Can other ugly events, like changes
 876 * of the mapping (di,ch)<->minor, happen during the sleep? --he
 877 */
 878int
 879isdn_readbchan_tty(int di, int channel, struct tty_struct *tty, int cisco_hack)
 880{
 881        int count;
 882        int count_pull;
 883        int count_put;
 884        int dflag;
 885        struct sk_buff *skb;
 886        char last = 0;
 887        int len;
 888
 889        if (!dev->drv[di])
 890                return 0;
 891        if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
 892                return 0;
 893
 894        len = tty_buffer_request_room(tty, dev->drv[di]->rcvcount[channel]);
 895        if (len == 0)
 896                return len;
 897
 898        count = 0;
 899        while (len) {
 900                if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
 901                        break;
 902#ifdef CONFIG_ISDN_AUDIO
 903                if (ISDN_AUDIO_SKB_LOCK(skb))
 904                        break;
 905                ISDN_AUDIO_SKB_LOCK(skb) = 1;
 906                if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
 907                        char *p = skb->data;
 908                        unsigned long DLEmask = (1 << channel);
 909
 910                        dflag = 0;
 911                        count_pull = count_put = 0;
 912                        while ((count_pull < skb->len) && (len > 0)) {
 913                                /* push every character but the last to the tty buffer directly */
 914                                if (count_put)
 915                                        tty_insert_flip_char(tty, last, TTY_NORMAL);
 916                                len--;
 917                                if (dev->drv[di]->DLEflag & DLEmask) {
 918                                        last = DLE;
 919                                        dev->drv[di]->DLEflag &= ~DLEmask;
 920                                } else {
 921                                        last = *p;
 922                                        if (last == DLE) {
 923                                                dev->drv[di]->DLEflag |= DLEmask;
 924                                                (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
 925                                        }
 926                                        p++;
 927                                        count_pull++;
 928                                }
 929                                count_put++;
 930                        }
 931                        if (count_pull >= skb->len)
 932                                dflag = 1;
 933                } else {
 934#endif
 935                        /* No DLE's in buff, so simply copy it */
 936                        dflag = 1;
 937                        if ((count_pull = skb->len) > len) {
 938                                count_pull = len;
 939                                dflag = 0;
 940                        }
 941                        count_put = count_pull;
 942                        if (count_put > 1)
 943                                tty_insert_flip_string(tty, skb->data, count_put - 1);
 944                        last = skb->data[count_put - 1];
 945                        len -= count_put;
 946#ifdef CONFIG_ISDN_AUDIO
 947                }
 948#endif
 949                count += count_put;
 950                if (dflag) {
 951                        /* We got all the data in this buff.
 952                         * Now we can dequeue it.
 953                         */
 954                        if (cisco_hack)
 955                                tty_insert_flip_char(tty, last, 0xFF);
 956                        else
 957                                tty_insert_flip_char(tty, last, TTY_NORMAL);
 958#ifdef CONFIG_ISDN_AUDIO
 959                        ISDN_AUDIO_SKB_LOCK(skb) = 0;
 960#endif
 961                        skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
 962                        dev_kfree_skb(skb);
 963                } else {
 964                        tty_insert_flip_char(tty, last, TTY_NORMAL);
 965                        /* Not yet emptied this buff, so it
 966                         * must stay in the queue, for further calls
 967                         * but we pull off the data we got until now.
 968                         */
 969                        skb_pull(skb, count_pull);
 970#ifdef CONFIG_ISDN_AUDIO
 971                        ISDN_AUDIO_SKB_LOCK(skb) = 0;
 972#endif
 973                }
 974                dev->drv[di]->rcvcount[channel] -= count_put;
 975        }
 976        return count;
 977}
 978
 979
 980static inline int
 981isdn_minor2drv(int minor)
 982{
 983        return (dev->drvmap[minor]);
 984}
 985
 986static inline int
 987isdn_minor2chan(int minor)
 988{
 989        return (dev->chanmap[minor]);
 990}
 991
 992static char *
 993isdn_statstr(void)
 994{
 995        static char istatbuf[2048];
 996        char *p;
 997        int i;
 998
 999        sprintf(istatbuf, "idmap:\t");
1000        p = istatbuf + strlen(istatbuf);
1001        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1002                sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
1003                p = istatbuf + strlen(istatbuf);
1004        }
1005        sprintf(p, "\nchmap:\t");
1006        p = istatbuf + strlen(istatbuf);
1007        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1008                sprintf(p, "%d ", dev->chanmap[i]);
1009                p = istatbuf + strlen(istatbuf);
1010        }
1011        sprintf(p, "\ndrmap:\t");
1012        p = istatbuf + strlen(istatbuf);
1013        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1014                sprintf(p, "%d ", dev->drvmap[i]);
1015                p = istatbuf + strlen(istatbuf);
1016        }
1017        sprintf(p, "\nusage:\t");
1018        p = istatbuf + strlen(istatbuf);
1019        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1020                sprintf(p, "%d ", dev->usage[i]);
1021                p = istatbuf + strlen(istatbuf);
1022        }
1023        sprintf(p, "\nflags:\t");
1024        p = istatbuf + strlen(istatbuf);
1025        for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
1026                if (dev->drv[i]) {
1027                        sprintf(p, "%ld ", dev->drv[i]->online);
1028                        p = istatbuf + strlen(istatbuf);
1029                } else {
1030                        sprintf(p, "? ");
1031                        p = istatbuf + strlen(istatbuf);
1032                }
1033        }
1034        sprintf(p, "\nphone:\t");
1035        p = istatbuf + strlen(istatbuf);
1036        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1037                sprintf(p, "%s ", dev->num[i]);
1038                p = istatbuf + strlen(istatbuf);
1039        }
1040        sprintf(p, "\n");
1041        return istatbuf;
1042}
1043
1044/* Module interface-code */
1045
1046void
1047isdn_info_update(void)
1048{
1049        infostruct *p = dev->infochain;
1050
1051        while (p) {
1052                *(p->private) = 1;
1053                p = (infostruct *) p->next;
1054        }
1055        wake_up_interruptible(&(dev->info_waitq));
1056}
1057
1058static ssize_t
1059isdn_read(struct file *file, char __user *buf, size_t count, loff_t *off)
1060{
1061        uint minor = iminor(file->f_path.dentry->d_inode);
1062        int len = 0;
1063        int drvidx;
1064        int chidx;
1065        int retval;
1066        char *p;
1067
1068        mutex_lock(&isdn_mutex);
1069        if (minor == ISDN_MINOR_STATUS) {
1070                if (!file->private_data) {
1071                        if (file->f_flags & O_NONBLOCK) {
1072                                retval = -EAGAIN;
1073                                goto out;
1074                        }
1075                        interruptible_sleep_on(&(dev->info_waitq));
1076                }
1077                p = isdn_statstr();
1078                file->private_data = NULL;
1079                if ((len = strlen(p)) <= count) {
1080                        if (copy_to_user(buf, p, len)) {
1081                                retval = -EFAULT;
1082                                goto out;
1083                        }
1084                        *off += len;
1085                        retval = len;
1086                        goto out;
1087                }
1088                retval = 0;
1089                goto out;
1090        }
1091        if (!dev->drivers) {
1092                retval = -ENODEV;
1093                goto out;
1094        }
1095        if (minor <= ISDN_MINOR_BMAX) {
1096                printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1097                drvidx = isdn_minor2drv(minor);
1098                if (drvidx < 0) {
1099                        retval = -ENODEV;
1100                        goto out;
1101                }
1102                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1103                        retval = -ENODEV;
1104                        goto out;
1105                }
1106                chidx = isdn_minor2chan(minor);
1107                if (!(p = kmalloc(count, GFP_KERNEL))) {
1108                        retval = -ENOMEM;
1109                        goto out;
1110                }
1111                len = isdn_readbchan(drvidx, chidx, p, NULL, count,
1112                                     &dev->drv[drvidx]->rcv_waitq[chidx]);
1113                *off += len;
1114                if (copy_to_user(buf, p, len))
1115                        len = -EFAULT;
1116                kfree(p);
1117                retval = len;
1118                goto out;
1119        }
1120        if (minor <= ISDN_MINOR_CTRLMAX) {
1121                drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1122                if (drvidx < 0) {
1123                        retval = -ENODEV;
1124                        goto out;
1125                }
1126                if (!dev->drv[drvidx]->stavail) {
1127                        if (file->f_flags & O_NONBLOCK) {
1128                                retval = -EAGAIN;
1129                                goto out;
1130                        }
1131                        interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
1132                }
1133                if (dev->drv[drvidx]->interface->readstat) {
1134                        if (count > dev->drv[drvidx]->stavail)
1135                                count = dev->drv[drvidx]->stavail;
1136                        len = dev->drv[drvidx]->interface->readstat(buf, count,
1137                                                                    drvidx, isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1138                        if (len < 0) {
1139                                retval = len;
1140                                goto out;
1141                        }
1142                } else {
1143                        len = 0;
1144                }
1145                if (len)
1146                        dev->drv[drvidx]->stavail -= len;
1147                else
1148                        dev->drv[drvidx]->stavail = 0;
1149                *off += len;
1150                retval = len;
1151                goto out;
1152        }
1153#ifdef CONFIG_ISDN_PPP
1154        if (minor <= ISDN_MINOR_PPPMAX) {
1155                retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1156                goto out;
1157        }
1158#endif
1159        retval = -ENODEV;
1160out:
1161        mutex_unlock(&isdn_mutex);
1162        return retval;
1163}
1164
1165static ssize_t
1166isdn_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
1167{
1168        uint minor = iminor(file->f_path.dentry->d_inode);
1169        int drvidx;
1170        int chidx;
1171        int retval;
1172
1173        if (minor == ISDN_MINOR_STATUS)
1174                return -EPERM;
1175        if (!dev->drivers)
1176                return -ENODEV;
1177
1178        mutex_lock(&isdn_mutex);
1179        if (minor <= ISDN_MINOR_BMAX) {
1180                printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1181                drvidx = isdn_minor2drv(minor);
1182                if (drvidx < 0) {
1183                        retval = -ENODEV;
1184                        goto out;
1185                }
1186                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1187                        retval = -ENODEV;
1188                        goto out;
1189                }
1190                chidx = isdn_minor2chan(minor);
1191                while ((retval = isdn_writebuf_stub(drvidx, chidx, buf, count)) == 0)
1192                        interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
1193                goto out;
1194        }
1195        if (minor <= ISDN_MINOR_CTRLMAX) {
1196                drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1197                if (drvidx < 0) {
1198                        retval = -ENODEV;
1199                        goto out;
1200                }
1201                /*
1202                 * We want to use the isdnctrl device to load the firmware
1203                 *
1204                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1205                 return -ENODEV;
1206                */
1207                if (dev->drv[drvidx]->interface->writecmd)
1208                        retval = dev->drv[drvidx]->interface->
1209                                writecmd(buf, count, drvidx,
1210                                         isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1211                else
1212                        retval = count;
1213                goto out;
1214        }
1215#ifdef CONFIG_ISDN_PPP
1216        if (minor <= ISDN_MINOR_PPPMAX) {
1217                retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1218                goto out;
1219        }
1220#endif
1221        retval = -ENODEV;
1222out:
1223        mutex_unlock(&isdn_mutex);
1224        return retval;
1225}
1226
1227static unsigned int
1228isdn_poll(struct file *file, poll_table *wait)
1229{
1230        unsigned int mask = 0;
1231        unsigned int minor = iminor(file->f_path.dentry->d_inode);
1232        int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1233
1234        mutex_lock(&isdn_mutex);
1235        if (minor == ISDN_MINOR_STATUS) {
1236                poll_wait(file, &(dev->info_waitq), wait);
1237                /* mask = POLLOUT | POLLWRNORM; */
1238                if (file->private_data) {
1239                        mask |= POLLIN | POLLRDNORM;
1240                }
1241                goto out;
1242        }
1243        if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1244                if (drvidx < 0) {
1245                        /* driver deregistered while file open */
1246                        mask = POLLHUP;
1247                        goto out;
1248                }
1249                poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1250                mask = POLLOUT | POLLWRNORM;
1251                if (dev->drv[drvidx]->stavail) {
1252                        mask |= POLLIN | POLLRDNORM;
1253                }
1254                goto out;
1255        }
1256#ifdef CONFIG_ISDN_PPP
1257        if (minor <= ISDN_MINOR_PPPMAX) {
1258                mask = isdn_ppp_poll(file, wait);
1259                goto out;
1260        }
1261#endif
1262        mask = POLLERR;
1263out:
1264        mutex_unlock(&isdn_mutex);
1265        return mask;
1266}
1267
1268
1269static int
1270isdn_ioctl(struct file *file, uint cmd, ulong arg)
1271{
1272        uint minor = iminor(file->f_path.dentry->d_inode);
1273        isdn_ctrl c;
1274        int drvidx;
1275        int ret;
1276        int i;
1277        char __user *p;
1278        char *s;
1279        union iocpar {
1280                char name[10];
1281                char bname[22];
1282                isdn_ioctl_struct iocts;
1283                isdn_net_ioctl_phone phone;
1284                isdn_net_ioctl_cfg cfg;
1285        } iocpar;
1286        void __user *argp = (void __user *)arg;
1287
1288#define name  iocpar.name
1289#define bname iocpar.bname
1290#define iocts iocpar.iocts
1291#define phone iocpar.phone
1292#define cfg   iocpar.cfg
1293
1294        if (minor == ISDN_MINOR_STATUS) {
1295                switch (cmd) {
1296                case IIOCGETDVR:
1297                        return (TTY_DV +
1298                                (NET_DV << 8) +
1299                                (INF_DV << 16));
1300                case IIOCGETCPS:
1301                        if (arg) {
1302                                ulong __user *p = argp;
1303                                int i;
1304                                if (!access_ok(VERIFY_WRITE, p,
1305                                               sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
1306                                        return -EFAULT;
1307                                for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1308                                        put_user(dev->ibytes[i], p++);
1309                                        put_user(dev->obytes[i], p++);
1310                                }
1311                                return 0;
1312                        } else
1313                                return -EINVAL;
1314                        break;
1315#ifdef CONFIG_NETDEVICES
1316                case IIOCNETGPN:
1317                        /* Get peer phone number of a connected
1318                         * isdn network interface */
1319                        if (arg) {
1320                                if (copy_from_user(&phone, argp, sizeof(phone)))
1321                                        return -EFAULT;
1322                                return isdn_net_getpeer(&phone, argp);
1323                        } else
1324                                return -EINVAL;
1325#endif
1326                default:
1327                        return -EINVAL;
1328                }
1329        }
1330        if (!dev->drivers)
1331                return -ENODEV;
1332        if (minor <= ISDN_MINOR_BMAX) {
1333                drvidx = isdn_minor2drv(minor);
1334                if (drvidx < 0)
1335                        return -ENODEV;
1336                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1337                        return -ENODEV;
1338                return 0;
1339        }
1340        if (minor <= ISDN_MINOR_CTRLMAX) {
1341/*
1342 * isdn net devices manage lots of configuration variables as linked lists.
1343 * Those lists must only be manipulated from user space. Some of the ioctl's
1344 * service routines access user space and are not atomic. Therefore, ioctl's
1345 * manipulating the lists and ioctl's sleeping while accessing the lists
1346 * are serialized by means of a semaphore.
1347 */
1348                switch (cmd) {
1349                case IIOCNETDWRSET:
1350                        printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1351                        return (-EINVAL);
1352                case IIOCNETLCR:
1353                        printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1354                        return -ENODEV;
1355#ifdef CONFIG_NETDEVICES
1356                case IIOCNETAIF:
1357                        /* Add a network-interface */
1358                        if (arg) {
1359                                if (copy_from_user(name, argp, sizeof(name)))
1360                                        return -EFAULT;
1361                                s = name;
1362                        } else {
1363                                s = NULL;
1364                        }
1365                        ret = mutex_lock_interruptible(&dev->mtx);
1366                        if (ret) return ret;
1367                        if ((s = isdn_net_new(s, NULL))) {
1368                                if (copy_to_user(argp, s, strlen(s) + 1)) {
1369                                        ret = -EFAULT;
1370                                } else {
1371                                        ret = 0;
1372                                }
1373                        } else
1374                                ret = -ENODEV;
1375                        mutex_unlock(&dev->mtx);
1376                        return ret;
1377                case IIOCNETASL:
1378                        /* Add a slave to a network-interface */
1379                        if (arg) {
1380                                if (copy_from_user(bname, argp, sizeof(bname) - 1))
1381                                        return -EFAULT;
1382                        } else
1383                                return -EINVAL;
1384                        ret = mutex_lock_interruptible(&dev->mtx);
1385                        if (ret) return ret;
1386                        if ((s = isdn_net_newslave(bname))) {
1387                                if (copy_to_user(argp, s, strlen(s) + 1)) {
1388                                        ret = -EFAULT;
1389                                } else {
1390                                        ret = 0;
1391                                }
1392                        } else
1393                                ret = -ENODEV;
1394                        mutex_unlock(&dev->mtx);
1395                        return ret;
1396                case IIOCNETDIF:
1397                        /* Delete a network-interface */
1398                        if (arg) {
1399                                if (copy_from_user(name, argp, sizeof(name)))
1400                                        return -EFAULT;
1401                                ret = mutex_lock_interruptible(&dev->mtx);
1402                                if (ret) return ret;
1403                                ret = isdn_net_rm(name);
1404                                mutex_unlock(&dev->mtx);
1405                                return ret;
1406                        } else
1407                                return -EINVAL;
1408                case IIOCNETSCF:
1409                        /* Set configurable parameters of a network-interface */
1410                        if (arg) {
1411                                if (copy_from_user(&cfg, argp, sizeof(cfg)))
1412                                        return -EFAULT;
1413                                return isdn_net_setcfg(&cfg);
1414                        } else
1415                                return -EINVAL;
1416                case IIOCNETGCF:
1417                        /* Get configurable parameters of a network-interface */
1418                        if (arg) {
1419                                if (copy_from_user(&cfg, argp, sizeof(cfg)))
1420                                        return -EFAULT;
1421                                if (!(ret = isdn_net_getcfg(&cfg))) {
1422                                        if (copy_to_user(argp, &cfg, sizeof(cfg)))
1423                                                return -EFAULT;
1424                                }
1425                                return ret;
1426                        } else
1427                                return -EINVAL;
1428                case IIOCNETANM:
1429                        /* Add a phone-number to a network-interface */
1430                        if (arg) {
1431                                if (copy_from_user(&phone, argp, sizeof(phone)))
1432                                        return -EFAULT;
1433                                ret = mutex_lock_interruptible(&dev->mtx);
1434                                if (ret) return ret;
1435                                ret = isdn_net_addphone(&phone);
1436                                mutex_unlock(&dev->mtx);
1437                                return ret;
1438                        } else
1439                                return -EINVAL;
1440                case IIOCNETGNM:
1441                        /* Get list of phone-numbers of a network-interface */
1442                        if (arg) {
1443                                if (copy_from_user(&phone, argp, sizeof(phone)))
1444                                        return -EFAULT;
1445                                ret = mutex_lock_interruptible(&dev->mtx);
1446                                if (ret) return ret;
1447                                ret = isdn_net_getphones(&phone, argp);
1448                                mutex_unlock(&dev->mtx);
1449                                return ret;
1450                        } else
1451                                return -EINVAL;
1452                case IIOCNETDNM:
1453                        /* Delete a phone-number of a network-interface */
1454                        if (arg) {
1455                                if (copy_from_user(&phone, argp, sizeof(phone)))
1456                                        return -EFAULT;
1457                                ret = mutex_lock_interruptible(&dev->mtx);
1458                                if (ret) return ret;
1459                                ret = isdn_net_delphone(&phone);
1460                                mutex_unlock(&dev->mtx);
1461                                return ret;
1462                        } else
1463                                return -EINVAL;
1464                case IIOCNETDIL:
1465                        /* Force dialing of a network-interface */
1466                        if (arg) {
1467                                if (copy_from_user(name, argp, sizeof(name)))
1468                                        return -EFAULT;
1469                                return isdn_net_force_dial(name);
1470                        } else
1471                                return -EINVAL;
1472#ifdef CONFIG_ISDN_PPP
1473                case IIOCNETALN:
1474                        if (!arg)
1475                                return -EINVAL;
1476                        if (copy_from_user(name, argp, sizeof(name)))
1477                                return -EFAULT;
1478                        return isdn_ppp_dial_slave(name);
1479                case IIOCNETDLN:
1480                        if (!arg)
1481                                return -EINVAL;
1482                        if (copy_from_user(name, argp, sizeof(name)))
1483                                return -EFAULT;
1484                        return isdn_ppp_hangup_slave(name);
1485#endif
1486                case IIOCNETHUP:
1487                        /* Force hangup of a network-interface */
1488                        if (!arg)
1489                                return -EINVAL;
1490                        if (copy_from_user(name, argp, sizeof(name)))
1491                                return -EFAULT;
1492                        return isdn_net_force_hangup(name);
1493                        break;
1494#endif                          /* CONFIG_NETDEVICES */
1495                case IIOCSETVER:
1496                        dev->net_verbose = arg;
1497                        printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1498                        return 0;
1499                case IIOCSETGST:
1500                        if (arg)
1501                                dev->global_flags |= ISDN_GLOBAL_STOPPED;
1502                        else
1503                                dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1504                        printk(KERN_INFO "isdn: Global Mode %s\n",
1505                               (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1506                        return 0;
1507                case IIOCSETBRJ:
1508                        drvidx = -1;
1509                        if (arg) {
1510                                int i;
1511                                char *p;
1512                                if (copy_from_user(&iocts, argp,
1513                                                   sizeof(isdn_ioctl_struct)))
1514                                        return -EFAULT;
1515                                iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1516                                if (strlen(iocts.drvid)) {
1517                                        if ((p = strchr(iocts.drvid, ',')))
1518                                                *p = 0;
1519                                        drvidx = -1;
1520                                        for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1521                                                if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1522                                                        drvidx = i;
1523                                                        break;
1524                                                }
1525                                }
1526                        }
1527                        if (drvidx == -1)
1528                                return -ENODEV;
1529                        if (iocts.arg)
1530                                dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1531                        else
1532                                dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1533                        return 0;
1534                case IIOCSIGPRF:
1535                        dev->profd = current;
1536                        return 0;
1537                        break;
1538                case IIOCGETPRF:
1539                        /* Get all Modem-Profiles */
1540                        if (arg) {
1541                                char __user *p = argp;
1542                                int i;
1543
1544                                if (!access_ok(VERIFY_WRITE, argp,
1545                                               (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1546                                               * ISDN_MAX_CHANNELS))
1547                                        return -EFAULT;
1548
1549                                for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1550                                        if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1551                                                         ISDN_MODEM_NUMREG))
1552                                                return -EFAULT;
1553                                        p += ISDN_MODEM_NUMREG;
1554                                        if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1555                                                return -EFAULT;
1556                                        p += ISDN_MSNLEN;
1557                                        if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1558                                                return -EFAULT;
1559                                        p += ISDN_LMSNLEN;
1560                                }
1561                                return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1562                        } else
1563                                return -EINVAL;
1564                        break;
1565                case IIOCSETPRF:
1566                        /* Set all Modem-Profiles */
1567                        if (arg) {
1568                                char __user *p = argp;
1569                                int i;
1570
1571                                if (!access_ok(VERIFY_READ, argp,
1572                                               (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1573                                               * ISDN_MAX_CHANNELS))
1574                                        return -EFAULT;
1575
1576                                for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1577                                        if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1578                                                           ISDN_MODEM_NUMREG))
1579                                                return -EFAULT;
1580                                        p += ISDN_MODEM_NUMREG;
1581                                        if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
1582                                                return -EFAULT;
1583                                        p += ISDN_LMSNLEN;
1584                                        if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1585                                                return -EFAULT;
1586                                        p += ISDN_MSNLEN;
1587                                }
1588                                return 0;
1589                        } else
1590                                return -EINVAL;
1591                        break;
1592                case IIOCSETMAP:
1593                case IIOCGETMAP:
1594                        /* Set/Get MSN->EAZ-Mapping for a driver */
1595                        if (arg) {
1596
1597                                if (copy_from_user(&iocts, argp,
1598                                                   sizeof(isdn_ioctl_struct)))
1599                                        return -EFAULT;
1600                                iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1601                                if (strlen(iocts.drvid)) {
1602                                        drvidx = -1;
1603                                        for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1604                                                if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1605                                                        drvidx = i;
1606                                                        break;
1607                                                }
1608                                } else
1609                                        drvidx = 0;
1610                                if (drvidx == -1)
1611                                        return -ENODEV;
1612                                if (cmd == IIOCSETMAP) {
1613                                        int loop = 1;
1614
1615                                        p = (char __user *) iocts.arg;
1616                                        i = 0;
1617                                        while (loop) {
1618                                                int j = 0;
1619
1620                                                while (1) {
1621                                                        if (!access_ok(VERIFY_READ, p, 1))
1622                                                                return -EFAULT;
1623                                                        get_user(bname[j], p++);
1624                                                        switch (bname[j]) {
1625                                                        case '\0':
1626                                                                loop = 0;
1627                                                                /* Fall through */
1628                                                        case ',':
1629                                                                bname[j] = '\0';
1630                                                                strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1631                                                                j = ISDN_MSNLEN;
1632                                                                break;
1633                                                        default:
1634                                                                j++;
1635                                                        }
1636                                                        if (j >= ISDN_MSNLEN)
1637                                                                break;
1638                                                }
1639                                                if (++i > 9)
1640                                                        break;
1641                                        }
1642                                } else {
1643                                        p = (char __user *) iocts.arg;
1644                                        for (i = 0; i < 10; i++) {
1645                                                snprintf(bname, sizeof(bname), "%s%s",
1646                                                         strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1647                                                         dev->drv[drvidx]->msn2eaz[i] : "_",
1648                                                         (i < 9) ? "," : "\0");
1649                                                if (copy_to_user(p, bname, strlen(bname) + 1))
1650                                                        return -EFAULT;
1651                                                p += strlen(bname);
1652                                        }
1653                                }
1654                                return 0;
1655                        } else
1656                                return -EINVAL;
1657                case IIOCDBGVAR:
1658                        if (arg) {
1659                                if (copy_to_user(argp, &dev, sizeof(ulong)))
1660                                        return -EFAULT;
1661                                return 0;
1662                        } else
1663                                return -EINVAL;
1664                        break;
1665                default:
1666                        if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1667                                cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1668                        else
1669                                return -EINVAL;
1670                        if (arg) {
1671                                int i;
1672                                char *p;
1673                                if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1674                                        return -EFAULT;
1675                                iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1676                                if (strlen(iocts.drvid)) {
1677                                        if ((p = strchr(iocts.drvid, ',')))
1678                                                *p = 0;
1679                                        drvidx = -1;
1680                                        for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1681                                                if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1682                                                        drvidx = i;
1683                                                        break;
1684                                                }
1685                                } else
1686                                        drvidx = 0;
1687                                if (drvidx == -1)
1688                                        return -ENODEV;
1689                                if (!access_ok(VERIFY_WRITE, argp,
1690                                               sizeof(isdn_ioctl_struct)))
1691                                        return -EFAULT;
1692                                c.driver = drvidx;
1693                                c.command = ISDN_CMD_IOCTL;
1694                                c.arg = cmd;
1695                                memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1696                                ret = isdn_command(&c);
1697                                memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1698                                if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1699                                        return -EFAULT;
1700                                return ret;
1701                        } else
1702                                return -EINVAL;
1703                }
1704        }
1705#ifdef CONFIG_ISDN_PPP
1706        if (minor <= ISDN_MINOR_PPPMAX)
1707                return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1708#endif
1709        return -ENODEV;
1710
1711#undef name
1712#undef bname
1713#undef iocts
1714#undef phone
1715#undef cfg
1716}
1717
1718static long
1719isdn_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1720{
1721        int ret;
1722
1723        mutex_lock(&isdn_mutex);
1724        ret = isdn_ioctl(file, cmd, arg);
1725        mutex_unlock(&isdn_mutex);
1726
1727        return ret;
1728}
1729
1730/*
1731 * Open the device code.
1732 */
1733static int
1734isdn_open(struct inode *ino, struct file *filep)
1735{
1736        uint minor = iminor(ino);
1737        int drvidx;
1738        int chidx;
1739        int retval = -ENODEV;
1740
1741        mutex_lock(&isdn_mutex);
1742        if (minor == ISDN_MINOR_STATUS) {
1743                infostruct *p;
1744
1745                if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1746                        p->next = (char *) dev->infochain;
1747                        p->private = (char *) &(filep->private_data);
1748                        dev->infochain = p;
1749                        /* At opening we allow a single update */
1750                        filep->private_data = (char *) 1;
1751                        retval = 0;
1752                        goto out;
1753                } else {
1754                        retval = -ENOMEM;
1755                        goto out;
1756                }
1757        }
1758        if (!dev->channels)
1759                goto out;
1760        if (minor <= ISDN_MINOR_BMAX) {
1761                printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1762                drvidx = isdn_minor2drv(minor);
1763                if (drvidx < 0)
1764                        goto out;
1765                chidx = isdn_minor2chan(minor);
1766                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1767                        goto out;
1768                if (!(dev->drv[drvidx]->online & (1 << chidx)))
1769                        goto out;
1770                isdn_lock_drivers();
1771                retval = 0;
1772                goto out;
1773        }
1774        if (minor <= ISDN_MINOR_CTRLMAX) {
1775                drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1776                if (drvidx < 0)
1777                        goto out;
1778                isdn_lock_drivers();
1779                retval = 0;
1780                goto out;
1781        }
1782#ifdef CONFIG_ISDN_PPP
1783        if (minor <= ISDN_MINOR_PPPMAX) {
1784                retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1785                if (retval == 0)
1786                        isdn_lock_drivers();
1787                goto out;
1788        }
1789#endif
1790out:
1791        nonseekable_open(ino, filep);
1792        mutex_unlock(&isdn_mutex);
1793        return retval;
1794}
1795
1796static int
1797isdn_close(struct inode *ino, struct file *filep)
1798{
1799        uint minor = iminor(ino);
1800
1801        mutex_lock(&isdn_mutex);
1802        if (minor == ISDN_MINOR_STATUS) {
1803                infostruct *p = dev->infochain;
1804                infostruct *q = NULL;
1805
1806                while (p) {
1807                        if (p->private == (char *) &(filep->private_data)) {
1808                                if (q)
1809                                        q->next = p->next;
1810                                else
1811                                        dev->infochain = (infostruct *) (p->next);
1812                                kfree(p);
1813                                goto out;
1814                        }
1815                        q = p;
1816                        p = (infostruct *) (p->next);
1817                }
1818                printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1819                goto out;
1820        }
1821        isdn_unlock_drivers();
1822        if (minor <= ISDN_MINOR_BMAX)
1823                goto out;
1824        if (minor <= ISDN_MINOR_CTRLMAX) {
1825                if (dev->profd == current)
1826                        dev->profd = NULL;
1827                goto out;
1828        }
1829#ifdef CONFIG_ISDN_PPP
1830        if (minor <= ISDN_MINOR_PPPMAX)
1831                isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1832#endif
1833
1834out:
1835        mutex_unlock(&isdn_mutex);
1836        return 0;
1837}
1838
1839static const struct file_operations isdn_fops =
1840{
1841        .owner          = THIS_MODULE,
1842        .llseek         = no_llseek,
1843        .read           = isdn_read,
1844        .write          = isdn_write,
1845        .poll           = isdn_poll,
1846        .unlocked_ioctl = isdn_unlocked_ioctl,
1847        .open           = isdn_open,
1848        .release        = isdn_close,
1849};
1850
1851char *
1852isdn_map_eaz2msn(char *msn, int di)
1853{
1854        isdn_driver_t *this = dev->drv[di];
1855        int i;
1856
1857        if (strlen(msn) == 1) {
1858                i = msn[0] - '0';
1859                if ((i >= 0) && (i <= 9))
1860                        if (strlen(this->msn2eaz[i]))
1861                                return (this->msn2eaz[i]);
1862        }
1863        return (msn);
1864}
1865
1866/*
1867 * Find an unused ISDN-channel, whose feature-flags match the
1868 * given L2- and L3-protocols.
1869 */
1870#define L2V (~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038))
1871
1872/*
1873 * This function must be called with holding the dev->lock.
1874 */
1875int
1876isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1877                      , int pre_chan, char *msn)
1878{
1879        int i;
1880        ulong features;
1881        ulong vfeatures;
1882
1883        features = ((1 << l2_proto) | (0x10000 << l3_proto));
1884        vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1885                     ~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038));
1886        /* If Layer-2 protocol is V.110, accept drivers with
1887         * transparent feature even if these don't support V.110
1888         * because we can emulate this in linklevel.
1889         */
1890        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1891                if (USG_NONE(dev->usage[i]) &&
1892                    (dev->drvmap[i] != -1)) {
1893                        int d = dev->drvmap[i];
1894                        if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1895                            ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1896                                continue;
1897                        if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1898                                continue;
1899                        if (dev->usage[i] & ISDN_USAGE_DISABLED)
1900                                continue; /* usage not allowed */
1901                        if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1902                                if (((dev->drv[d]->interface->features & features) == features) ||
1903                                    (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1904                                     (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1905                                        if ((pre_dev < 0) || (pre_chan < 0)) {
1906                                                dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1907                                                dev->usage[i] |= usage;
1908                                                isdn_info_update();
1909                                                return i;
1910                                        } else {
1911                                                if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1912                                                        dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1913                                                        dev->usage[i] |= usage;
1914                                                        isdn_info_update();
1915                                                        return i;
1916                                                }
1917                                        }
1918                                }
1919                        }
1920                }
1921        return -1;
1922}
1923
1924/*
1925 * Set state of ISDN-channel to 'unused'
1926 */
1927void
1928isdn_free_channel(int di, int ch, int usage)
1929{
1930        int i;
1931
1932        if ((di < 0) || (ch < 0)) {
1933                printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
1934                       __func__, di, ch);
1935                return;
1936        }
1937        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1938                if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1939                    (dev->drvmap[i] == di) &&
1940                    (dev->chanmap[i] == ch)) {
1941                        dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1942                        strcpy(dev->num[i], "???");
1943                        dev->ibytes[i] = 0;
1944                        dev->obytes[i] = 0;
1945// 20.10.99 JIM, try to reinitialize v110 !
1946                        dev->v110emu[i] = 0;
1947                        atomic_set(&(dev->v110use[i]), 0);
1948                        isdn_v110_close(dev->v110[i]);
1949                        dev->v110[i] = NULL;
1950// 20.10.99 JIM, try to reinitialize v110 !
1951                        isdn_info_update();
1952                        if (dev->drv[di])
1953                                skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
1954                }
1955}
1956
1957/*
1958 * Cancel Exclusive-Flag for ISDN-channel
1959 */
1960void
1961isdn_unexclusive_channel(int di, int ch)
1962{
1963        int i;
1964
1965        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1966                if ((dev->drvmap[i] == di) &&
1967                    (dev->chanmap[i] == ch)) {
1968                        dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1969                        isdn_info_update();
1970                        return;
1971                }
1972}
1973
1974/*
1975 *  writebuf replacement for SKB_ABLE drivers
1976 */
1977static int
1978isdn_writebuf_stub(int drvidx, int chan, const u_char __user *buf, int len)
1979{
1980        int ret;
1981        int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1982        struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1983
1984        if (!skb)
1985                return -ENOMEM;
1986        skb_reserve(skb, hl);
1987        if (copy_from_user(skb_put(skb, len), buf, len)) {
1988                dev_kfree_skb(skb);
1989                return -EFAULT;
1990        }
1991        ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1992        if (ret <= 0)
1993                dev_kfree_skb(skb);
1994        if (ret > 0)
1995                dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1996        return ret;
1997}
1998
1999/*
2000 * Return: length of data on success, -ERRcode on failure.
2001 */
2002int
2003isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
2004{
2005        int ret;
2006        struct sk_buff *nskb = NULL;
2007        int v110_ret = skb->len;
2008        int idx = isdn_dc2minor(drvidx, chan);
2009
2010        if (dev->v110[idx]) {
2011                atomic_inc(&dev->v110use[idx]);
2012                nskb = isdn_v110_encode(dev->v110[idx], skb);
2013                atomic_dec(&dev->v110use[idx]);
2014                if (!nskb)
2015                        return 0;
2016                v110_ret = *((int *)nskb->data);
2017                skb_pull(nskb, sizeof(int));
2018                if (!nskb->len) {
2019                        dev_kfree_skb(nskb);
2020                        return v110_ret;
2021                }
2022                /* V.110 must always be acknowledged */
2023                ack = 1;
2024                ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2025        } else {
2026                int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2027
2028                if (skb_headroom(skb) < hl) {
2029                        /*
2030                         * This should only occur when new HL driver with
2031                         * increased hl_hdrlen was loaded after netdevice
2032                         * was created and connected to the new driver.
2033                         *
2034                         * The V.110 branch (re-allocates on its own) does
2035                         * not need this
2036                         */
2037                        struct sk_buff *skb_tmp;
2038
2039                        skb_tmp = skb_realloc_headroom(skb, hl);
2040                        printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2041                        if (!skb_tmp) return -ENOMEM; /* 0 better? */
2042                        ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
2043                        if (ret > 0) {
2044                                dev_kfree_skb(skb);
2045                        } else {
2046                                dev_kfree_skb(skb_tmp);
2047                        }
2048                } else {
2049                        ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2050                }
2051        }
2052        if (ret > 0) {
2053                dev->obytes[idx] += ret;
2054                if (dev->v110[idx]) {
2055                        atomic_inc(&dev->v110use[idx]);
2056                        dev->v110[idx]->skbuser++;
2057                        atomic_dec(&dev->v110use[idx]);
2058                        /* For V.110 return unencoded data length */
2059                        ret = v110_ret;
2060                        /* if the complete frame was send we free the skb;
2061                           if not upper function will requeue the skb */
2062                        if (ret == skb->len)
2063                                dev_kfree_skb(skb);
2064                }
2065        } else
2066                if (dev->v110[idx])
2067                        dev_kfree_skb(nskb);
2068        return ret;
2069}
2070
2071static int
2072isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2073{
2074        int j, k, m;
2075
2076        init_waitqueue_head(&d->st_waitq);
2077        if (d->flags & DRV_FLAG_RUNNING)
2078                return -1;
2079        if (n < 1) return 0;
2080
2081        m = (adding) ? d->channels + n : n;
2082
2083        if (dev->channels + n > ISDN_MAX_CHANNELS) {
2084                printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2085                       ISDN_MAX_CHANNELS);
2086                return -1;
2087        }
2088
2089        if ((adding) && (d->rcverr))
2090                kfree(d->rcverr);
2091        if (!(d->rcverr = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2092                printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2093                return -1;
2094        }
2095
2096        if ((adding) && (d->rcvcount))
2097                kfree(d->rcvcount);
2098        if (!(d->rcvcount = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2099                printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2100                if (!adding)
2101                        kfree(d->rcverr);
2102                return -1;
2103        }
2104
2105        if ((adding) && (d->rpqueue)) {
2106                for (j = 0; j < d->channels; j++)
2107                        skb_queue_purge(&d->rpqueue[j]);
2108                kfree(d->rpqueue);
2109        }
2110        if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
2111                printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2112                if (!adding) {
2113                        kfree(d->rcvcount);
2114                        kfree(d->rcverr);
2115                }
2116                return -1;
2117        }
2118        for (j = 0; j < m; j++) {
2119                skb_queue_head_init(&d->rpqueue[j]);
2120        }
2121
2122        if ((adding) && (d->rcv_waitq))
2123                kfree(d->rcv_waitq);
2124        d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
2125        if (!d->rcv_waitq) {
2126                printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2127                if (!adding) {
2128                        kfree(d->rpqueue);
2129                        kfree(d->rcvcount);
2130                        kfree(d->rcverr);
2131                }
2132                return -1;
2133        }
2134        d->snd_waitq = d->rcv_waitq + m;
2135        for (j = 0; j < m; j++) {
2136                init_waitqueue_head(&d->rcv_waitq[j]);
2137                init_waitqueue_head(&d->snd_waitq[j]);
2138        }
2139
2140        dev->channels += n;
2141        for (j = d->channels; j < m; j++)
2142                for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2143                        if (dev->chanmap[k] < 0) {
2144                                dev->chanmap[k] = j;
2145                                dev->drvmap[k] = drvidx;
2146                                break;
2147                        }
2148        d->channels = m;
2149        return 0;
2150}
2151
2152/*
2153 * Low-level-driver registration
2154 */
2155
2156static void
2157set_global_features(void)
2158{
2159        int drvidx;
2160
2161        dev->global_features = 0;
2162        for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2163                if (!dev->drv[drvidx])
2164                        continue;
2165                if (dev->drv[drvidx]->interface)
2166                        dev->global_features |= dev->drv[drvidx]->interface->features;
2167        }
2168}
2169
2170#ifdef CONFIG_ISDN_DIVERSION
2171
2172static char *map_drvname(int di)
2173{
2174        if ((di < 0) || (di >= ISDN_MAX_DRIVERS))
2175                return (NULL);
2176        return (dev->drvid[di]); /* driver name */
2177} /* map_drvname */
2178
2179static int map_namedrv(char *id)
2180{  int i;
2181
2182        for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2183        { if (!strcmp(dev->drvid[i], id))
2184                        return (i);
2185        }
2186        return (-1);
2187} /* map_namedrv */
2188
2189int DIVERT_REG_NAME(isdn_divert_if *i_div)
2190{
2191        if (i_div->if_magic != DIVERT_IF_MAGIC)
2192                return (DIVERT_VER_ERR);
2193        switch (i_div->cmd)
2194        {
2195        case DIVERT_CMD_REL:
2196                if (divert_if != i_div)
2197                        return (DIVERT_REL_ERR);
2198                divert_if = NULL; /* free interface */
2199                return (DIVERT_NO_ERR);
2200
2201        case DIVERT_CMD_REG:
2202                if (divert_if)
2203                        return (DIVERT_REG_ERR);
2204                i_div->ll_cmd = isdn_command; /* set command function */
2205                i_div->drv_to_name = map_drvname;
2206                i_div->name_to_drv = map_namedrv;
2207                divert_if = i_div; /* remember interface */
2208                return (DIVERT_NO_ERR);
2209
2210        default:
2211                return (DIVERT_CMD_ERR);
2212        }
2213} /* DIVERT_REG_NAME */
2214
2215EXPORT_SYMBOL(DIVERT_REG_NAME);
2216
2217#endif /* CONFIG_ISDN_DIVERSION */
2218
2219
2220EXPORT_SYMBOL(register_isdn);
2221#ifdef CONFIG_ISDN_PPP
2222EXPORT_SYMBOL(isdn_ppp_register_compressor);
2223EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2224#endif
2225
2226int
2227register_isdn(isdn_if *i)
2228{
2229        isdn_driver_t *d;
2230        int j;
2231        ulong flags;
2232        int drvidx;
2233
2234        if (dev->drivers >= ISDN_MAX_DRIVERS) {
2235                printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2236                       ISDN_MAX_DRIVERS);
2237                return 0;
2238        }
2239        if (!i->writebuf_skb) {
2240                printk(KERN_WARNING "register_isdn: No write routine given.\n");
2241                return 0;
2242        }
2243        if (!(d = kzalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
2244                printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2245                return 0;
2246        }
2247
2248        d->maxbufsize = i->maxbufsize;
2249        d->pktcount = 0;
2250        d->stavail = 0;
2251        d->flags = DRV_FLAG_LOADED;
2252        d->online = 0;
2253        d->interface = i;
2254        d->channels = 0;
2255        spin_lock_irqsave(&dev->lock, flags);
2256        for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2257                if (!dev->drv[drvidx])
2258                        break;
2259        if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2260                spin_unlock_irqrestore(&dev->lock, flags);
2261                kfree(d);
2262                return 0;
2263        }
2264        i->channels = drvidx;
2265        i->rcvcallb_skb = isdn_receive_skb_callback;
2266        i->statcallb = isdn_status_callback;
2267        if (!strlen(i->id))
2268                sprintf(i->id, "line%d", drvidx);
2269        for (j = 0; j < drvidx; j++)
2270                if (!strcmp(i->id, dev->drvid[j]))
2271                        sprintf(i->id, "line%d", drvidx);
2272        dev->drv[drvidx] = d;
2273        strcpy(dev->drvid[drvidx], i->id);
2274        isdn_info_update();
2275        dev->drivers++;
2276        set_global_features();
2277        spin_unlock_irqrestore(&dev->lock, flags);
2278        return 1;
2279}
2280
2281/*
2282*****************************************************************************
2283* And now the modules code.
2284*****************************************************************************
2285*/
2286
2287static char *
2288isdn_getrev(const char *revision)
2289{
2290        char *rev;
2291        char *p;
2292
2293        if ((p = strchr(revision, ':'))) {
2294                rev = p + 2;
2295                p = strchr(rev, '$');
2296                *--p = 0;
2297        } else
2298                rev = "???";
2299        return rev;
2300}
2301
2302/*
2303 * Allocate and initialize all data, register modem-devices
2304 */
2305static int __init isdn_init(void)
2306{
2307        int i;
2308        char tmprev[50];
2309
2310        dev = vzalloc(sizeof(isdn_dev));
2311        if (!dev) {
2312                printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2313                return -EIO;
2314        }
2315        init_timer(&dev->timer);
2316        dev->timer.function = isdn_timer_funct;
2317        spin_lock_init(&dev->lock);
2318        spin_lock_init(&dev->timerlock);
2319#ifdef MODULE
2320        dev->owner = THIS_MODULE;
2321#endif
2322        mutex_init(&dev->mtx);
2323        init_waitqueue_head(&dev->info_waitq);
2324        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2325                dev->drvmap[i] = -1;
2326                dev->chanmap[i] = -1;
2327                dev->m_idx[i] = -1;
2328                strcpy(dev->num[i], "???");
2329        }
2330        if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2331                printk(KERN_WARNING "isdn: Could not register control devices\n");
2332                vfree(dev);
2333                return -EIO;
2334        }
2335        if ((isdn_tty_modem_init()) < 0) {
2336                printk(KERN_WARNING "isdn: Could not register tty devices\n");
2337                vfree(dev);
2338                unregister_chrdev(ISDN_MAJOR, "isdn");
2339                return -EIO;
2340        }
2341#ifdef CONFIG_ISDN_PPP
2342        if (isdn_ppp_init() < 0) {
2343                printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2344                isdn_tty_exit();
2345                unregister_chrdev(ISDN_MAJOR, "isdn");
2346                vfree(dev);
2347                return -EIO;
2348        }
2349#endif                          /* CONFIG_ISDN_PPP */
2350
2351        strcpy(tmprev, isdn_revision);
2352        printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
2353        strcpy(tmprev, isdn_net_revision);
2354        printk("%s/", isdn_getrev(tmprev));
2355        strcpy(tmprev, isdn_ppp_revision);
2356        printk("%s/", isdn_getrev(tmprev));
2357        strcpy(tmprev, isdn_audio_revision);
2358        printk("%s/", isdn_getrev(tmprev));
2359        strcpy(tmprev, isdn_v110_revision);
2360        printk("%s", isdn_getrev(tmprev));
2361
2362#ifdef MODULE
2363        printk(" loaded\n");
2364#else
2365        printk("\n");
2366#endif
2367        isdn_info_update();
2368        return 0;
2369}
2370
2371/*
2372 * Unload module
2373 */
2374static void __exit isdn_exit(void)
2375{
2376#ifdef CONFIG_ISDN_PPP
2377        isdn_ppp_cleanup();
2378#endif
2379        if (isdn_net_rmall() < 0) {
2380                printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2381                return;
2382        }
2383        isdn_tty_exit();
2384        unregister_chrdev(ISDN_MAJOR, "isdn");
2385        del_timer(&dev->timer);
2386        /* call vfree with interrupts enabled, else it will hang */
2387        vfree(dev);
2388        printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2389}
2390
2391module_init(isdn_init);
2392module_exit(isdn_exit);
2393