linux/drivers/isdn/i4l/isdn_tty.c
<<
>>
Prefs
   1/*
   2 * Linux ISDN subsystem, tty functions and AT-command emulator (linklevel).
   3 *
   4 * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
   5 * Copyright 1995,96    by Thinking Objects Software GmbH Wuerzburg
   6 *
   7 * This software may be used and distributed according to the terms
   8 * of the GNU General Public License, incorporated herein by reference.
   9 *
  10 */
  11#undef ISDN_TTY_STAT_DEBUG
  12
  13#include <linux/isdn.h>
  14#include <linux/serial.h> /* ASYNC_* flags */
  15#include <linux/slab.h>
  16#include <linux/delay.h>
  17#include <linux/mutex.h>
  18#include "isdn_common.h"
  19#include "isdn_tty.h"
  20#ifdef CONFIG_ISDN_AUDIO
  21#include "isdn_audio.h"
  22#define VBUF 0x3e0
  23#define VBUFX (VBUF/16)
  24#endif
  25
  26#define FIX_FILE_TRANSFER
  27#define DUMMY_HAYES_AT
  28
  29/* Prototypes */
  30
  31static DEFINE_MUTEX(modem_info_mutex);
  32static int isdn_tty_edit_at(const char *, int, modem_info *);
  33static void isdn_tty_check_esc(const u_char *, u_char, int, int *, u_long *);
  34static void isdn_tty_modem_reset_regs(modem_info *, int);
  35static void isdn_tty_cmd_ATA(modem_info *);
  36static void isdn_tty_flush_buffer(struct tty_struct *);
  37static void isdn_tty_modem_result(int, modem_info *);
  38#ifdef CONFIG_ISDN_AUDIO
  39static int isdn_tty_countDLE(unsigned char *, int);
  40#endif
  41
  42/* Leave this unchanged unless you know what you do! */
  43#define MODEM_PARANOIA_CHECK
  44#define MODEM_DO_RESTART
  45
  46static int bit2si[8] =
  47{1, 5, 7, 7, 7, 7, 7, 7};
  48static int si2bit[8] =
  49{4, 1, 4, 4, 4, 4, 4, 4};
  50
  51/* isdn_tty_try_read() is called from within isdn_tty_rcv_skb()
  52 * to stuff incoming data directly into a tty's flip-buffer. This
  53 * is done to speed up tty-receiving if the receive-queue is empty.
  54 * This routine MUST be called with interrupts off.
  55 * Return:
  56 *  1 = Success
  57 *  0 = Failure, data has to be buffered and later processed by
  58 *      isdn_tty_readmodem().
  59 */
  60static int
  61isdn_tty_try_read(modem_info *info, struct sk_buff *skb)
  62{
  63        int c;
  64        int len;
  65        struct tty_struct *tty;
  66        char last;
  67
  68        if (!info->online)
  69                return 0;
  70
  71        tty = info->port.tty;
  72        if (!tty)
  73                return 0;
  74
  75        if (!(info->mcr & UART_MCR_RTS))
  76                return 0;
  77
  78        len = skb->len
  79#ifdef CONFIG_ISDN_AUDIO
  80                + ISDN_AUDIO_SKB_DLECOUNT(skb)
  81#endif
  82                ;
  83
  84        c = tty_buffer_request_room(tty, len);
  85        if (c < len)
  86                return 0;
  87
  88#ifdef CONFIG_ISDN_AUDIO
  89        if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
  90                int l = skb->len;
  91                unsigned char *dp = skb->data;
  92                while (--l) {
  93                        if (*dp == DLE)
  94                                tty_insert_flip_char(tty, DLE, 0);
  95                        tty_insert_flip_char(tty, *dp++, 0);
  96                }
  97                if (*dp == DLE)
  98                        tty_insert_flip_char(tty, DLE, 0);
  99                last = *dp;
 100        } else {
 101#endif
 102                if (len > 1)
 103                        tty_insert_flip_string(tty, skb->data, len - 1);
 104                last = skb->data[len - 1];
 105#ifdef CONFIG_ISDN_AUDIO
 106        }
 107#endif
 108        if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
 109                tty_insert_flip_char(tty, last, 0xFF);
 110        else
 111                tty_insert_flip_char(tty, last, TTY_NORMAL);
 112        tty_flip_buffer_push(tty);
 113        kfree_skb(skb);
 114
 115        return 1;
 116}
 117
 118/* isdn_tty_readmodem() is called periodically from within timer-interrupt.
 119 * It tries getting received data from the receive queue an stuff it into
 120 * the tty's flip-buffer.
 121 */
 122void
 123isdn_tty_readmodem(void)
 124{
 125        int resched = 0;
 126        int midx;
 127        int i;
 128        int r;
 129        struct tty_struct *tty;
 130        modem_info *info;
 131
 132        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
 133                midx = dev->m_idx[i];
 134                if (midx < 0)
 135                        continue;
 136
 137                info = &dev->mdm.info[midx];
 138                if (!info->online)
 139                        continue;
 140
 141                r = 0;
 142#ifdef CONFIG_ISDN_AUDIO
 143                isdn_audio_eval_dtmf(info);
 144                if ((info->vonline & 1) && (info->emu.vpar[1]))
 145                        isdn_audio_eval_silence(info);
 146#endif
 147                tty = info->port.tty;
 148                if (tty) {
 149                        if (info->mcr & UART_MCR_RTS) {
 150                                /* CISCO AsyncPPP Hack */
 151                                if (!(info->emu.mdmreg[REG_CPPP] & BIT_CPPP))
 152                                        r = isdn_readbchan_tty(info->isdn_driver, info->isdn_channel, tty, 0);
 153                                else
 154                                        r = isdn_readbchan_tty(info->isdn_driver, info->isdn_channel, tty, 1);
 155                                if (r)
 156                                        tty_flip_buffer_push(tty);
 157                        } else
 158                                r = 1;
 159                } else
 160                        r = 1;
 161                if (r) {
 162                        info->rcvsched = 0;
 163                        resched = 1;
 164                } else
 165                        info->rcvsched = 1;
 166        }
 167        if (!resched)
 168                isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
 169}
 170
 171int
 172isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
 173{
 174        ulong flags;
 175        int midx;
 176#ifdef CONFIG_ISDN_AUDIO
 177        int ifmt;
 178#endif
 179        modem_info *info;
 180
 181        if ((midx = dev->m_idx[i]) < 0) {
 182                /* if midx is invalid, packet is not for tty */
 183                return 0;
 184        }
 185        info = &dev->mdm.info[midx];
 186#ifdef CONFIG_ISDN_AUDIO
 187        ifmt = 1;
 188
 189        if ((info->vonline) && (!info->emu.vpar[4]))
 190                isdn_audio_calc_dtmf(info, skb->data, skb->len, ifmt);
 191        if ((info->vonline & 1) && (info->emu.vpar[1]))
 192                isdn_audio_calc_silence(info, skb->data, skb->len, ifmt);
 193#endif
 194        if ((info->online < 2)
 195#ifdef CONFIG_ISDN_AUDIO
 196            && (!(info->vonline & 1))
 197#endif
 198                ) {
 199                /* If Modem not listening, drop data */
 200                kfree_skb(skb);
 201                return 1;
 202        }
 203        if (info->emu.mdmreg[REG_T70] & BIT_T70) {
 204                if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT) {
 205                        /* T.70 decoding: throw away the T.70 header (2 or 4 bytes)   */
 206                        if (skb->data[0] == 3) /* pure data packet -> 4 byte headers  */
 207                                skb_pull(skb, 4);
 208                        else
 209                                if (skb->data[0] == 1) /* keepalive packet -> 2 byte hdr  */
 210                                        skb_pull(skb, 2);
 211                } else
 212                        /* T.70 decoding: Simply throw away the T.70 header (4 bytes) */
 213                        if ((skb->data[0] == 1) && ((skb->data[1] == 0) || (skb->data[1] == 1)))
 214                                skb_pull(skb, 4);
 215        }
 216#ifdef CONFIG_ISDN_AUDIO
 217        ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
 218        ISDN_AUDIO_SKB_LOCK(skb) = 0;
 219        if (info->vonline & 1) {
 220                /* voice conversion/compression */
 221                switch (info->emu.vpar[3]) {
 222                case 2:
 223                case 3:
 224                case 4:
 225                        /* adpcm
 226                         * Since compressed data takes less
 227                         * space, we can overwrite the buffer.
 228                         */
 229                        skb_trim(skb, isdn_audio_xlaw2adpcm(info->adpcmr,
 230                                                            ifmt,
 231                                                            skb->data,
 232                                                            skb->data,
 233                                                            skb->len));
 234                        break;
 235                case 5:
 236                        /* a-law */
 237                        if (!ifmt)
 238                                isdn_audio_ulaw2alaw(skb->data, skb->len);
 239                        break;
 240                case 6:
 241                        /* u-law */
 242                        if (ifmt)
 243                                isdn_audio_alaw2ulaw(skb->data, skb->len);
 244                        break;
 245                }
 246                ISDN_AUDIO_SKB_DLECOUNT(skb) =
 247                        isdn_tty_countDLE(skb->data, skb->len);
 248        }
 249#ifdef CONFIG_ISDN_TTY_FAX
 250        else {
 251                if (info->faxonline & 2) {
 252                        isdn_tty_fax_bitorder(info, skb);
 253                        ISDN_AUDIO_SKB_DLECOUNT(skb) =
 254                                isdn_tty_countDLE(skb->data, skb->len);
 255                }
 256        }
 257#endif
 258#endif
 259        /* Try to deliver directly via tty-buf if queue is empty */
 260        spin_lock_irqsave(&info->readlock, flags);
 261        if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
 262                if (isdn_tty_try_read(info, skb)) {
 263                        spin_unlock_irqrestore(&info->readlock, flags);
 264                        return 1;
 265                }
 266        /* Direct deliver failed or queue wasn't empty.
 267         * Queue up for later dequeueing via timer-irq.
 268         */
 269        __skb_queue_tail(&dev->drv[di]->rpqueue[channel], skb);
 270        dev->drv[di]->rcvcount[channel] +=
 271                (skb->len
 272#ifdef CONFIG_ISDN_AUDIO
 273                 + ISDN_AUDIO_SKB_DLECOUNT(skb)
 274#endif
 275                        );
 276        spin_unlock_irqrestore(&info->readlock, flags);
 277        /* Schedule dequeuing */
 278        if ((dev->modempoll) && (info->rcvsched))
 279                isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
 280        return 1;
 281}
 282
 283static void
 284isdn_tty_cleanup_xmit(modem_info *info)
 285{
 286        skb_queue_purge(&info->xmit_queue);
 287#ifdef CONFIG_ISDN_AUDIO
 288        skb_queue_purge(&info->dtmf_queue);
 289#endif
 290}
 291
 292static void
 293isdn_tty_tint(modem_info *info)
 294{
 295        struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
 296        int len, slen;
 297
 298        if (!skb)
 299                return;
 300        len = skb->len;
 301        if ((slen = isdn_writebuf_skb_stub(info->isdn_driver,
 302                                           info->isdn_channel, 1, skb)) == len) {
 303                struct tty_struct *tty = info->port.tty;
 304                info->send_outstanding++;
 305                info->msr &= ~UART_MSR_CTS;
 306                info->lsr &= ~UART_LSR_TEMT;
 307                tty_wakeup(tty);
 308                return;
 309        }
 310        if (slen < 0) {
 311                /* Error: no channel, already shutdown, or wrong parameter */
 312                dev_kfree_skb(skb);
 313                return;
 314        }
 315        skb_queue_head(&info->xmit_queue, skb);
 316}
 317
 318#ifdef CONFIG_ISDN_AUDIO
 319static int
 320isdn_tty_countDLE(unsigned char *buf, int len)
 321{
 322        int count = 0;
 323
 324        while (len--)
 325                if (*buf++ == DLE)
 326                        count++;
 327        return count;
 328}
 329
 330/* This routine is called from within isdn_tty_write() to perform
 331 * DLE-decoding when sending audio-data.
 332 */
 333static int
 334isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
 335{
 336        unsigned char *p = &info->port.xmit_buf[info->xmit_count];
 337        int count = 0;
 338
 339        while (len > 0) {
 340                if (m->lastDLE) {
 341                        m->lastDLE = 0;
 342                        switch (*p) {
 343                        case DLE:
 344                                /* Escape code */
 345                                if (len > 1)
 346                                        memmove(p, p + 1, len - 1);
 347                                p--;
 348                                count++;
 349                                break;
 350                        case ETX:
 351                                /* End of data */
 352                                info->vonline |= 4;
 353                                return count;
 354                        case DC4:
 355                                /* Abort RX */
 356                                info->vonline &= ~1;
 357#ifdef ISDN_DEBUG_MODEM_VOICE
 358                                printk(KERN_DEBUG
 359                                       "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
 360                                       info->line);
 361#endif
 362                                isdn_tty_at_cout("\020\003", info);
 363                                if (!info->vonline) {
 364#ifdef ISDN_DEBUG_MODEM_VOICE
 365                                        printk(KERN_DEBUG
 366                                               "DLEdown: send VCON on ttyI%d\n",
 367                                               info->line);
 368#endif
 369                                        isdn_tty_at_cout("\r\nVCON\r\n", info);
 370                                }
 371                                /* Fall through */
 372                        case 'q':
 373                        case 's':
 374                                /* Silence */
 375                                if (len > 1)
 376                                        memmove(p, p + 1, len - 1);
 377                                p--;
 378                                break;
 379                        }
 380                } else {
 381                        if (*p == DLE)
 382                                m->lastDLE = 1;
 383                        else
 384                                count++;
 385                }
 386                p++;
 387                len--;
 388        }
 389        if (len < 0) {
 390                printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
 391                return 0;
 392        }
 393        return count;
 394}
 395
 396/* This routine is called from within isdn_tty_write() when receiving
 397 * audio-data. It interrupts receiving, if an character other than
 398 * ^S or ^Q is sent.
 399 */
 400static int
 401isdn_tty_end_vrx(const char *buf, int c)
 402{
 403        char ch;
 404
 405        while (c--) {
 406                ch = *buf;
 407                if ((ch != 0x11) && (ch != 0x13))
 408                        return 1;
 409                buf++;
 410        }
 411        return 0;
 412}
 413
 414static int voice_cf[7] =
 415{0, 0, 4, 3, 2, 0, 0};
 416
 417#endif                          /* CONFIG_ISDN_AUDIO */
 418
 419/* isdn_tty_senddown() is called either directly from within isdn_tty_write()
 420 * or via timer-interrupt from within isdn_tty_modem_xmit(). It pulls
 421 * outgoing data from the tty's xmit-buffer, handles voice-decompression or
 422 * T.70 if necessary, and finally queues it up for sending via isdn_tty_tint.
 423 */
 424static void
 425isdn_tty_senddown(modem_info *info)
 426{
 427        int buflen;
 428        int skb_res;
 429#ifdef CONFIG_ISDN_AUDIO
 430        int audio_len;
 431#endif
 432        struct sk_buff *skb;
 433
 434#ifdef CONFIG_ISDN_AUDIO
 435        if (info->vonline & 4) {
 436                info->vonline &= ~6;
 437                if (!info->vonline) {
 438#ifdef ISDN_DEBUG_MODEM_VOICE
 439                        printk(KERN_DEBUG
 440                               "senddown: send VCON on ttyI%d\n",
 441                               info->line);
 442#endif
 443                        isdn_tty_at_cout("\r\nVCON\r\n", info);
 444                }
 445        }
 446#endif
 447        if (!(buflen = info->xmit_count))
 448                return;
 449        if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
 450                info->msr &= ~UART_MSR_CTS;
 451        info->lsr &= ~UART_LSR_TEMT;
 452        /* info->xmit_count is modified here and in isdn_tty_write().
 453         * So we return here if isdn_tty_write() is in the
 454         * critical section.
 455         */
 456        atomic_inc(&info->xmit_lock);
 457        if (!(atomic_dec_and_test(&info->xmit_lock)))
 458                return;
 459        if (info->isdn_driver < 0) {
 460                info->xmit_count = 0;
 461                return;
 462        }
 463        skb_res = dev->drv[info->isdn_driver]->interface->hl_hdrlen + 4;
 464#ifdef CONFIG_ISDN_AUDIO
 465        if (info->vonline & 2)
 466                audio_len = buflen * voice_cf[info->emu.vpar[3]];
 467        else
 468                audio_len = 0;
 469        skb = dev_alloc_skb(skb_res + buflen + audio_len);
 470#else
 471        skb = dev_alloc_skb(skb_res + buflen);
 472#endif
 473        if (!skb) {
 474                printk(KERN_WARNING
 475                       "isdn_tty: Out of memory in ttyI%d senddown\n",
 476                       info->line);
 477                return;
 478        }
 479        skb_reserve(skb, skb_res);
 480        memcpy(skb_put(skb, buflen), info->port.xmit_buf, buflen);
 481        info->xmit_count = 0;
 482#ifdef CONFIG_ISDN_AUDIO
 483        if (info->vonline & 2) {
 484                /* For now, ifmt is fixed to 1 (alaw), since this
 485                 * is used with ISDN everywhere in the world, except
 486                 * US, Canada and Japan.
 487                 * Later, when US-ISDN protocols are implemented,
 488                 * this setting will depend on the D-channel protocol.
 489                 */
 490                int ifmt = 1;
 491
 492                /* voice conversion/decompression */
 493                switch (info->emu.vpar[3]) {
 494                case 2:
 495                case 3:
 496                case 4:
 497                        /* adpcm, compatible to ZyXel 1496 modem
 498                         * with ROM revision 6.01
 499                         */
 500                        audio_len = isdn_audio_adpcm2xlaw(info->adpcms,
 501                                                          ifmt,
 502                                                          skb->data,
 503                                                          skb_put(skb, audio_len),
 504                                                          buflen);
 505                        skb_pull(skb, buflen);
 506                        skb_trim(skb, audio_len);
 507                        break;
 508                case 5:
 509                        /* a-law */
 510                        if (!ifmt)
 511                                isdn_audio_alaw2ulaw(skb->data,
 512                                                     buflen);
 513                        break;
 514                case 6:
 515                        /* u-law */
 516                        if (ifmt)
 517                                isdn_audio_ulaw2alaw(skb->data,
 518                                                     buflen);
 519                        break;
 520                }
 521        }
 522#endif                          /* CONFIG_ISDN_AUDIO */
 523        if (info->emu.mdmreg[REG_T70] & BIT_T70) {
 524                /* Add T.70 simplified header */
 525                if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT)
 526                        memcpy(skb_push(skb, 2), "\1\0", 2);
 527                else
 528                        memcpy(skb_push(skb, 4), "\1\0\1\0", 4);
 529        }
 530        skb_queue_tail(&info->xmit_queue, skb);
 531}
 532
 533/************************************************************
 534 *
 535 * Modem-functions
 536 *
 537 * mostly "stolen" from original Linux-serial.c and friends.
 538 *
 539 ************************************************************/
 540
 541/* The next routine is called once from within timer-interrupt
 542 * triggered within isdn_tty_modem_ncarrier(). It calls
 543 * isdn_tty_modem_result() to stuff a "NO CARRIER" Message
 544 * into the tty's buffer.
 545 */
 546static void
 547isdn_tty_modem_do_ncarrier(unsigned long data)
 548{
 549        modem_info *info = (modem_info *) data;
 550        isdn_tty_modem_result(RESULT_NO_CARRIER, info);
 551}
 552
 553/* Next routine is called, whenever the DTR-signal is raised.
 554 * It checks the ncarrier-flag, and triggers the above routine
 555 * when necessary. The ncarrier-flag is set, whenever DTR goes
 556 * low.
 557 */
 558static void
 559isdn_tty_modem_ncarrier(modem_info *info)
 560{
 561        if (info->ncarrier) {
 562                info->nc_timer.expires = jiffies + HZ;
 563                add_timer(&info->nc_timer);
 564        }
 565}
 566
 567/*
 568 * return the usage calculated by si and layer 2 protocol
 569 */
 570static int
 571isdn_calc_usage(int si, int l2)
 572{
 573        int usg = ISDN_USAGE_MODEM;
 574
 575#ifdef CONFIG_ISDN_AUDIO
 576        if (si == 1) {
 577                switch (l2) {
 578                case ISDN_PROTO_L2_MODEM:
 579                        usg = ISDN_USAGE_MODEM;
 580                        break;
 581#ifdef CONFIG_ISDN_TTY_FAX
 582                case ISDN_PROTO_L2_FAX:
 583                        usg = ISDN_USAGE_FAX;
 584                        break;
 585#endif
 586                case ISDN_PROTO_L2_TRANS:
 587                default:
 588                        usg = ISDN_USAGE_VOICE;
 589                        break;
 590                }
 591        }
 592#endif
 593        return (usg);
 594}
 595
 596/* isdn_tty_dial() performs dialing of a tty an the necessary
 597 * setup of the lower levels before that.
 598 */
 599static void
 600isdn_tty_dial(char *n, modem_info *info, atemu *m)
 601{
 602        int usg = ISDN_USAGE_MODEM;
 603        int si = 7;
 604        int l2 = m->mdmreg[REG_L2PROT];
 605        u_long flags;
 606        isdn_ctrl cmd;
 607        int i;
 608        int j;
 609
 610        for (j = 7; j >= 0; j--)
 611                if (m->mdmreg[REG_SI1] & (1 << j)) {
 612                        si = bit2si[j];
 613                        break;
 614                }
 615        usg = isdn_calc_usage(si, l2);
 616#ifdef CONFIG_ISDN_AUDIO
 617        if ((si == 1) &&
 618            (l2 != ISDN_PROTO_L2_MODEM)
 619#ifdef CONFIG_ISDN_TTY_FAX
 620            && (l2 != ISDN_PROTO_L2_FAX)
 621#endif
 622                ) {
 623                l2 = ISDN_PROTO_L2_TRANS;
 624                usg = ISDN_USAGE_VOICE;
 625        }
 626#endif
 627        m->mdmreg[REG_SI1I] = si2bit[si];
 628        spin_lock_irqsave(&dev->lock, flags);
 629        i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
 630        if (i < 0) {
 631                spin_unlock_irqrestore(&dev->lock, flags);
 632                isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
 633        } else {
 634                info->isdn_driver = dev->drvmap[i];
 635                info->isdn_channel = dev->chanmap[i];
 636                info->drv_index = i;
 637                dev->m_idx[i] = info->line;
 638                dev->usage[i] |= ISDN_USAGE_OUTGOING;
 639                info->last_dir = 1;
 640                strcpy(info->last_num, n);
 641                isdn_info_update();
 642                spin_unlock_irqrestore(&dev->lock, flags);
 643                cmd.driver = info->isdn_driver;
 644                cmd.arg = info->isdn_channel;
 645                cmd.command = ISDN_CMD_CLREAZ;
 646                isdn_command(&cmd);
 647                strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
 648                cmd.driver = info->isdn_driver;
 649                cmd.command = ISDN_CMD_SETEAZ;
 650                isdn_command(&cmd);
 651                cmd.driver = info->isdn_driver;
 652                cmd.command = ISDN_CMD_SETL2;
 653                info->last_l2 = l2;
 654                cmd.arg = info->isdn_channel + (l2 << 8);
 655                isdn_command(&cmd);
 656                cmd.driver = info->isdn_driver;
 657                cmd.command = ISDN_CMD_SETL3;
 658                cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
 659#ifdef CONFIG_ISDN_TTY_FAX
 660                if (l2 == ISDN_PROTO_L2_FAX) {
 661                        cmd.parm.fax = info->fax;
 662                        info->fax->direction = ISDN_TTY_FAX_CONN_OUT;
 663                }
 664#endif
 665                isdn_command(&cmd);
 666                cmd.driver = info->isdn_driver;
 667                cmd.arg = info->isdn_channel;
 668                sprintf(cmd.parm.setup.phone, "%s", n);
 669                sprintf(cmd.parm.setup.eazmsn, "%s",
 670                        isdn_map_eaz2msn(m->msn, info->isdn_driver));
 671                cmd.parm.setup.si1 = si;
 672                cmd.parm.setup.si2 = m->mdmreg[REG_SI2];
 673                cmd.command = ISDN_CMD_DIAL;
 674                info->dialing = 1;
 675                info->emu.carrierwait = 0;
 676                strcpy(dev->num[i], n);
 677                isdn_info_update();
 678                isdn_command(&cmd);
 679                isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
 680        }
 681}
 682
 683/* isdn_tty_hangup() disassociates a tty from the real
 684 * ISDN-line (hangup). The usage-status is cleared
 685 * and some cleanup is done also.
 686 */
 687void
 688isdn_tty_modem_hup(modem_info *info, int local)
 689{
 690        isdn_ctrl cmd;
 691        int di, ch;
 692
 693        if (!info)
 694                return;
 695
 696        di = info->isdn_driver;
 697        ch = info->isdn_channel;
 698        if (di < 0 || ch < 0)
 699                return;
 700
 701        info->isdn_driver = -1;
 702        info->isdn_channel = -1;
 703
 704#ifdef ISDN_DEBUG_MODEM_HUP
 705        printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
 706#endif
 707        info->rcvsched = 0;
 708        isdn_tty_flush_buffer(info->port.tty);
 709        if (info->online) {
 710                info->last_lhup = local;
 711                info->online = 0;
 712                isdn_tty_modem_result(RESULT_NO_CARRIER, info);
 713        }
 714#ifdef CONFIG_ISDN_AUDIO
 715        info->vonline = 0;
 716#ifdef CONFIG_ISDN_TTY_FAX
 717        info->faxonline = 0;
 718        info->fax->phase = ISDN_FAX_PHASE_IDLE;
 719#endif
 720        info->emu.vpar[4] = 0;
 721        info->emu.vpar[5] = 8;
 722        kfree(info->dtmf_state);
 723        info->dtmf_state = NULL;
 724        kfree(info->silence_state);
 725        info->silence_state = NULL;
 726        kfree(info->adpcms);
 727        info->adpcms = NULL;
 728        kfree(info->adpcmr);
 729        info->adpcmr = NULL;
 730#endif
 731        if ((info->msr & UART_MSR_RI) &&
 732            (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
 733                isdn_tty_modem_result(RESULT_RUNG, info);
 734        info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
 735        info->lsr |= UART_LSR_TEMT;
 736
 737        if (local) {
 738                cmd.driver = di;
 739                cmd.command = ISDN_CMD_HANGUP;
 740                cmd.arg = ch;
 741                isdn_command(&cmd);
 742        }
 743
 744        isdn_all_eaz(di, ch);
 745        info->emu.mdmreg[REG_RINGCNT] = 0;
 746        isdn_free_channel(di, ch, 0);
 747
 748        if (info->drv_index >= 0) {
 749                dev->m_idx[info->drv_index] = -1;
 750                info->drv_index = -1;
 751        }
 752}
 753
 754/*
 755 * Begin of a CAPI like interface, currently used only for
 756 * supplementary service (CAPI 2.0 part III)
 757 */
 758#include <linux/isdn/capicmd.h>
 759#include <linux/module.h>
 760
 761int
 762isdn_tty_capi_facility(capi_msg *cm) {
 763        return (-1); /* dummy */
 764}
 765
 766/* isdn_tty_suspend() tries to suspend the current tty connection
 767 */
 768static void
 769isdn_tty_suspend(char *id, modem_info *info, atemu *m)
 770{
 771        isdn_ctrl cmd;
 772
 773        int l;
 774
 775        if (!info)
 776                return;
 777
 778#ifdef ISDN_DEBUG_MODEM_SERVICES
 779        printk(KERN_DEBUG "Msusp ttyI%d\n", info->line);
 780#endif
 781        l = strlen(id);
 782        if ((info->isdn_driver >= 0)) {
 783                cmd.parm.cmsg.Length = l + 18;
 784                cmd.parm.cmsg.Command = CAPI_FACILITY;
 785                cmd.parm.cmsg.Subcommand = CAPI_REQ;
 786                cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
 787                cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
 788                cmd.parm.cmsg.para[1] = 0;
 789                cmd.parm.cmsg.para[2] = l + 3;
 790                cmd.parm.cmsg.para[3] = 4; /* 16 bit 0x0004 Suspend */
 791                cmd.parm.cmsg.para[4] = 0;
 792                cmd.parm.cmsg.para[5] = l;
 793                strncpy(&cmd.parm.cmsg.para[6], id, l);
 794                cmd.command = CAPI_PUT_MESSAGE;
 795                cmd.driver = info->isdn_driver;
 796                cmd.arg = info->isdn_channel;
 797                isdn_command(&cmd);
 798        }
 799}
 800
 801/* isdn_tty_resume() tries to resume a suspended call
 802 * setup of the lower levels before that. unfortunately here is no
 803 * checking for compatibility of used protocols implemented by Q931
 804 * It does the same things like isdn_tty_dial, the last command
 805 * is different, may be we can merge it.
 806 */
 807
 808static void
 809isdn_tty_resume(char *id, modem_info *info, atemu *m)
 810{
 811        int usg = ISDN_USAGE_MODEM;
 812        int si = 7;
 813        int l2 = m->mdmreg[REG_L2PROT];
 814        isdn_ctrl cmd;
 815        ulong flags;
 816        int i;
 817        int j;
 818        int l;
 819
 820        l = strlen(id);
 821        for (j = 7; j >= 0; j--)
 822                if (m->mdmreg[REG_SI1] & (1 << j)) {
 823                        si = bit2si[j];
 824                        break;
 825                }
 826        usg = isdn_calc_usage(si, l2);
 827#ifdef CONFIG_ISDN_AUDIO
 828        if ((si == 1) &&
 829            (l2 != ISDN_PROTO_L2_MODEM)
 830#ifdef CONFIG_ISDN_TTY_FAX
 831            && (l2 != ISDN_PROTO_L2_FAX)
 832#endif
 833                ) {
 834                l2 = ISDN_PROTO_L2_TRANS;
 835                usg = ISDN_USAGE_VOICE;
 836        }
 837#endif
 838        m->mdmreg[REG_SI1I] = si2bit[si];
 839        spin_lock_irqsave(&dev->lock, flags);
 840        i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
 841        if (i < 0) {
 842                spin_unlock_irqrestore(&dev->lock, flags);
 843                isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
 844        } else {
 845                info->isdn_driver = dev->drvmap[i];
 846                info->isdn_channel = dev->chanmap[i];
 847                info->drv_index = i;
 848                dev->m_idx[i] = info->line;
 849                dev->usage[i] |= ISDN_USAGE_OUTGOING;
 850                info->last_dir = 1;
 851//              strcpy(info->last_num, n);
 852                isdn_info_update();
 853                spin_unlock_irqrestore(&dev->lock, flags);
 854                cmd.driver = info->isdn_driver;
 855                cmd.arg = info->isdn_channel;
 856                cmd.command = ISDN_CMD_CLREAZ;
 857                isdn_command(&cmd);
 858                strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
 859                cmd.driver = info->isdn_driver;
 860                cmd.command = ISDN_CMD_SETEAZ;
 861                isdn_command(&cmd);
 862                cmd.driver = info->isdn_driver;
 863                cmd.command = ISDN_CMD_SETL2;
 864                info->last_l2 = l2;
 865                cmd.arg = info->isdn_channel + (l2 << 8);
 866                isdn_command(&cmd);
 867                cmd.driver = info->isdn_driver;
 868                cmd.command = ISDN_CMD_SETL3;
 869                cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
 870                isdn_command(&cmd);
 871                cmd.driver = info->isdn_driver;
 872                cmd.arg = info->isdn_channel;
 873                cmd.parm.cmsg.Length = l + 18;
 874                cmd.parm.cmsg.Command = CAPI_FACILITY;
 875                cmd.parm.cmsg.Subcommand = CAPI_REQ;
 876                cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
 877                cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
 878                cmd.parm.cmsg.para[1] = 0;
 879                cmd.parm.cmsg.para[2] = l + 3;
 880                cmd.parm.cmsg.para[3] = 5; /* 16 bit 0x0005 Resume */
 881                cmd.parm.cmsg.para[4] = 0;
 882                cmd.parm.cmsg.para[5] = l;
 883                strncpy(&cmd.parm.cmsg.para[6], id, l);
 884                cmd.command = CAPI_PUT_MESSAGE;
 885                info->dialing = 1;
 886//              strcpy(dev->num[i], n);
 887                isdn_info_update();
 888                isdn_command(&cmd);
 889                isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
 890        }
 891}
 892
 893/* isdn_tty_send_msg() sends a message to a HL driver
 894 * This is used for hybrid modem cards to send AT commands to it
 895 */
 896
 897static void
 898isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
 899{
 900        int usg = ISDN_USAGE_MODEM;
 901        int si = 7;
 902        int l2 = m->mdmreg[REG_L2PROT];
 903        isdn_ctrl cmd;
 904        ulong flags;
 905        int i;
 906        int j;
 907        int l;
 908
 909        l = strlen(msg);
 910        if (!l) {
 911                isdn_tty_modem_result(RESULT_ERROR, info);
 912                return;
 913        }
 914        for (j = 7; j >= 0; j--)
 915                if (m->mdmreg[REG_SI1] & (1 << j)) {
 916                        si = bit2si[j];
 917                        break;
 918                }
 919        usg = isdn_calc_usage(si, l2);
 920#ifdef CONFIG_ISDN_AUDIO
 921        if ((si == 1) &&
 922            (l2 != ISDN_PROTO_L2_MODEM)
 923#ifdef CONFIG_ISDN_TTY_FAX
 924            && (l2 != ISDN_PROTO_L2_FAX)
 925#endif
 926                ) {
 927                l2 = ISDN_PROTO_L2_TRANS;
 928                usg = ISDN_USAGE_VOICE;
 929        }
 930#endif
 931        m->mdmreg[REG_SI1I] = si2bit[si];
 932        spin_lock_irqsave(&dev->lock, flags);
 933        i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
 934        if (i < 0) {
 935                spin_unlock_irqrestore(&dev->lock, flags);
 936                isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
 937        } else {
 938                info->isdn_driver = dev->drvmap[i];
 939                info->isdn_channel = dev->chanmap[i];
 940                info->drv_index = i;
 941                dev->m_idx[i] = info->line;
 942                dev->usage[i] |= ISDN_USAGE_OUTGOING;
 943                info->last_dir = 1;
 944                isdn_info_update();
 945                spin_unlock_irqrestore(&dev->lock, flags);
 946                cmd.driver = info->isdn_driver;
 947                cmd.arg = info->isdn_channel;
 948                cmd.command = ISDN_CMD_CLREAZ;
 949                isdn_command(&cmd);
 950                strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
 951                cmd.driver = info->isdn_driver;
 952                cmd.command = ISDN_CMD_SETEAZ;
 953                isdn_command(&cmd);
 954                cmd.driver = info->isdn_driver;
 955                cmd.command = ISDN_CMD_SETL2;
 956                info->last_l2 = l2;
 957                cmd.arg = info->isdn_channel + (l2 << 8);
 958                isdn_command(&cmd);
 959                cmd.driver = info->isdn_driver;
 960                cmd.command = ISDN_CMD_SETL3;
 961                cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
 962                isdn_command(&cmd);
 963                cmd.driver = info->isdn_driver;
 964                cmd.arg = info->isdn_channel;
 965                cmd.parm.cmsg.Length = l + 14;
 966                cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
 967                cmd.parm.cmsg.Subcommand = CAPI_REQ;
 968                cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
 969                cmd.parm.cmsg.para[0] = l + 1;
 970                strncpy(&cmd.parm.cmsg.para[1], msg, l);
 971                cmd.parm.cmsg.para[l + 1] = 0xd;
 972                cmd.command = CAPI_PUT_MESSAGE;
 973/*              info->dialing = 1;
 974                strcpy(dev->num[i], n);
 975                isdn_info_update();
 976*/
 977                isdn_command(&cmd);
 978        }
 979}
 980
 981static inline int
 982isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
 983{
 984#ifdef MODEM_PARANOIA_CHECK
 985        if (!info) {
 986                printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
 987                       name, routine);
 988                return 1;
 989        }
 990        if (info->magic != ISDN_ASYNC_MAGIC) {
 991                printk(KERN_WARNING "isdn_tty: bad magic for modem struct %s in %s\n",
 992                       name, routine);
 993                return 1;
 994        }
 995#endif
 996        return 0;
 997}
 998
 999/*
1000 * This routine is called to set the UART divisor registers to match
1001 * the specified baud rate for a serial port.
1002 */
1003static void
1004isdn_tty_change_speed(modem_info *info)
1005{
1006        struct tty_port *port = &info->port;
1007        uint cflag,
1008                cval,
1009                quot;
1010        int i;
1011
1012        if (!port->tty || !port->tty->termios)
1013                return;
1014        cflag = port->tty->termios->c_cflag;
1015
1016        quot = i = cflag & CBAUD;
1017        if (i & CBAUDEX) {
1018                i &= ~CBAUDEX;
1019                if (i < 1 || i > 2)
1020                        port->tty->termios->c_cflag &= ~CBAUDEX;
1021                else
1022                        i += 15;
1023        }
1024        if (quot) {
1025                info->mcr |= UART_MCR_DTR;
1026                isdn_tty_modem_ncarrier(info);
1027        } else {
1028                info->mcr &= ~UART_MCR_DTR;
1029                if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1030#ifdef ISDN_DEBUG_MODEM_HUP
1031                        printk(KERN_DEBUG "Mhup in changespeed\n");
1032#endif
1033                        if (info->online)
1034                                info->ncarrier = 1;
1035                        isdn_tty_modem_reset_regs(info, 0);
1036                        isdn_tty_modem_hup(info, 1);
1037                }
1038                return;
1039        }
1040        /* byte size and parity */
1041        cval = cflag & (CSIZE | CSTOPB);
1042        cval >>= 4;
1043        if (cflag & PARENB)
1044                cval |= UART_LCR_PARITY;
1045        if (!(cflag & PARODD))
1046                cval |= UART_LCR_EPAR;
1047
1048        /* CTS flow control flag and modem status interrupts */
1049        if (cflag & CRTSCTS) {
1050                port->flags |= ASYNC_CTS_FLOW;
1051        } else
1052                port->flags &= ~ASYNC_CTS_FLOW;
1053        if (cflag & CLOCAL)
1054                port->flags &= ~ASYNC_CHECK_CD;
1055        else {
1056                port->flags |= ASYNC_CHECK_CD;
1057        }
1058}
1059
1060static int
1061isdn_tty_startup(modem_info *info)
1062{
1063        if (info->port.flags & ASYNC_INITIALIZED)
1064                return 0;
1065        isdn_lock_drivers();
1066#ifdef ISDN_DEBUG_MODEM_OPEN
1067        printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
1068#endif
1069        /*
1070         * Now, initialize the UART
1071         */
1072        info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
1073        if (info->port.tty)
1074                clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
1075        /*
1076         * and set the speed of the serial port
1077         */
1078        isdn_tty_change_speed(info);
1079
1080        info->port.flags |= ASYNC_INITIALIZED;
1081        info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
1082        info->send_outstanding = 0;
1083        return 0;
1084}
1085
1086/*
1087 * This routine will shutdown a serial port; interrupts are disabled, and
1088 * DTR is dropped if the hangup on close termio flag is on.
1089 */
1090static void
1091isdn_tty_shutdown(modem_info *info)
1092{
1093        if (!(info->port.flags & ASYNC_INITIALIZED))
1094                return;
1095#ifdef ISDN_DEBUG_MODEM_OPEN
1096        printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
1097#endif
1098        isdn_unlock_drivers();
1099        info->msr &= ~UART_MSR_RI;
1100        if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL)) {
1101                info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
1102                if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1103                        isdn_tty_modem_reset_regs(info, 0);
1104#ifdef ISDN_DEBUG_MODEM_HUP
1105                        printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
1106#endif
1107                        isdn_tty_modem_hup(info, 1);
1108                }
1109        }
1110        if (info->port.tty)
1111                set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1112
1113        info->port.flags &= ~ASYNC_INITIALIZED;
1114}
1115
1116/* isdn_tty_write() is the main send-routine. It is called from the upper
1117 * levels within the kernel to perform sending data. Depending on the
1118 * online-flag it either directs output to the at-command-interpreter or
1119 * to the lower level. Additional tasks done here:
1120 *  - If online, check for escape-sequence (+++)
1121 *  - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
1122 *  - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
1123 *  - If dialing, abort dial.
1124 */
1125static int
1126isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
1127{
1128        int c;
1129        int total = 0;
1130        modem_info *info = (modem_info *) tty->driver_data;
1131        atemu *m = &info->emu;
1132
1133        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write"))
1134                return 0;
1135        /* See isdn_tty_senddown() */
1136        atomic_inc(&info->xmit_lock);
1137        while (1) {
1138                c = count;
1139                if (c > info->xmit_size - info->xmit_count)
1140                        c = info->xmit_size - info->xmit_count;
1141                if (info->isdn_driver >= 0 && c > dev->drv[info->isdn_driver]->maxbufsize)
1142                        c = dev->drv[info->isdn_driver]->maxbufsize;
1143                if (c <= 0)
1144                        break;
1145                if ((info->online > 1)
1146#ifdef CONFIG_ISDN_AUDIO
1147                    || (info->vonline & 3)
1148#endif
1149                        ) {
1150#ifdef CONFIG_ISDN_AUDIO
1151                        if (!info->vonline)
1152#endif
1153                                isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
1154                                                   &(m->pluscount),
1155                                                   &(m->lastplus));
1156                        memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
1157#ifdef CONFIG_ISDN_AUDIO
1158                        if (info->vonline) {
1159                                int cc = isdn_tty_handleDLEdown(info, m, c);
1160                                if (info->vonline & 2) {
1161                                        if (!cc) {
1162                                                /* If DLE decoding results in zero-transmit, but
1163                                                 * c originally was non-zero, do a wakeup.
1164                                                 */
1165                                                tty_wakeup(tty);
1166                                                info->msr |= UART_MSR_CTS;
1167                                                info->lsr |= UART_LSR_TEMT;
1168                                        }
1169                                        info->xmit_count += cc;
1170                                }
1171                                if ((info->vonline & 3) == 1) {
1172                                        /* Do NOT handle Ctrl-Q or Ctrl-S
1173                                         * when in full-duplex audio mode.
1174                                         */
1175                                        if (isdn_tty_end_vrx(buf, c)) {
1176                                                info->vonline &= ~1;
1177#ifdef ISDN_DEBUG_MODEM_VOICE
1178                                                printk(KERN_DEBUG
1179                                                       "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
1180                                                       info->line);
1181#endif
1182                                                isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
1183                                        }
1184                                }
1185                        } else
1186                                if (TTY_IS_FCLASS1(info)) {
1187                                        int cc = isdn_tty_handleDLEdown(info, m, c);
1188
1189                                        if (info->vonline & 4) { /* ETX seen */
1190                                                isdn_ctrl c;
1191
1192                                                c.command = ISDN_CMD_FAXCMD;
1193                                                c.driver = info->isdn_driver;
1194                                                c.arg = info->isdn_channel;
1195                                                c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
1196                                                c.parm.aux.subcmd = ETX;
1197                                                isdn_command(&c);
1198                                        }
1199                                        info->vonline = 0;
1200#ifdef ISDN_DEBUG_MODEM_VOICE
1201                                        printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
1202#endif
1203                                        info->xmit_count += cc;
1204                                } else
1205#endif
1206                                        info->xmit_count += c;
1207                } else {
1208                        info->msr |= UART_MSR_CTS;
1209                        info->lsr |= UART_LSR_TEMT;
1210                        if (info->dialing) {
1211                                info->dialing = 0;
1212#ifdef ISDN_DEBUG_MODEM_HUP
1213                                printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
1214#endif
1215                                isdn_tty_modem_result(RESULT_NO_CARRIER, info);
1216                                isdn_tty_modem_hup(info, 1);
1217                        } else
1218                                c = isdn_tty_edit_at(buf, c, info);
1219                }
1220                buf += c;
1221                count -= c;
1222                total += c;
1223        }
1224        atomic_dec(&info->xmit_lock);
1225        if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
1226                if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
1227                        isdn_tty_senddown(info);
1228                        isdn_tty_tint(info);
1229                }
1230                isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1231        }
1232        return total;
1233}
1234
1235static int
1236isdn_tty_write_room(struct tty_struct *tty)
1237{
1238        modem_info *info = (modem_info *) tty->driver_data;
1239        int ret;
1240
1241        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write_room"))
1242                return 0;
1243        if (!info->online)
1244                return info->xmit_size;
1245        ret = info->xmit_size - info->xmit_count;
1246        return (ret < 0) ? 0 : ret;
1247}
1248
1249static int
1250isdn_tty_chars_in_buffer(struct tty_struct *tty)
1251{
1252        modem_info *info = (modem_info *) tty->driver_data;
1253
1254        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_chars_in_buffer"))
1255                return 0;
1256        if (!info->online)
1257                return 0;
1258        return (info->xmit_count);
1259}
1260
1261static void
1262isdn_tty_flush_buffer(struct tty_struct *tty)
1263{
1264        modem_info *info;
1265
1266        if (!tty) {
1267                return;
1268        }
1269        info = (modem_info *) tty->driver_data;
1270        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
1271                return;
1272        }
1273        isdn_tty_cleanup_xmit(info);
1274        info->xmit_count = 0;
1275        tty_wakeup(tty);
1276}
1277
1278static void
1279isdn_tty_flush_chars(struct tty_struct *tty)
1280{
1281        modem_info *info = (modem_info *) tty->driver_data;
1282
1283        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
1284                return;
1285        if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
1286                isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1287}
1288
1289/*
1290 * ------------------------------------------------------------
1291 * isdn_tty_throttle()
1292 *
1293 * This routine is called by the upper-layer tty layer to signal that
1294 * incoming characters should be throttled.
1295 * ------------------------------------------------------------
1296 */
1297static void
1298isdn_tty_throttle(struct tty_struct *tty)
1299{
1300        modem_info *info = (modem_info *) tty->driver_data;
1301
1302        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_throttle"))
1303                return;
1304        if (I_IXOFF(tty))
1305                info->x_char = STOP_CHAR(tty);
1306        info->mcr &= ~UART_MCR_RTS;
1307}
1308
1309static void
1310isdn_tty_unthrottle(struct tty_struct *tty)
1311{
1312        modem_info *info = (modem_info *) tty->driver_data;
1313
1314        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_unthrottle"))
1315                return;
1316        if (I_IXOFF(tty)) {
1317                if (info->x_char)
1318                        info->x_char = 0;
1319                else
1320                        info->x_char = START_CHAR(tty);
1321        }
1322        info->mcr |= UART_MCR_RTS;
1323}
1324
1325/*
1326 * ------------------------------------------------------------
1327 * isdn_tty_ioctl() and friends
1328 * ------------------------------------------------------------
1329 */
1330
1331/*
1332 * isdn_tty_get_lsr_info - get line status register info
1333 *
1334 * Purpose: Let user call ioctl() to get info when the UART physically
1335 *          is emptied.  On bus types like RS485, the transmitter must
1336 *          release the bus after transmitting. This must be done when
1337 *          the transmit shift register is empty, not be done when the
1338 *          transmit holding register is empty.  This functionality
1339 *          allows RS485 driver to be written in user space.
1340 */
1341static int
1342isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
1343{
1344        u_char status;
1345        uint result;
1346
1347        status = info->lsr;
1348        result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1349        return put_user(result, value);
1350}
1351
1352
1353static int
1354isdn_tty_tiocmget(struct tty_struct *tty)
1355{
1356        modem_info *info = (modem_info *) tty->driver_data;
1357        u_char control, status;
1358
1359        if (isdn_tty_paranoia_check(info, tty->name, __func__))
1360                return -ENODEV;
1361        if (tty->flags & (1 << TTY_IO_ERROR))
1362                return -EIO;
1363
1364        mutex_lock(&modem_info_mutex);
1365#ifdef ISDN_DEBUG_MODEM_IOCTL
1366        printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
1367#endif
1368
1369        control = info->mcr;
1370        status = info->msr;
1371        mutex_unlock(&modem_info_mutex);
1372        return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1373                | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1374                | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1375                | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1376                | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1377                | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1378}
1379
1380static int
1381isdn_tty_tiocmset(struct tty_struct *tty,
1382                  unsigned int set, unsigned int clear)
1383{
1384        modem_info *info = (modem_info *) tty->driver_data;
1385
1386        if (isdn_tty_paranoia_check(info, tty->name, __func__))
1387                return -ENODEV;
1388        if (tty->flags & (1 << TTY_IO_ERROR))
1389                return -EIO;
1390
1391#ifdef ISDN_DEBUG_MODEM_IOCTL
1392        printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
1393#endif
1394
1395        mutex_lock(&modem_info_mutex);
1396        if (set & TIOCM_RTS)
1397                info->mcr |= UART_MCR_RTS;
1398        if (set & TIOCM_DTR) {
1399                info->mcr |= UART_MCR_DTR;
1400                isdn_tty_modem_ncarrier(info);
1401        }
1402
1403        if (clear & TIOCM_RTS)
1404                info->mcr &= ~UART_MCR_RTS;
1405        if (clear & TIOCM_DTR) {
1406                info->mcr &= ~UART_MCR_DTR;
1407                if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1408                        isdn_tty_modem_reset_regs(info, 0);
1409#ifdef ISDN_DEBUG_MODEM_HUP
1410                        printk(KERN_DEBUG "Mhup in TIOCMSET\n");
1411#endif
1412                        if (info->online)
1413                                info->ncarrier = 1;
1414                        isdn_tty_modem_hup(info, 1);
1415                }
1416        }
1417        mutex_unlock(&modem_info_mutex);
1418        return 0;
1419}
1420
1421static int
1422isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
1423{
1424        modem_info *info = (modem_info *) tty->driver_data;
1425        int retval;
1426
1427        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
1428                return -ENODEV;
1429        if (tty->flags & (1 << TTY_IO_ERROR))
1430                return -EIO;
1431        switch (cmd) {
1432        case TCSBRK:   /* SVID version: non-zero arg --> no break */
1433#ifdef ISDN_DEBUG_MODEM_IOCTL
1434                printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
1435#endif
1436                retval = tty_check_change(tty);
1437                if (retval)
1438                        return retval;
1439                tty_wait_until_sent(tty, 0);
1440                return 0;
1441        case TCSBRKP:  /* support for POSIX tcsendbreak() */
1442#ifdef ISDN_DEBUG_MODEM_IOCTL
1443                printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
1444#endif
1445                retval = tty_check_change(tty);
1446                if (retval)
1447                        return retval;
1448                tty_wait_until_sent(tty, 0);
1449                return 0;
1450        case TIOCSERGETLSR:     /* Get line status register */
1451#ifdef ISDN_DEBUG_MODEM_IOCTL
1452                printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
1453#endif
1454                return isdn_tty_get_lsr_info(info, (uint __user *) arg);
1455        default:
1456#ifdef ISDN_DEBUG_MODEM_IOCTL
1457                printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
1458#endif
1459                return -ENOIOCTLCMD;
1460        }
1461        return 0;
1462}
1463
1464static void
1465isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1466{
1467        modem_info *info = (modem_info *) tty->driver_data;
1468
1469        if (!old_termios)
1470                isdn_tty_change_speed(info);
1471        else {
1472                if (tty->termios->c_cflag == old_termios->c_cflag &&
1473                    tty->termios->c_ispeed == old_termios->c_ispeed &&
1474                    tty->termios->c_ospeed == old_termios->c_ospeed)
1475                        return;
1476                isdn_tty_change_speed(info);
1477                if ((old_termios->c_cflag & CRTSCTS) &&
1478                    !(tty->termios->c_cflag & CRTSCTS))
1479                        tty->hw_stopped = 0;
1480        }
1481}
1482
1483/*
1484 * ------------------------------------------------------------
1485 * isdn_tty_open() and friends
1486 * ------------------------------------------------------------
1487 */
1488
1489/*
1490 * This routine is called whenever a serial port is opened.  It
1491 * enables interrupts for a serial port, linking in its async structure into
1492 * the IRQ chain.   It also performs the serial-specific
1493 * initialization for the tty structure.
1494 */
1495static int
1496isdn_tty_open(struct tty_struct *tty, struct file *filp)
1497{
1498        struct tty_port *port;
1499        modem_info *info;
1500        int retval;
1501
1502        info = &dev->mdm.info[tty->index];
1503        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_open"))
1504                return -ENODEV;
1505        port = &info->port;
1506#ifdef ISDN_DEBUG_MODEM_OPEN
1507        printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
1508               port->count);
1509#endif
1510        port->count++;
1511        tty->driver_data = info;
1512        port->tty = tty;
1513        tty->port = port;
1514        /*
1515         * Start up serial port
1516         */
1517        retval = isdn_tty_startup(info);
1518        if (retval) {
1519#ifdef ISDN_DEBUG_MODEM_OPEN
1520                printk(KERN_DEBUG "isdn_tty_open return after startup\n");
1521#endif
1522                return retval;
1523        }
1524        retval = tty_port_block_til_ready(port, tty, filp);
1525        if (retval) {
1526#ifdef ISDN_DEBUG_MODEM_OPEN
1527                printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
1528#endif
1529                return retval;
1530        }
1531#ifdef ISDN_DEBUG_MODEM_OPEN
1532        printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
1533#endif
1534        dev->modempoll++;
1535#ifdef ISDN_DEBUG_MODEM_OPEN
1536        printk(KERN_DEBUG "isdn_tty_open normal exit\n");
1537#endif
1538        return 0;
1539}
1540
1541static void
1542isdn_tty_close(struct tty_struct *tty, struct file *filp)
1543{
1544        modem_info *info = (modem_info *) tty->driver_data;
1545        struct tty_port *port = &info->port;
1546        ulong timeout;
1547
1548        if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
1549                return;
1550        if (tty_hung_up_p(filp)) {
1551#ifdef ISDN_DEBUG_MODEM_OPEN
1552                printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
1553#endif
1554                return;
1555        }
1556        if ((tty->count == 1) && (port->count != 1)) {
1557                /*
1558                 * Uh, oh.  tty->count is 1, which means that the tty
1559                 * structure will be freed.  Info->count should always
1560                 * be one in these conditions.  If it's greater than
1561                 * one, we've got real problems, since it means the
1562                 * serial port won't be shutdown.
1563                 */
1564                printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
1565                       "info->count is %d\n", port->count);
1566                port->count = 1;
1567        }
1568        if (--port->count < 0) {
1569                printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
1570                       info->line, port->count);
1571                port->count = 0;
1572        }
1573        if (port->count) {
1574#ifdef ISDN_DEBUG_MODEM_OPEN
1575                printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
1576#endif
1577                return;
1578        }
1579        port->flags |= ASYNC_CLOSING;
1580
1581        tty->closing = 1;
1582        /*
1583         * At this point we stop accepting input.  To do this, we
1584         * disable the receive line status interrupts, and tell the
1585         * interrupt driver to stop checking the data ready bit in the
1586         * line status register.
1587         */
1588        if (port->flags & ASYNC_INITIALIZED) {
1589                tty_wait_until_sent_from_close(tty, 3000);      /* 30 seconds timeout */
1590                /*
1591                 * Before we drop DTR, make sure the UART transmitter
1592                 * has completely drained; this is especially
1593                 * important if there is a transmit FIFO!
1594                 */
1595                timeout = jiffies + HZ;
1596                while (!(info->lsr & UART_LSR_TEMT)) {
1597                        schedule_timeout_interruptible(20);
1598                        if (time_after(jiffies, timeout))
1599                                break;
1600                }
1601        }
1602        dev->modempoll--;
1603        isdn_tty_shutdown(info);
1604        isdn_tty_flush_buffer(tty);
1605        tty_ldisc_flush(tty);
1606        port->tty = NULL;
1607        info->ncarrier = 0;
1608
1609        tty_port_close_end(port, tty);
1610#ifdef ISDN_DEBUG_MODEM_OPEN
1611        printk(KERN_DEBUG "isdn_tty_close normal exit\n");
1612#endif
1613}
1614
1615/*
1616 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1617 */
1618static void
1619isdn_tty_hangup(struct tty_struct *tty)
1620{
1621        modem_info *info = (modem_info *) tty->driver_data;
1622        struct tty_port *port = &info->port;
1623
1624        if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
1625                return;
1626        isdn_tty_shutdown(info);
1627        port->count = 0;
1628        port->flags &= ~ASYNC_NORMAL_ACTIVE;
1629        port->tty = NULL;
1630        wake_up_interruptible(&port->open_wait);
1631}
1632
1633/* This routine initializes all emulator-data.
1634 */
1635static void
1636isdn_tty_reset_profile(atemu *m)
1637{
1638        m->profile[0] = 0;
1639        m->profile[1] = 0;
1640        m->profile[2] = 43;
1641        m->profile[3] = 13;
1642        m->profile[4] = 10;
1643        m->profile[5] = 8;
1644        m->profile[6] = 3;
1645        m->profile[7] = 60;
1646        m->profile[8] = 2;
1647        m->profile[9] = 6;
1648        m->profile[10] = 7;
1649        m->profile[11] = 70;
1650        m->profile[12] = 0x45;
1651        m->profile[13] = 4;
1652        m->profile[14] = ISDN_PROTO_L2_X75I;
1653        m->profile[15] = ISDN_PROTO_L3_TRANS;
1654        m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
1655        m->profile[17] = ISDN_MODEM_WINSIZE;
1656        m->profile[18] = 4;
1657        m->profile[19] = 0;
1658        m->profile[20] = 0;
1659        m->profile[23] = 0;
1660        m->pmsn[0] = '\0';
1661        m->plmsn[0] = '\0';
1662}
1663
1664#ifdef CONFIG_ISDN_AUDIO
1665static void
1666isdn_tty_modem_reset_vpar(atemu *m)
1667{
1668        m->vpar[0] = 2;         /* Voice-device            (2 = phone line) */
1669        m->vpar[1] = 0;         /* Silence detection level (0 = none      ) */
1670        m->vpar[2] = 70;        /* Silence interval        (7 sec.        ) */
1671        m->vpar[3] = 2;         /* Compression type        (1 = ADPCM-2   ) */
1672        m->vpar[4] = 0;         /* DTMF detection level    (0 = softcode  ) */
1673        m->vpar[5] = 8;         /* DTMF interval           (8 * 5 ms.     ) */
1674}
1675#endif
1676
1677#ifdef CONFIG_ISDN_TTY_FAX
1678static void
1679isdn_tty_modem_reset_faxpar(modem_info *info)
1680{
1681        T30_s *f = info->fax;
1682
1683        f->code = 0;
1684        f->phase = ISDN_FAX_PHASE_IDLE;
1685        f->direction = 0;
1686        f->resolution = 1;      /* fine */
1687        f->rate = 5;            /* 14400 bit/s */
1688        f->width = 0;
1689        f->length = 0;
1690        f->compression = 0;
1691        f->ecm = 0;
1692        f->binary = 0;
1693        f->scantime = 0;
1694        memset(&f->id[0], 32, FAXIDLEN - 1);
1695        f->id[FAXIDLEN - 1] = 0;
1696        f->badlin = 0;
1697        f->badmul = 0;
1698        f->bor = 0;
1699        f->nbc = 0;
1700        f->cq = 0;
1701        f->cr = 0;
1702        f->ctcrty = 0;
1703        f->minsp = 0;
1704        f->phcto = 30;
1705        f->rel = 0;
1706        memset(&f->pollid[0], 32, FAXIDLEN - 1);
1707        f->pollid[FAXIDLEN - 1] = 0;
1708}
1709#endif
1710
1711static void
1712isdn_tty_modem_reset_regs(modem_info *info, int force)
1713{
1714        atemu *m = &info->emu;
1715        if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
1716                memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
1717                memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
1718                memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
1719                info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
1720        }
1721#ifdef CONFIG_ISDN_AUDIO
1722        isdn_tty_modem_reset_vpar(m);
1723#endif
1724#ifdef CONFIG_ISDN_TTY_FAX
1725        isdn_tty_modem_reset_faxpar(info);
1726#endif
1727        m->mdmcmdl = 0;
1728}
1729
1730static void
1731modem_write_profile(atemu *m)
1732{
1733        memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
1734        memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
1735        memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
1736        if (dev->profd)
1737                send_sig(SIGIO, dev->profd, 1);
1738}
1739
1740static const struct tty_operations modem_ops = {
1741        .open = isdn_tty_open,
1742        .close = isdn_tty_close,
1743        .write = isdn_tty_write,
1744        .flush_chars = isdn_tty_flush_chars,
1745        .write_room = isdn_tty_write_room,
1746        .chars_in_buffer = isdn_tty_chars_in_buffer,
1747        .flush_buffer = isdn_tty_flush_buffer,
1748        .ioctl = isdn_tty_ioctl,
1749        .throttle = isdn_tty_throttle,
1750        .unthrottle = isdn_tty_unthrottle,
1751        .set_termios = isdn_tty_set_termios,
1752        .hangup = isdn_tty_hangup,
1753        .tiocmget = isdn_tty_tiocmget,
1754        .tiocmset = isdn_tty_tiocmset,
1755};
1756
1757static int isdn_tty_carrier_raised(struct tty_port *port)
1758{
1759        modem_info *info = container_of(port, modem_info, port);
1760        return info->msr & UART_MSR_DCD;
1761}
1762
1763static const struct tty_port_operations isdn_tty_port_ops = {
1764        .carrier_raised = isdn_tty_carrier_raised,
1765};
1766
1767int
1768isdn_tty_modem_init(void)
1769{
1770        isdn_modem_t    *m;
1771        int             i, retval;
1772        modem_info      *info;
1773
1774        m = &dev->mdm;
1775        m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
1776        if (!m->tty_modem)
1777                return -ENOMEM;
1778        m->tty_modem->name = "ttyI";
1779        m->tty_modem->major = ISDN_TTY_MAJOR;
1780        m->tty_modem->minor_start = 0;
1781        m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
1782        m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
1783        m->tty_modem->init_termios = tty_std_termios;
1784        m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1785        m->tty_modem->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1786        m->tty_modem->driver_name = "isdn_tty";
1787        tty_set_operations(m->tty_modem, &modem_ops);
1788        retval = tty_register_driver(m->tty_modem);
1789        if (retval) {
1790                printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
1791                goto err;
1792        }
1793        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1794                info = &m->info[i];
1795#ifdef CONFIG_ISDN_TTY_FAX
1796                if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
1797                        printk(KERN_ERR "Could not allocate fax t30-buffer\n");
1798                        retval = -ENOMEM;
1799                        goto err_unregister;
1800                }
1801#endif
1802                tty_port_init(&info->port);
1803                info->port.ops = &isdn_tty_port_ops;
1804                spin_lock_init(&info->readlock);
1805                sprintf(info->last_cause, "0000");
1806                sprintf(info->last_num, "none");
1807                info->last_dir = 0;
1808                info->last_lhup = 1;
1809                info->last_l2 = -1;
1810                info->last_si = 0;
1811                isdn_tty_reset_profile(&info->emu);
1812                isdn_tty_modem_reset_regs(info, 1);
1813                info->magic = ISDN_ASYNC_MAGIC;
1814                info->line = i;
1815                info->x_char = 0;
1816                info->isdn_driver = -1;
1817                info->isdn_channel = -1;
1818                info->drv_index = -1;
1819                info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
1820                init_timer(&info->nc_timer);
1821                info->nc_timer.function = isdn_tty_modem_do_ncarrier;
1822                info->nc_timer.data = (unsigned long) info;
1823                skb_queue_head_init(&info->xmit_queue);
1824#ifdef CONFIG_ISDN_AUDIO
1825                skb_queue_head_init(&info->dtmf_queue);
1826#endif
1827                info->port.xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5,
1828                                GFP_KERNEL);
1829                if (!info->port.xmit_buf) {
1830                        printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
1831                        retval = -ENOMEM;
1832                        goto err_unregister;
1833                }
1834                /* Make room for T.70 header */
1835                info->port.xmit_buf += 4;
1836        }
1837        return 0;
1838err_unregister:
1839        for (i--; i >= 0; i--) {
1840                info = &m->info[i];
1841#ifdef CONFIG_ISDN_TTY_FAX
1842                kfree(info->fax);
1843#endif
1844                kfree(info->port.xmit_buf - 4);
1845        }
1846        tty_unregister_driver(m->tty_modem);
1847err:
1848        put_tty_driver(m->tty_modem);
1849        m->tty_modem = NULL;
1850        return retval;
1851}
1852
1853void
1854isdn_tty_exit(void)
1855{
1856        modem_info *info;
1857        int i;
1858
1859        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1860                info = &dev->mdm.info[i];
1861                isdn_tty_cleanup_xmit(info);
1862#ifdef CONFIG_ISDN_TTY_FAX
1863                kfree(info->fax);
1864#endif
1865                kfree(info->port.xmit_buf - 4);
1866        }
1867        tty_unregister_driver(dev->mdm.tty_modem);
1868        put_tty_driver(dev->mdm.tty_modem);
1869        dev->mdm.tty_modem = NULL;
1870}
1871
1872
1873/*
1874 * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
1875 *      match the MSN against the MSNs (glob patterns) defined for tty_emulator,
1876 *      and return 0 for match, 1 for no match, 2 if MSN could match if longer.
1877 */
1878
1879static int
1880isdn_tty_match_icall(char *cid, atemu *emu, int di)
1881{
1882#ifdef ISDN_DEBUG_MODEM_ICALL
1883        printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
1884               emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
1885               emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
1886#endif
1887        if (strlen(emu->lmsn)) {
1888                char *p = emu->lmsn;
1889                char *q;
1890                int  tmp;
1891                int  ret = 0;
1892
1893                while (1) {
1894                        if ((q = strchr(p, ';')))
1895                                *q = '\0';
1896                        if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
1897                                ret = tmp;
1898#ifdef ISDN_DEBUG_MODEM_ICALL
1899                        printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
1900                               p, isdn_map_eaz2msn(emu->msn, di), tmp);
1901#endif
1902                        if (q) {
1903                                *q = ';';
1904                                p = q;
1905                                p++;
1906                        }
1907                        if (!tmp)
1908                                return 0;
1909                        if (!q)
1910                                break;
1911                }
1912                return ret;
1913        } else {
1914                int tmp;
1915                tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
1916#ifdef ISDN_DEBUG_MODEM_ICALL
1917                printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
1918                       isdn_map_eaz2msn(emu->msn, di), tmp);
1919#endif
1920                return tmp;
1921        }
1922}
1923
1924/*
1925 * An incoming call-request has arrived.
1926 * Search the tty-devices for an appropriate device and bind
1927 * it to the ISDN-Channel.
1928 * Return:
1929 *
1930 *  0 = No matching device found.
1931 *  1 = A matching device found.
1932 *  3 = No match found, but eventually would match, if
1933 *      CID is longer.
1934 */
1935int
1936isdn_tty_find_icall(int di, int ch, setup_parm *setup)
1937{
1938        char *eaz;
1939        int i;
1940        int wret;
1941        int idx;
1942        int si1;
1943        int si2;
1944        char *nr;
1945        ulong flags;
1946
1947        if (!setup->phone[0]) {
1948                nr = "0";
1949                printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
1950        } else
1951                nr = setup->phone;
1952        si1 = (int) setup->si1;
1953        si2 = (int) setup->si2;
1954        if (!setup->eazmsn[0]) {
1955                printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
1956                eaz = "0";
1957        } else
1958                eaz = setup->eazmsn;
1959#ifdef ISDN_DEBUG_MODEM_ICALL
1960        printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
1961#endif
1962        wret = 0;
1963        spin_lock_irqsave(&dev->lock, flags);
1964        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1965                modem_info *info = &dev->mdm.info[i];
1966
1967                if (info->port.count == 0)
1968                        continue;
1969                if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) &&  /* SI1 is matching */
1970                    (info->emu.mdmreg[REG_SI2] == si2)) {         /* SI2 is matching */
1971                        idx = isdn_dc2minor(di, ch);
1972#ifdef ISDN_DEBUG_MODEM_ICALL
1973                        printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
1974                        printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
1975                               info->port.flags, info->isdn_driver,
1976                               info->isdn_channel, dev->usage[idx]);
1977#endif
1978                        if (
1979#ifndef FIX_FILE_TRANSFER
1980                                (info->port.flags & ASYNC_NORMAL_ACTIVE) &&
1981#endif
1982                                (info->isdn_driver == -1) &&
1983                                (info->isdn_channel == -1) &&
1984                                (USG_NONE(dev->usage[idx]))) {
1985                                int matchret;
1986
1987                                if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
1988                                        wret = matchret;
1989                                if (!matchret) {                  /* EAZ is matching */
1990                                        info->isdn_driver = di;
1991                                        info->isdn_channel = ch;
1992                                        info->drv_index = idx;
1993                                        dev->m_idx[idx] = info->line;
1994                                        dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
1995                                        dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
1996                                        strcpy(dev->num[idx], nr);
1997                                        strcpy(info->emu.cpn, eaz);
1998                                        info->emu.mdmreg[REG_SI1I] = si2bit[si1];
1999                                        info->emu.mdmreg[REG_PLAN] = setup->plan;
2000                                        info->emu.mdmreg[REG_SCREEN] = setup->screen;
2001                                        isdn_info_update();
2002                                        spin_unlock_irqrestore(&dev->lock, flags);
2003                                        printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2004                                               info->line);
2005                                        info->msr |= UART_MSR_RI;
2006                                        isdn_tty_modem_result(RESULT_RING, info);
2007                                        isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2008                                        return 1;
2009                                }
2010                        }
2011                }
2012        }
2013        spin_unlock_irqrestore(&dev->lock, flags);
2014        printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
2015               ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
2016        return (wret == 2) ? 3 : 0;
2017}
2018
2019#define TTY_IS_ACTIVE(info)     (info->port.flags & ASYNC_NORMAL_ACTIVE)
2020
2021int
2022isdn_tty_stat_callback(int i, isdn_ctrl *c)
2023{
2024        int mi;
2025        modem_info *info;
2026        char *e;
2027
2028        if (i < 0)
2029                return 0;
2030        if ((mi = dev->m_idx[i]) >= 0) {
2031                info = &dev->mdm.info[mi];
2032                switch (c->command) {
2033                case ISDN_STAT_CINF:
2034                        printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2035                        info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2036                        if (e == (char *)c->parm.num)
2037                                info->emu.charge = 0;
2038
2039                        break;
2040                case ISDN_STAT_BSENT:
2041#ifdef ISDN_TTY_STAT_DEBUG
2042                        printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
2043#endif
2044                        if ((info->isdn_driver == c->driver) &&
2045                            (info->isdn_channel == c->arg)) {
2046                                info->msr |= UART_MSR_CTS;
2047                                if (info->send_outstanding)
2048                                        if (!(--info->send_outstanding))
2049                                                info->lsr |= UART_LSR_TEMT;
2050                                isdn_tty_tint(info);
2051                                return 1;
2052                        }
2053                        break;
2054                case ISDN_STAT_CAUSE:
2055#ifdef ISDN_TTY_STAT_DEBUG
2056                        printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2057#endif
2058                        /* Signal cause to tty-device */
2059                        strncpy(info->last_cause, c->parm.num, 5);
2060                        return 1;
2061                case ISDN_STAT_DISPLAY:
2062#ifdef ISDN_TTY_STAT_DEBUG
2063                        printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2064#endif
2065                        /* Signal display to tty-device */
2066                        if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2067                            !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2068                                isdn_tty_at_cout("\r\n", info);
2069                                isdn_tty_at_cout("DISPLAY: ", info);
2070                                isdn_tty_at_cout(c->parm.display, info);
2071                                isdn_tty_at_cout("\r\n", info);
2072                        }
2073                        return 1;
2074                case ISDN_STAT_DCONN:
2075#ifdef ISDN_TTY_STAT_DEBUG
2076                        printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2077#endif
2078                        if (TTY_IS_ACTIVE(info)) {
2079                                if (info->dialing == 1) {
2080                                        info->dialing = 2;
2081                                        return 1;
2082                                }
2083                        }
2084                        break;
2085                case ISDN_STAT_DHUP:
2086#ifdef ISDN_TTY_STAT_DEBUG
2087                        printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2088#endif
2089                        if (TTY_IS_ACTIVE(info)) {
2090                                if (info->dialing == 1)
2091                                        isdn_tty_modem_result(RESULT_BUSY, info);
2092                                if (info->dialing > 1)
2093                                        isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2094                                info->dialing = 0;
2095#ifdef ISDN_DEBUG_MODEM_HUP
2096                                printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2097#endif
2098                                isdn_tty_modem_hup(info, 0);
2099                                return 1;
2100                        }
2101                        break;
2102                case ISDN_STAT_BCONN:
2103#ifdef ISDN_TTY_STAT_DEBUG
2104                        printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2105#endif
2106                        /* Wake up any processes waiting
2107                         * for incoming call of this device when
2108                         * DCD follow the state of incoming carrier
2109                         */
2110                        if (info->port.blocked_open &&
2111                            (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
2112                                wake_up_interruptible(&info->port.open_wait);
2113                        }
2114
2115                        /* Schedule CONNECT-Message to any tty
2116                         * waiting for it and
2117                         * set DCD-bit of its modem-status.
2118                         */
2119                        if (TTY_IS_ACTIVE(info) ||
2120                            (info->port.blocked_open &&
2121                             (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
2122                                info->msr |= UART_MSR_DCD;
2123                                info->emu.charge = 0;
2124                                if (info->dialing & 0xf)
2125                                        info->last_dir = 1;
2126                                else
2127                                        info->last_dir = 0;
2128                                info->dialing = 0;
2129                                info->rcvsched = 1;
2130                                if (USG_MODEM(dev->usage[i])) {
2131                                        if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2132                                                strcpy(info->emu.connmsg, c->parm.num);
2133                                                isdn_tty_modem_result(RESULT_CONNECT, info);
2134                                        } else
2135                                                isdn_tty_modem_result(RESULT_CONNECT64000, info);
2136                                }
2137                                if (USG_VOICE(dev->usage[i]))
2138                                        isdn_tty_modem_result(RESULT_VCON, info);
2139                                return 1;
2140                        }
2141                        break;
2142                case ISDN_STAT_BHUP:
2143#ifdef ISDN_TTY_STAT_DEBUG
2144                        printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2145#endif
2146                        if (TTY_IS_ACTIVE(info)) {
2147#ifdef ISDN_DEBUG_MODEM_HUP
2148                                printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2149#endif
2150                                isdn_tty_modem_hup(info, 0);
2151                                return 1;
2152                        }
2153                        break;
2154                case ISDN_STAT_NODCH:
2155#ifdef ISDN_TTY_STAT_DEBUG
2156                        printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2157#endif
2158                        if (TTY_IS_ACTIVE(info)) {
2159                                if (info->dialing) {
2160                                        info->dialing = 0;
2161                                        info->last_l2 = -1;
2162                                        info->last_si = 0;
2163                                        sprintf(info->last_cause, "0000");
2164                                        isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2165                                }
2166                                isdn_tty_modem_hup(info, 0);
2167                                return 1;
2168                        }
2169                        break;
2170                case ISDN_STAT_UNLOAD:
2171#ifdef ISDN_TTY_STAT_DEBUG
2172                        printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2173#endif
2174                        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2175                                info = &dev->mdm.info[i];
2176                                if (info->isdn_driver == c->driver) {
2177                                        if (info->online)
2178                                                isdn_tty_modem_hup(info, 1);
2179                                }
2180                        }
2181                        return 1;
2182#ifdef CONFIG_ISDN_TTY_FAX
2183                case ISDN_STAT_FAXIND:
2184                        if (TTY_IS_ACTIVE(info)) {
2185                                isdn_tty_fax_command(info, c);
2186                        }
2187                        break;
2188#endif
2189#ifdef CONFIG_ISDN_AUDIO
2190                case ISDN_STAT_AUDIO:
2191                        if (TTY_IS_ACTIVE(info)) {
2192                                switch (c->parm.num[0]) {
2193                                case ISDN_AUDIO_DTMF:
2194                                        if (info->vonline) {
2195                                                isdn_audio_put_dle_code(info,
2196                                                                        c->parm.num[1]);
2197                                        }
2198                                        break;
2199                                }
2200                        }
2201                        break;
2202#endif
2203                }
2204        }
2205        return 0;
2206}
2207
2208/*********************************************************************
2209 Modem-Emulator-Routines
2210*********************************************************************/
2211
2212#define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
2213
2214/*
2215 * Put a message from the AT-emulator into receive-buffer of tty,
2216 * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2217 */
2218void
2219isdn_tty_at_cout(char *msg, modem_info *info)
2220{
2221        struct tty_struct *tty;
2222        atemu *m = &info->emu;
2223        char *p;
2224        char c;
2225        u_long flags;
2226        struct sk_buff *skb = NULL;
2227        char *sp = NULL;
2228        int l;
2229
2230        if (!msg) {
2231                printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2232                return;
2233        }
2234
2235        l = strlen(msg);
2236
2237        spin_lock_irqsave(&info->readlock, flags);
2238        tty = info->port.tty;
2239        if ((info->port.flags & ASYNC_CLOSING) || (!tty)) {
2240                spin_unlock_irqrestore(&info->readlock, flags);
2241                return;
2242        }
2243
2244        /* use queue instead of direct, if online and */
2245        /* data is in queue or buffer is full */
2246        if (info->online && ((tty_buffer_request_room(tty, l) < l) ||
2247                             !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
2248                skb = alloc_skb(l, GFP_ATOMIC);
2249                if (!skb) {
2250                        spin_unlock_irqrestore(&info->readlock, flags);
2251                        return;
2252                }
2253                sp = skb_put(skb, l);
2254#ifdef CONFIG_ISDN_AUDIO
2255                ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2256                ISDN_AUDIO_SKB_LOCK(skb) = 0;
2257#endif
2258        }
2259
2260        for (p = msg; *p; p++) {
2261                switch (*p) {
2262                case '\r':
2263                        c = m->mdmreg[REG_CR];
2264                        break;
2265                case '\n':
2266                        c = m->mdmreg[REG_LF];
2267                        break;
2268                case '\b':
2269                        c = m->mdmreg[REG_BS];
2270                        break;
2271                default:
2272                        c = *p;
2273                }
2274                if (skb) {
2275                        *sp++ = c;
2276                } else {
2277                        if (tty_insert_flip_char(tty, c, TTY_NORMAL) == 0)
2278                                break;
2279                }
2280        }
2281        if (skb) {
2282                __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2283                dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2284                spin_unlock_irqrestore(&info->readlock, flags);
2285                /* Schedule dequeuing */
2286                if (dev->modempoll && info->rcvsched)
2287                        isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2288
2289        } else {
2290                spin_unlock_irqrestore(&info->readlock, flags);
2291                tty_flip_buffer_push(tty);
2292        }
2293}
2294
2295/*
2296 * Perform ATH Hangup
2297 */
2298static void
2299isdn_tty_on_hook(modem_info *info)
2300{
2301        if (info->isdn_channel >= 0) {
2302#ifdef ISDN_DEBUG_MODEM_HUP
2303                printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2304#endif
2305                isdn_tty_modem_hup(info, 1);
2306        }
2307}
2308
2309static void
2310isdn_tty_off_hook(void)
2311{
2312        printk(KERN_DEBUG "isdn_tty_off_hook\n");
2313}
2314
2315#define PLUSWAIT1 (HZ / 2)      /* 0.5 sec. */
2316#define PLUSWAIT2 (HZ * 3 / 2)  /* 1.5 sec */
2317
2318/*
2319 * Check Buffer for Modem-escape-sequence, activate timer-callback to
2320 * isdn_tty_modem_escape() if sequence found.
2321 *
2322 * Parameters:
2323 *   p          pointer to databuffer
2324 *   plus       escape-character
2325 *   count      length of buffer
2326 *   pluscount  count of valid escape-characters so far
2327 *   lastplus   timestamp of last character
2328 */
2329static void
2330isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
2331                   u_long *lastplus)
2332{
2333        if (plus > 127)
2334                return;
2335        if (count > 3) {
2336                p += count - 3;
2337                count = 3;
2338                *pluscount = 0;
2339        }
2340        while (count > 0) {
2341                if (*(p++) == plus) {
2342                        if ((*pluscount)++) {
2343                                /* Time since last '+' > 0.5 sec. ? */
2344                                if (time_after(jiffies, *lastplus + PLUSWAIT1))
2345                                        *pluscount = 1;
2346                        } else {
2347                                /* Time since last non-'+' < 1.5 sec. ? */
2348                                if (time_before(jiffies, *lastplus + PLUSWAIT2))
2349                                        *pluscount = 0;
2350                        }
2351                        if ((*pluscount == 3) && (count == 1))
2352                                isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2353                        if (*pluscount > 3)
2354                                *pluscount = 1;
2355                } else
2356                        *pluscount = 0;
2357                *lastplus = jiffies;
2358                count--;
2359        }
2360}
2361
2362/*
2363 * Return result of AT-emulator to tty-receive-buffer, depending on
2364 * modem-register 12, bit 0 and 1.
2365 * For CONNECT-messages also switch to online-mode.
2366 * For RING-message handle auto-ATA if register 0 != 0
2367 */
2368
2369static void
2370isdn_tty_modem_result(int code, modem_info *info)
2371{
2372        atemu *m = &info->emu;
2373        static char *msg[] =
2374                {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2375                 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2376                 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2377        char s[ISDN_MSNLEN + 10];
2378
2379        switch (code) {
2380        case RESULT_RING:
2381                m->mdmreg[REG_RINGCNT]++;
2382                if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2383                        /* Automatically accept incoming call */
2384                        isdn_tty_cmd_ATA(info);
2385                break;
2386        case RESULT_NO_CARRIER:
2387#ifdef ISDN_DEBUG_MODEM_HUP
2388                printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
2389                       (info->port.flags & ASYNC_CLOSING),
2390                       (!info->port.tty));
2391#endif
2392                m->mdmreg[REG_RINGCNT] = 0;
2393                del_timer(&info->nc_timer);
2394                info->ncarrier = 0;
2395                if ((info->port.flags & ASYNC_CLOSING) || (!info->port.tty))
2396                        return;
2397
2398#ifdef CONFIG_ISDN_AUDIO
2399                if (info->vonline & 1) {
2400#ifdef ISDN_DEBUG_MODEM_VOICE
2401                        printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
2402                               info->line);
2403#endif
2404                        /* voice-recording, add DLE-ETX */
2405                        isdn_tty_at_cout("\020\003", info);
2406                }
2407                if (info->vonline & 2) {
2408#ifdef ISDN_DEBUG_MODEM_VOICE
2409                        printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
2410                               info->line);
2411#endif
2412                        /* voice-playing, add DLE-DC4 */
2413                        isdn_tty_at_cout("\020\024", info);
2414                }
2415#endif
2416                break;
2417        case RESULT_CONNECT:
2418        case RESULT_CONNECT64000:
2419                sprintf(info->last_cause, "0000");
2420                if (!info->online)
2421                        info->online = 2;
2422                break;
2423        case RESULT_VCON:
2424#ifdef ISDN_DEBUG_MODEM_VOICE
2425                printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
2426                       info->line);
2427#endif
2428                sprintf(info->last_cause, "0000");
2429                if (!info->online)
2430                        info->online = 1;
2431                break;
2432        } /* switch (code) */
2433
2434        if (m->mdmreg[REG_RESP] & BIT_RESP) {
2435                /* Show results */
2436                if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
2437                        /* Show numeric results only */
2438                        sprintf(s, "\r\n%d\r\n", code);
2439                        isdn_tty_at_cout(s, info);
2440                } else {
2441                        if (code == RESULT_RING) {
2442                                /* return if "show RUNG" and ringcounter>1 */
2443                                if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
2444                                    (m->mdmreg[REG_RINGCNT] > 1))
2445                                        return;
2446                                /* print CID, _before_ _every_ ring */
2447                                if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
2448                                        isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
2449                                        isdn_tty_at_cout(dev->num[info->drv_index], info);
2450                                        if (m->mdmreg[REG_CDN] & BIT_CDN) {
2451                                                isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2452                                                isdn_tty_at_cout(info->emu.cpn, info);
2453                                        }
2454                                }
2455                        }
2456                        isdn_tty_at_cout("\r\n", info);
2457                        isdn_tty_at_cout(msg[code], info);
2458                        switch (code) {
2459                        case RESULT_CONNECT:
2460                                switch (m->mdmreg[REG_L2PROT]) {
2461                                case ISDN_PROTO_L2_MODEM:
2462                                        isdn_tty_at_cout(" ", info);
2463                                        isdn_tty_at_cout(m->connmsg, info);
2464                                        break;
2465                                }
2466                                break;
2467                        case RESULT_RING:
2468                                /* Append CPN, if enabled */
2469                                if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
2470                                        sprintf(s, "/%s", m->cpn);
2471                                        isdn_tty_at_cout(s, info);
2472                                }
2473                                /* Print CID only once, _after_ 1st RING */
2474                                if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
2475                                    (m->mdmreg[REG_RINGCNT] == 1)) {
2476                                        isdn_tty_at_cout("\r\n", info);
2477                                        isdn_tty_at_cout("CALLER NUMBER: ", info);
2478                                        isdn_tty_at_cout(dev->num[info->drv_index], info);
2479                                        if (m->mdmreg[REG_CDN] & BIT_CDN) {
2480                                                isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2481                                                isdn_tty_at_cout(info->emu.cpn, info);
2482                                        }
2483                                }
2484                                break;
2485                        case RESULT_NO_CARRIER:
2486                        case RESULT_NO_DIALTONE:
2487                        case RESULT_BUSY:
2488                        case RESULT_NO_ANSWER:
2489                                m->mdmreg[REG_RINGCNT] = 0;
2490                                /* Append Cause-Message if enabled */
2491                                if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
2492                                        sprintf(s, "/%s", info->last_cause);
2493                                        isdn_tty_at_cout(s, info);
2494                                }
2495                                break;
2496                        case RESULT_CONNECT64000:
2497                                /* Append Protocol to CONNECT message */
2498                                switch (m->mdmreg[REG_L2PROT]) {
2499                                case ISDN_PROTO_L2_X75I:
2500                                case ISDN_PROTO_L2_X75UI:
2501                                case ISDN_PROTO_L2_X75BUI:
2502                                        isdn_tty_at_cout("/X.75", info);
2503                                        break;
2504                                case ISDN_PROTO_L2_HDLC:
2505                                        isdn_tty_at_cout("/HDLC", info);
2506                                        break;
2507                                case ISDN_PROTO_L2_V11096:
2508                                        isdn_tty_at_cout("/V110/9600", info);
2509                                        break;
2510                                case ISDN_PROTO_L2_V11019:
2511                                        isdn_tty_at_cout("/V110/19200", info);
2512                                        break;
2513                                case ISDN_PROTO_L2_V11038:
2514                                        isdn_tty_at_cout("/V110/38400", info);
2515                                        break;
2516                                }
2517                                if (m->mdmreg[REG_T70] & BIT_T70) {
2518                                        isdn_tty_at_cout("/T.70", info);
2519                                        if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2520                                                isdn_tty_at_cout("+", info);
2521                                }
2522                                break;
2523                        }
2524                        isdn_tty_at_cout("\r\n", info);
2525                }
2526        }
2527        if (code == RESULT_NO_CARRIER) {
2528                if ((info->port.flags & ASYNC_CLOSING) || (!info->port.tty))
2529                        return;
2530
2531                if (info->port.flags & ASYNC_CHECK_CD)
2532                        tty_hangup(info->port.tty);
2533        }
2534}
2535
2536
2537/*
2538 * Display a modem-register-value.
2539 */
2540static void
2541isdn_tty_show_profile(int ridx, modem_info *info)
2542{
2543        char v[6];
2544
2545        sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
2546        isdn_tty_at_cout(v, info);
2547}
2548
2549/*
2550 * Get MSN-string from char-pointer, set pointer to end of number
2551 */
2552static void
2553isdn_tty_get_msnstr(char *n, char **p)
2554{
2555        int limit = ISDN_MSNLEN - 1;
2556
2557        while (((*p[0] >= '0' && *p[0] <= '9') ||
2558                /* Why a comma ??? */
2559                (*p[0] == ',') || (*p[0] == ':')) &&
2560               (limit--))
2561                *n++ = *p[0]++;
2562        *n = '\0';
2563}
2564
2565/*
2566 * Get phone-number from modem-commandbuffer
2567 */
2568static void
2569isdn_tty_getdial(char *p, char *q, int cnt)
2570{
2571        int first = 1;
2572        int limit = ISDN_MSNLEN - 1;    /* MUST match the size of interface var to avoid
2573                                           buffer overflow */
2574
2575        while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
2576                if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
2577                    ((*p == 'R') && first) ||
2578                    (*p == '*') || (*p == '#')) {
2579                        *q++ = *p;
2580                        limit--;
2581                }
2582                if (!limit)
2583                        break;
2584                p++;
2585                first = 0;
2586        }
2587        *q = 0;
2588}
2589
2590#define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
2591#define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
2592
2593static void
2594isdn_tty_report(modem_info *info)
2595{
2596        atemu *m = &info->emu;
2597        char s[80];
2598
2599        isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
2600        sprintf(s, "    Remote Number:    %s\r\n", info->last_num);
2601        isdn_tty_at_cout(s, info);
2602        sprintf(s, "    Direction:        %s\r\n", info->last_dir ? "outgoing" : "incoming");
2603        isdn_tty_at_cout(s, info);
2604        isdn_tty_at_cout("    Layer-2 Protocol: ", info);
2605        switch (info->last_l2) {
2606        case ISDN_PROTO_L2_X75I:
2607                isdn_tty_at_cout("X.75i", info);
2608                break;
2609        case ISDN_PROTO_L2_X75UI:
2610                isdn_tty_at_cout("X.75ui", info);
2611                break;
2612        case ISDN_PROTO_L2_X75BUI:
2613                isdn_tty_at_cout("X.75bui", info);
2614                break;
2615        case ISDN_PROTO_L2_HDLC:
2616                isdn_tty_at_cout("HDLC", info);
2617                break;
2618        case ISDN_PROTO_L2_V11096:
2619                isdn_tty_at_cout("V.110 9600 Baud", info);
2620                break;
2621        case ISDN_PROTO_L2_V11019:
2622                isdn_tty_at_cout("V.110 19200 Baud", info);
2623                break;
2624        case ISDN_PROTO_L2_V11038:
2625                isdn_tty_at_cout("V.110 38400 Baud", info);
2626                break;
2627        case ISDN_PROTO_L2_TRANS:
2628                isdn_tty_at_cout("transparent", info);
2629                break;
2630        case ISDN_PROTO_L2_MODEM:
2631                isdn_tty_at_cout("modem", info);
2632                break;
2633        case ISDN_PROTO_L2_FAX:
2634                isdn_tty_at_cout("fax", info);
2635                break;
2636        default:
2637                isdn_tty_at_cout("unknown", info);
2638                break;
2639        }
2640        if (m->mdmreg[REG_T70] & BIT_T70) {
2641                isdn_tty_at_cout("/T.70", info);
2642                if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2643                        isdn_tty_at_cout("+", info);
2644        }
2645        isdn_tty_at_cout("\r\n", info);
2646        isdn_tty_at_cout("    Service:          ", info);
2647        switch (info->last_si) {
2648        case 1:
2649                isdn_tty_at_cout("audio\r\n", info);
2650                break;
2651        case 5:
2652                isdn_tty_at_cout("btx\r\n", info);
2653                break;
2654        case 7:
2655                isdn_tty_at_cout("data\r\n", info);
2656                break;
2657        default:
2658                sprintf(s, "%d\r\n", info->last_si);
2659                isdn_tty_at_cout(s, info);
2660                break;
2661        }
2662        sprintf(s, "    Hangup location:  %s\r\n", info->last_lhup ? "local" : "remote");
2663        isdn_tty_at_cout(s, info);
2664        sprintf(s, "    Last cause:       %s\r\n", info->last_cause);
2665        isdn_tty_at_cout(s, info);
2666}
2667
2668/*
2669 * Parse AT&.. commands.
2670 */
2671static int
2672isdn_tty_cmd_ATand(char **p, modem_info *info)
2673{
2674        atemu *m = &info->emu;
2675        int i;
2676        char rb[100];
2677
2678#define MAXRB (sizeof(rb) - 1)
2679
2680        switch (*p[0]) {
2681        case 'B':
2682                /* &B - Set Buffersize */
2683                p[0]++;
2684                i = isdn_getnum(p);
2685                if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
2686                        PARSE_ERROR1;
2687#ifdef CONFIG_ISDN_AUDIO
2688                if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
2689                        PARSE_ERROR1;
2690#endif
2691                m->mdmreg[REG_PSIZE] = i / 16;
2692                info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2693                switch (m->mdmreg[REG_L2PROT]) {
2694                case ISDN_PROTO_L2_V11096:
2695                case ISDN_PROTO_L2_V11019:
2696                case ISDN_PROTO_L2_V11038:
2697                        info->xmit_size /= 10;
2698                }
2699                break;
2700        case 'C':
2701                /* &C - DCD Status */
2702                p[0]++;
2703                switch (isdn_getnum(p)) {
2704                case 0:
2705                        m->mdmreg[REG_DCD] &= ~BIT_DCD;
2706                        break;
2707                case 1:
2708                        m->mdmreg[REG_DCD] |= BIT_DCD;
2709                        break;
2710                default:
2711                        PARSE_ERROR1
2712                                }
2713                break;
2714        case 'D':
2715                /* &D - Set DTR-Low-behavior */
2716                p[0]++;
2717                switch (isdn_getnum(p)) {
2718                case 0:
2719                        m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
2720                        m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2721                        break;
2722                case 2:
2723                        m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2724                        m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2725                        break;
2726                case 3:
2727                        m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2728                        m->mdmreg[REG_DTRR] |= BIT_DTRR;
2729                        break;
2730                default:
2731                        PARSE_ERROR1
2732                                }
2733                break;
2734        case 'E':
2735                /* &E -Set EAZ/MSN */
2736                p[0]++;
2737                isdn_tty_get_msnstr(m->msn, p);
2738                break;
2739        case 'F':
2740                /* &F -Set Factory-Defaults */
2741                p[0]++;
2742                if (info->msr & UART_MSR_DCD)
2743                        PARSE_ERROR1;
2744                isdn_tty_reset_profile(m);
2745                isdn_tty_modem_reset_regs(info, 1);
2746                break;
2747#ifdef DUMMY_HAYES_AT
2748        case 'K':
2749                /* only for be compilant with common scripts */
2750                /* &K Flowcontrol - no function */
2751                p[0]++;
2752                isdn_getnum(p);
2753                break;
2754#endif
2755        case 'L':
2756                /* &L -Set Numbers to listen on */
2757                p[0]++;
2758                i = 0;
2759                while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
2760                       (i < ISDN_LMSNLEN - 1))
2761                        m->lmsn[i++] = *p[0]++;
2762                m->lmsn[i] = '\0';
2763                break;
2764        case 'R':
2765                /* &R - Set V.110 bitrate adaption */
2766                p[0]++;
2767                i = isdn_getnum(p);
2768                switch (i) {
2769                case 0:
2770                        /* Switch off V.110, back to X.75 */
2771                        m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2772                        m->mdmreg[REG_SI2] = 0;
2773                        info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2774                        break;
2775                case 9600:
2776                        m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
2777                        m->mdmreg[REG_SI2] = 197;
2778                        info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2779                        break;
2780                case 19200:
2781                        m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
2782                        m->mdmreg[REG_SI2] = 199;
2783                        info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2784                        break;
2785                case 38400:
2786                        m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
2787                        m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
2788                        info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2789                        break;
2790                default:
2791                        PARSE_ERROR1;
2792                }
2793                /* Switch off T.70 */
2794                m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2795                /* Set Service 7 */
2796                m->mdmreg[REG_SI1] |= 4;
2797                break;
2798        case 'S':
2799                /* &S - Set Windowsize */
2800                p[0]++;
2801                i = isdn_getnum(p);
2802                if ((i > 0) && (i < 9))
2803                        m->mdmreg[REG_WSIZE] = i;
2804                else
2805                        PARSE_ERROR1;
2806                break;
2807        case 'V':
2808                /* &V - Show registers */
2809                p[0]++;
2810                isdn_tty_at_cout("\r\n", info);
2811                for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
2812                        sprintf(rb, "S%02d=%03d%s", i,
2813                                m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
2814                        isdn_tty_at_cout(rb, info);
2815                }
2816                sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
2817                        strlen(m->msn) ? m->msn : "None");
2818                isdn_tty_at_cout(rb, info);
2819                if (strlen(m->lmsn)) {
2820                        isdn_tty_at_cout("\r\nListen: ", info);
2821                        isdn_tty_at_cout(m->lmsn, info);
2822                        isdn_tty_at_cout("\r\n", info);
2823                }
2824                break;
2825        case 'W':
2826                /* &W - Write Profile */
2827                p[0]++;
2828                switch (*p[0]) {
2829                case '0':
2830                        p[0]++;
2831                        modem_write_profile(m);
2832                        break;
2833                default:
2834                        PARSE_ERROR1;
2835                }
2836                break;
2837        case 'X':
2838                /* &X - Switch to BTX-Mode and T.70 */
2839                p[0]++;
2840                switch (isdn_getnum(p)) {
2841                case 0:
2842                        m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2843                        info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2844                        break;
2845                case 1:
2846                        m->mdmreg[REG_T70] |= BIT_T70;
2847                        m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
2848                        m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2849                        info->xmit_size = 112;
2850                        m->mdmreg[REG_SI1] = 4;
2851                        m->mdmreg[REG_SI2] = 0;
2852                        break;
2853                case 2:
2854                        m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
2855                        m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2856                        info->xmit_size = 112;
2857                        m->mdmreg[REG_SI1] = 4;
2858                        m->mdmreg[REG_SI2] = 0;
2859                        break;
2860                default:
2861                        PARSE_ERROR1;
2862                }
2863                break;
2864        default:
2865                PARSE_ERROR1;
2866        }
2867        return 0;
2868}
2869
2870static int
2871isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
2872{
2873        /* Some plausibility checks */
2874        switch (mreg) {
2875        case REG_L2PROT:
2876                if (mval > ISDN_PROTO_L2_MAX)
2877                        return 1;
2878                break;
2879        case REG_PSIZE:
2880                if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
2881                        return 1;
2882#ifdef CONFIG_ISDN_AUDIO
2883                if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
2884                        return 1;
2885#endif
2886                info->xmit_size = mval * 16;
2887                switch (m->mdmreg[REG_L2PROT]) {
2888                case ISDN_PROTO_L2_V11096:
2889                case ISDN_PROTO_L2_V11019:
2890                case ISDN_PROTO_L2_V11038:
2891                        info->xmit_size /= 10;
2892                }
2893                break;
2894        case REG_SI1I:
2895        case REG_PLAN:
2896        case REG_SCREEN:
2897                /* readonly registers */
2898                return 1;
2899        }
2900        return 0;
2901}
2902
2903/*
2904 * Perform ATS command
2905 */
2906static int
2907isdn_tty_cmd_ATS(char **p, modem_info *info)
2908{
2909        atemu *m = &info->emu;
2910        int bitpos;
2911        int mreg;
2912        int mval;
2913        int bval;
2914
2915        mreg = isdn_getnum(p);
2916        if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
2917                PARSE_ERROR1;
2918        switch (*p[0]) {
2919        case '=':
2920                p[0]++;
2921                mval = isdn_getnum(p);
2922                if (mval < 0 || mval > 255)
2923                        PARSE_ERROR1;
2924                if (isdn_tty_check_ats(mreg, mval, info, m))
2925                        PARSE_ERROR1;
2926                m->mdmreg[mreg] = mval;
2927                break;
2928        case '.':
2929                /* Set/Clear a single bit */
2930                p[0]++;
2931                bitpos = isdn_getnum(p);
2932                if ((bitpos < 0) || (bitpos > 7))
2933                        PARSE_ERROR1;
2934                switch (*p[0]) {
2935                case '=':
2936                        p[0]++;
2937                        bval = isdn_getnum(p);
2938                        if (bval < 0 || bval > 1)
2939                                PARSE_ERROR1;
2940                        if (bval)
2941                                mval = m->mdmreg[mreg] | (1 << bitpos);
2942                        else
2943                                mval = m->mdmreg[mreg] & ~(1 << bitpos);
2944                        if (isdn_tty_check_ats(mreg, mval, info, m))
2945                                PARSE_ERROR1;
2946                        m->mdmreg[mreg] = mval;
2947                        break;
2948                case '?':
2949                        p[0]++;
2950                        isdn_tty_at_cout("\r\n", info);
2951                        isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
2952                                         info);
2953                        break;
2954                default:
2955                        PARSE_ERROR1;
2956                }
2957                break;
2958        case '?':
2959                p[0]++;
2960                isdn_tty_show_profile(mreg, info);
2961                break;
2962        default:
2963                PARSE_ERROR1;
2964                break;
2965        }
2966        return 0;
2967}
2968
2969/*
2970 * Perform ATA command
2971 */
2972static void
2973isdn_tty_cmd_ATA(modem_info *info)
2974{
2975        atemu *m = &info->emu;
2976        isdn_ctrl cmd;
2977        int l2;
2978
2979        if (info->msr & UART_MSR_RI) {
2980                /* Accept incoming call */
2981                info->last_dir = 0;
2982                strcpy(info->last_num, dev->num[info->drv_index]);
2983                m->mdmreg[REG_RINGCNT] = 0;
2984                info->msr &= ~UART_MSR_RI;
2985                l2 = m->mdmreg[REG_L2PROT];
2986#ifdef CONFIG_ISDN_AUDIO
2987                /* If more than one bit set in reg18, autoselect Layer2 */
2988                if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
2989                        if (m->mdmreg[REG_SI1I] == 1) {
2990                                if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
2991                                        l2 = ISDN_PROTO_L2_TRANS;
2992                        } else
2993                                l2 = ISDN_PROTO_L2_X75I;
2994                }
2995#endif
2996                cmd.driver = info->isdn_driver;
2997                cmd.command = ISDN_CMD_SETL2;
2998                cmd.arg = info->isdn_channel + (l2 << 8);
2999                info->last_l2 = l2;
3000                isdn_command(&cmd);
3001                cmd.driver = info->isdn_driver;
3002                cmd.command = ISDN_CMD_SETL3;
3003                cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
3004#ifdef CONFIG_ISDN_TTY_FAX
3005                if (l2 == ISDN_PROTO_L2_FAX) {
3006                        cmd.parm.fax = info->fax;
3007                        info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3008                }
3009#endif
3010                isdn_command(&cmd);
3011                cmd.driver = info->isdn_driver;
3012                cmd.arg = info->isdn_channel;
3013                cmd.command = ISDN_CMD_ACCEPTD;
3014                info->dialing = 16;
3015                info->emu.carrierwait = 0;
3016                isdn_command(&cmd);
3017                isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3018        } else
3019                isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3020}
3021
3022#ifdef CONFIG_ISDN_AUDIO
3023/*
3024 * Parse AT+F.. commands
3025 */
3026static int
3027isdn_tty_cmd_PLUSF(char **p, modem_info *info)
3028{
3029        atemu *m = &info->emu;
3030        char rs[20];
3031
3032        if (!strncmp(p[0], "CLASS", 5)) {
3033                p[0] += 5;
3034                switch (*p[0]) {
3035                case '?':
3036                        p[0]++;
3037                        sprintf(rs, "\r\n%d",
3038                                (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3039#ifdef CONFIG_ISDN_TTY_FAX
3040                        if (TTY_IS_FCLASS2(info))
3041                                sprintf(rs, "\r\n2");
3042                        else if (TTY_IS_FCLASS1(info))
3043                                sprintf(rs, "\r\n1");
3044#endif
3045                        isdn_tty_at_cout(rs, info);
3046                        break;
3047                case '=':
3048                        p[0]++;
3049                        switch (*p[0]) {
3050                        case '0':
3051                                p[0]++;
3052                                m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3053                                m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3054                                m->mdmreg[REG_SI1] = 4;
3055                                info->xmit_size =
3056                                        m->mdmreg[REG_PSIZE] * 16;
3057                                break;
3058#ifdef CONFIG_ISDN_TTY_FAX
3059                        case '1':
3060                                p[0]++;
3061                                if (!(dev->global_features &
3062                                      ISDN_FEATURE_L3_FCLASS1))
3063                                        PARSE_ERROR1;
3064                                m->mdmreg[REG_SI1] = 1;
3065                                m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3066                                m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3067                                info->xmit_size =
3068                                        m->mdmreg[REG_PSIZE] * 16;
3069                                break;
3070                        case '2':
3071                                p[0]++;
3072                                if (!(dev->global_features &
3073                                      ISDN_FEATURE_L3_FCLASS2))
3074                                        PARSE_ERROR1;
3075                                m->mdmreg[REG_SI1] = 1;
3076                                m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3077                                m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3078                                info->xmit_size =
3079                                        m->mdmreg[REG_PSIZE] * 16;
3080                                break;
3081#endif
3082                        case '8':
3083                                p[0]++;
3084                                /* L2 will change on dialout with si=1 */
3085                                m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3086                                m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3087                                m->mdmreg[REG_SI1] = 5;
3088                                info->xmit_size = VBUF;
3089                                break;
3090                        case '?':
3091                                p[0]++;
3092                                strcpy(rs, "\r\n0,");
3093#ifdef CONFIG_ISDN_TTY_FAX
3094                                if (dev->global_features &
3095                                    ISDN_FEATURE_L3_FCLASS1)
3096                                        strcat(rs, "1,");
3097                                if (dev->global_features &
3098                                    ISDN_FEATURE_L3_FCLASS2)
3099                                        strcat(rs, "2,");
3100#endif
3101                                strcat(rs, "8");
3102                                isdn_tty_at_cout(rs, info);
3103                                break;
3104                        default:
3105                                PARSE_ERROR1;
3106                        }
3107                        break;
3108                default:
3109                        PARSE_ERROR1;
3110                }
3111                return 0;
3112        }
3113#ifdef CONFIG_ISDN_TTY_FAX
3114        return (isdn_tty_cmd_PLUSF_FAX(p, info));
3115#else
3116        PARSE_ERROR1;
3117#endif
3118}
3119
3120/*
3121 * Parse AT+V.. commands
3122 */
3123static int
3124isdn_tty_cmd_PLUSV(char **p, modem_info *info)
3125{
3126        atemu *m = &info->emu;
3127        isdn_ctrl cmd;
3128        static char *vcmd[] =
3129                {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
3130        int i;
3131        int par1;
3132        int par2;
3133        char rs[20];
3134
3135        i = 0;
3136        while (vcmd[i]) {
3137                if (!strncmp(vcmd[i], p[0], 2)) {
3138                        p[0] += 2;
3139                        break;
3140                }
3141                i++;
3142        }
3143        switch (i) {
3144        case 0:
3145                /* AT+VNH - Auto hangup feature */
3146                switch (*p[0]) {
3147                case '?':
3148                        p[0]++;
3149                        isdn_tty_at_cout("\r\n1", info);
3150                        break;
3151                case '=':
3152                        p[0]++;
3153                        switch (*p[0]) {
3154                        case '1':
3155                                p[0]++;
3156                                break;
3157                        case '?':
3158                                p[0]++;
3159                                isdn_tty_at_cout("\r\n1", info);
3160                                break;
3161                        default:
3162                                PARSE_ERROR1;
3163                        }
3164                        break;
3165                default:
3166                        PARSE_ERROR1;
3167                }
3168                break;
3169        case 1:
3170                /* AT+VIP - Reset all voice parameters */
3171                isdn_tty_modem_reset_vpar(m);
3172                break;
3173        case 2:
3174                /* AT+VLS - Select device, accept incoming call */
3175                switch (*p[0]) {
3176                case '?':
3177                        p[0]++;
3178                        sprintf(rs, "\r\n%d", m->vpar[0]);
3179                        isdn_tty_at_cout(rs, info);
3180                        break;
3181                case '=':
3182                        p[0]++;
3183                        switch (*p[0]) {
3184                        case '0':
3185                                p[0]++;
3186                                m->vpar[0] = 0;
3187                                break;
3188                        case '2':
3189                                p[0]++;
3190                                m->vpar[0] = 2;
3191                                break;
3192                        case '?':
3193                                p[0]++;
3194                                isdn_tty_at_cout("\r\n0,2", info);
3195                                break;
3196                        default:
3197                                PARSE_ERROR1;
3198                        }
3199                        break;
3200                default:
3201                        PARSE_ERROR1;
3202                }
3203                break;
3204        case 3:
3205                /* AT+VRX - Start recording */
3206                if (!m->vpar[0])
3207                        PARSE_ERROR1;
3208                if (info->online != 1) {
3209                        isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3210                        return 1;
3211                }
3212                info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3213                if (!info->dtmf_state) {
3214                        printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3215                        PARSE_ERROR1;
3216                }
3217                info->silence_state = isdn_audio_silence_init(info->silence_state);
3218                if (!info->silence_state) {
3219                        printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3220                        PARSE_ERROR1;
3221                }
3222                if (m->vpar[3] < 5) {
3223                        info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3224                        if (!info->adpcmr) {
3225                                printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3226                                PARSE_ERROR1;
3227                        }
3228                }
3229#ifdef ISDN_DEBUG_AT
3230                printk(KERN_DEBUG "AT: +VRX\n");
3231#endif
3232                info->vonline |= 1;
3233                isdn_tty_modem_result(RESULT_CONNECT, info);
3234                return 0;
3235                break;
3236        case 4:
3237                /* AT+VSD - Silence detection */
3238                switch (*p[0]) {
3239                case '?':
3240                        p[0]++;
3241                        sprintf(rs, "\r\n<%d>,<%d>",
3242                                m->vpar[1],
3243                                m->vpar[2]);
3244                        isdn_tty_at_cout(rs, info);
3245                        break;
3246                case '=':
3247                        p[0]++;
3248                        if ((*p[0] >= '0') && (*p[0] <= '9')) {
3249                                par1 = isdn_getnum(p);
3250                                if ((par1 < 0) || (par1 > 31))
3251                                        PARSE_ERROR1;
3252                                if (*p[0] != ',')
3253                                        PARSE_ERROR1;
3254                                p[0]++;
3255                                par2 = isdn_getnum(p);
3256                                if ((par2 < 0) || (par2 > 255))
3257                                        PARSE_ERROR1;
3258                                m->vpar[1] = par1;
3259                                m->vpar[2] = par2;
3260                                break;
3261                        } else
3262                                if (*p[0] == '?') {
3263                                        p[0]++;
3264                                        isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3265                                                         info);
3266                                        break;
3267                                } else
3268                                        PARSE_ERROR1;
3269                        break;
3270                default:
3271                        PARSE_ERROR1;
3272                }
3273                break;
3274        case 5:
3275                /* AT+VSM - Select compression */
3276                switch (*p[0]) {
3277                case '?':
3278                        p[0]++;
3279                        sprintf(rs, "\r\n<%d>,<%d><8000>",
3280                                m->vpar[3],
3281                                m->vpar[1]);
3282                        isdn_tty_at_cout(rs, info);
3283                        break;
3284                case '=':
3285                        p[0]++;
3286                        switch (*p[0]) {
3287                        case '2':
3288                        case '3':
3289                        case '4':
3290                        case '5':
3291                        case '6':
3292                                par1 = isdn_getnum(p);
3293                                if ((par1 < 2) || (par1 > 6))
3294                                        PARSE_ERROR1;
3295                                m->vpar[3] = par1;
3296                                break;
3297                        case '?':
3298                                p[0]++;
3299                                isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3300                                                 info);
3301                                isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3302                                                 info);
3303                                isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3304                                                 info);
3305                                isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3306                                                 info);
3307                                isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3308                                                 info);
3309                                break;
3310                        default:
3311                                PARSE_ERROR1;
3312                        }
3313                        break;
3314                default:
3315                        PARSE_ERROR1;
3316                }
3317                break;
3318        case 6:
3319                /* AT+VTX - Start sending */
3320                if (!m->vpar[0])
3321                        PARSE_ERROR1;
3322                if (info->online != 1) {
3323                        isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3324                        return 1;
3325                }
3326                info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3327                if (!info->dtmf_state) {
3328                        printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3329                        PARSE_ERROR1;
3330                }
3331                if (m->vpar[3] < 5) {
3332                        info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3333                        if (!info->adpcms) {
3334                                printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3335                                PARSE_ERROR1;
3336                        }
3337                }
3338#ifdef ISDN_DEBUG_AT
3339                printk(KERN_DEBUG "AT: +VTX\n");
3340#endif
3341                m->lastDLE = 0;
3342                info->vonline |= 2;
3343                isdn_tty_modem_result(RESULT_CONNECT, info);
3344                return 0;
3345                break;
3346        case 7:
3347                /* AT+VDD - DTMF detection */
3348                switch (*p[0]) {
3349                case '?':
3350                        p[0]++;
3351                        sprintf(rs, "\r\n<%d>,<%d>",
3352                                m->vpar[4],
3353                                m->vpar[5]);
3354                        isdn_tty_at_cout(rs, info);
3355                        break;
3356                case '=':
3357                        p[0]++;
3358                        if ((*p[0] >= '0') && (*p[0] <= '9')) {
3359                                if (info->online != 1)
3360                                        PARSE_ERROR1;
3361                                par1 = isdn_getnum(p);
3362                                if ((par1 < 0) || (par1 > 15))
3363                                        PARSE_ERROR1;
3364                                if (*p[0] != ',')
3365                                        PARSE_ERROR1;
3366                                p[0]++;
3367                                par2 = isdn_getnum(p);
3368                                if ((par2 < 0) || (par2 > 255))
3369                                        PARSE_ERROR1;
3370                                m->vpar[4] = par1;
3371                                m->vpar[5] = par2;
3372                                cmd.driver = info->isdn_driver;
3373                                cmd.command = ISDN_CMD_AUDIO;
3374                                cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3375                                cmd.parm.num[0] = par1;
3376                                cmd.parm.num[1] = par2;
3377                                isdn_command(&cmd);
3378                                break;
3379                        } else
3380                                if (*p[0] == '?') {
3381                                        p[0]++;
3382                                        isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3383                                                         info);
3384                                        break;
3385                                } else
3386                                        PARSE_ERROR1;
3387                        break;
3388                default:
3389                        PARSE_ERROR1;
3390                }
3391                break;
3392        default:
3393                PARSE_ERROR1;
3394        }
3395        return 0;
3396}
3397#endif                          /* CONFIG_ISDN_AUDIO */
3398
3399/*
3400 * Parse and perform an AT-command-line.
3401 */
3402static void
3403isdn_tty_parse_at(modem_info *info)
3404{
3405        atemu *m = &info->emu;
3406        char *p;
3407        char ds[ISDN_MSNLEN];
3408
3409#ifdef ISDN_DEBUG_AT
3410        printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
3411#endif
3412        for (p = &m->mdmcmd[2]; *p;) {
3413                switch (*p) {
3414                case ' ':
3415                        p++;
3416                        break;
3417                case 'A':
3418                        /* A - Accept incoming call */
3419                        p++;
3420                        isdn_tty_cmd_ATA(info);
3421                        return;
3422                        break;
3423                case 'D':
3424                        /* D - Dial */
3425                        if (info->msr & UART_MSR_DCD)
3426                                PARSE_ERROR;
3427                        if (info->msr & UART_MSR_RI) {
3428                                isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3429                                return;
3430                        }
3431                        isdn_tty_getdial(++p, ds, sizeof ds);
3432                        p += strlen(p);
3433                        if (!strlen(m->msn))
3434                                isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
3435                        else if (strlen(ds))
3436                                isdn_tty_dial(ds, info, m);
3437                        else
3438                                PARSE_ERROR;
3439                        return;
3440                case 'E':
3441                        /* E - Turn Echo on/off */
3442                        p++;
3443                        switch (isdn_getnum(&p)) {
3444                        case 0:
3445                                m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
3446                                break;
3447                        case 1:
3448                                m->mdmreg[REG_ECHO] |= BIT_ECHO;
3449                                break;
3450                        default:
3451                                PARSE_ERROR;
3452                        }
3453                        break;
3454                case 'H':
3455                        /* H - On/Off-hook */
3456                        p++;
3457                        switch (*p) {
3458                        case '0':
3459                                p++;
3460                                isdn_tty_on_hook(info);
3461                                break;
3462                        case '1':
3463                                p++;
3464                                isdn_tty_off_hook();
3465                                break;
3466                        default:
3467                                isdn_tty_on_hook(info);
3468                                break;
3469                        }
3470                        break;
3471                case 'I':
3472                        /* I - Information */
3473                        p++;
3474                        isdn_tty_at_cout("\r\nLinux ISDN", info);
3475                        switch (*p) {
3476                        case '0':
3477                        case '1':
3478                                p++;
3479                                break;
3480                        case '2':
3481                                p++;
3482                                isdn_tty_report(info);
3483                                break;
3484                        case '3':
3485                                p++;
3486                                snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
3487                                isdn_tty_at_cout(ds, info);
3488                                break;
3489                        default:;
3490                        }
3491                        break;
3492#ifdef DUMMY_HAYES_AT
3493                case 'L':
3494                case 'M':
3495                        /* only for be compilant with common scripts */
3496                        /* no function */
3497                        p++;
3498                        isdn_getnum(&p);
3499                        break;
3500#endif
3501                case 'O':
3502                        /* O - Go online */
3503                        p++;
3504                        if (info->msr & UART_MSR_DCD)
3505                                /* if B-Channel is up */
3506                                isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
3507                        else
3508                                isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3509                        return;
3510                case 'Q':
3511                        /* Q - Turn Emulator messages on/off */
3512                        p++;
3513                        switch (isdn_getnum(&p)) {
3514                        case 0:
3515                                m->mdmreg[REG_RESP] |= BIT_RESP;
3516                                break;
3517                        case 1:
3518                                m->mdmreg[REG_RESP] &= ~BIT_RESP;
3519                                break;
3520                        default:
3521                                PARSE_ERROR;
3522                        }
3523                        break;
3524                case 'S':
3525                        /* S - Set/Get Register */
3526                        p++;
3527                        if (isdn_tty_cmd_ATS(&p, info))
3528                                return;
3529                        break;
3530                case 'V':
3531                        /* V - Numeric or ASCII Emulator-messages */
3532                        p++;
3533                        switch (isdn_getnum(&p)) {
3534                        case 0:
3535                                m->mdmreg[REG_RESP] |= BIT_RESPNUM;
3536                                break;
3537                        case 1:
3538                                m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
3539                                break;
3540                        default:
3541                                PARSE_ERROR;
3542                        }
3543                        break;
3544                case 'Z':
3545                        /* Z - Load Registers from Profile */
3546                        p++;
3547                        if (info->msr & UART_MSR_DCD) {
3548                                info->online = 0;
3549                                isdn_tty_on_hook(info);
3550                        }
3551                        isdn_tty_modem_reset_regs(info, 1);
3552                        break;
3553                case '+':
3554                        p++;
3555                        switch (*p) {
3556#ifdef CONFIG_ISDN_AUDIO
3557                        case 'F':
3558                                p++;
3559                                if (isdn_tty_cmd_PLUSF(&p, info))
3560                                        return;
3561                                break;
3562                        case 'V':
3563                                if ((!(m->mdmreg[REG_SI1] & 1)) ||
3564                                    (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
3565                                        PARSE_ERROR;
3566                                p++;
3567                                if (isdn_tty_cmd_PLUSV(&p, info))
3568                                        return;
3569                                break;
3570#endif                          /* CONFIG_ISDN_AUDIO */
3571                        case 'S':       /* SUSPEND */
3572                                p++;
3573                                isdn_tty_get_msnstr(ds, &p);
3574                                isdn_tty_suspend(ds, info, m);
3575                                break;
3576                        case 'R':       /* RESUME */
3577                                p++;
3578                                isdn_tty_get_msnstr(ds, &p);
3579                                isdn_tty_resume(ds, info, m);
3580                                break;
3581                        case 'M':       /* MESSAGE */
3582                                p++;
3583                                isdn_tty_send_msg(info, m, p);
3584                                break;
3585                        default:
3586                                PARSE_ERROR;
3587                        }
3588                        break;
3589                case '&':
3590                        p++;
3591                        if (isdn_tty_cmd_ATand(&p, info))
3592                                return;
3593                        break;
3594                default:
3595                        PARSE_ERROR;
3596                }
3597        }
3598#ifdef CONFIG_ISDN_AUDIO
3599        if (!info->vonline)
3600#endif
3601                isdn_tty_modem_result(RESULT_OK, info);
3602}
3603
3604/* Need own toupper() because standard-toupper is not available
3605 * within modules.
3606 */
3607#define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
3608
3609/*
3610 * Perform line-editing of AT-commands
3611 *
3612 * Parameters:
3613 *   p        inputbuffer
3614 *   count    length of buffer
3615 *   channel  index to line (minor-device)
3616 */
3617static int
3618isdn_tty_edit_at(const char *p, int count, modem_info *info)
3619{
3620        atemu *m = &info->emu;
3621        int total = 0;
3622        u_char c;
3623        char eb[2];
3624        int cnt;
3625
3626        for (cnt = count; cnt > 0; p++, cnt--) {
3627                c = *p;
3628                total++;
3629                if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
3630                        /* Separator (CR or LF) */
3631                        m->mdmcmd[m->mdmcmdl] = 0;
3632                        if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3633                                eb[0] = c;
3634                                eb[1] = 0;
3635                                isdn_tty_at_cout(eb, info);
3636                        }
3637                        if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
3638                                isdn_tty_parse_at(info);
3639                        m->mdmcmdl = 0;
3640                        continue;
3641                }
3642                if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
3643                        /* Backspace-Function */
3644                        if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
3645                                if (m->mdmcmdl)
3646                                        m->mdmcmdl--;
3647                                if (m->mdmreg[REG_ECHO] & BIT_ECHO)
3648                                        isdn_tty_at_cout("\b", info);
3649                        }
3650                        continue;
3651                }
3652                if (cmdchar(c)) {
3653                        if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3654                                eb[0] = c;
3655                                eb[1] = 0;
3656                                isdn_tty_at_cout(eb, info);
3657                        }
3658                        if (m->mdmcmdl < 255) {
3659                                c = my_toupper(c);
3660                                switch (m->mdmcmdl) {
3661                                case 1:
3662                                        if (c == 'T') {
3663                                                m->mdmcmd[m->mdmcmdl] = c;
3664                                                m->mdmcmd[++m->mdmcmdl] = 0;
3665                                                break;
3666                                        } else
3667                                                m->mdmcmdl = 0;
3668                                        /* Fall through, check for 'A' */
3669                                case 0:
3670                                        if (c == 'A') {
3671                                                m->mdmcmd[m->mdmcmdl] = c;
3672                                                m->mdmcmd[++m->mdmcmdl] = 0;
3673                                        }
3674                                        break;
3675                                default:
3676                                        m->mdmcmd[m->mdmcmdl] = c;
3677                                        m->mdmcmd[++m->mdmcmdl] = 0;
3678                                }
3679                        }
3680                }
3681        }
3682        return total;
3683}
3684
3685/*
3686 * Switch all modem-channels who are online and got a valid
3687 * escape-sequence 1.5 seconds ago, to command-mode.
3688 * This function is called every second via timer-interrupt from within
3689 * timer-dispatcher isdn_timer_function()
3690 */
3691void
3692isdn_tty_modem_escape(void)
3693{
3694        int ton = 0;
3695        int i;
3696        int midx;
3697
3698        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
3699                if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
3700                        modem_info *info = &dev->mdm.info[midx];
3701                        if (info->online) {
3702                                ton = 1;
3703                                if ((info->emu.pluscount == 3) &&
3704                                    time_after(jiffies,
3705                                            info->emu.lastplus + PLUSWAIT2)) {
3706                                        info->emu.pluscount = 0;
3707                                        info->online = 0;
3708                                        isdn_tty_modem_result(RESULT_OK, info);
3709                                }
3710                        }
3711                }
3712        isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
3713}
3714
3715/*
3716 * Put a RING-message to all modem-channels who have the RI-bit set.
3717 * This function is called every second via timer-interrupt from within
3718 * timer-dispatcher isdn_timer_function()
3719 */
3720void
3721isdn_tty_modem_ring(void)
3722{
3723        int ton = 0;
3724        int i;
3725
3726        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3727                modem_info *info = &dev->mdm.info[i];
3728                if (info->msr & UART_MSR_RI) {
3729                        ton = 1;
3730                        isdn_tty_modem_result(RESULT_RING, info);
3731                }
3732        }
3733        isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
3734}
3735
3736/*
3737 * For all online tty's, try sending data to
3738 * the lower levels.
3739 */
3740void
3741isdn_tty_modem_xmit(void)
3742{
3743        int ton = 1;
3744        int i;
3745
3746        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3747                modem_info *info = &dev->mdm.info[i];
3748                if (info->online) {
3749                        ton = 1;
3750                        isdn_tty_senddown(info);
3751                        isdn_tty_tint(info);
3752                }
3753        }
3754        isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
3755}
3756
3757/*
3758 * Check all channels if we have a 'no carrier' timeout.
3759 * Timeout value is set by Register S7.
3760 */
3761void
3762isdn_tty_carrier_timeout(void)
3763{
3764        int ton = 0;
3765        int i;
3766
3767        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3768                modem_info *info = &dev->mdm.info[i];
3769                if (!info->dialing)
3770                        continue;
3771                if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
3772                        info->dialing = 0;
3773                        isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3774                        isdn_tty_modem_hup(info, 1);
3775                } else
3776                        ton = 1;
3777        }
3778        isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
3779}
3780