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