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