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