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