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