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