]> Pileus Git - ~andy/linux/blob - drivers/isdn/i4l/isdn_tty.c
Merge tag 'disintegrate-fbdev-20121220' of git://git.infradead.org/users/dhowells...
[~andy/linux] / 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 = strlen(msg);
906         if (!l) {
907                 isdn_tty_modem_result(RESULT_ERROR, info);
908                 return;
909         }
910         for (j = 7; j >= 0; j--)
911                 if (m->mdmreg[REG_SI1] & (1 << j)) {
912                         si = bit2si[j];
913                         break;
914                 }
915         usg = isdn_calc_usage(si, l2);
916 #ifdef CONFIG_ISDN_AUDIO
917         if ((si == 1) &&
918             (l2 != ISDN_PROTO_L2_MODEM)
919 #ifdef CONFIG_ISDN_TTY_FAX
920             && (l2 != ISDN_PROTO_L2_FAX)
921 #endif
922                 ) {
923                 l2 = ISDN_PROTO_L2_TRANS;
924                 usg = ISDN_USAGE_VOICE;
925         }
926 #endif
927         m->mdmreg[REG_SI1I] = si2bit[si];
928         spin_lock_irqsave(&dev->lock, flags);
929         i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
930         if (i < 0) {
931                 spin_unlock_irqrestore(&dev->lock, flags);
932                 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
933         } else {
934                 info->isdn_driver = dev->drvmap[i];
935                 info->isdn_channel = dev->chanmap[i];
936                 info->drv_index = i;
937                 dev->m_idx[i] = info->line;
938                 dev->usage[i] |= ISDN_USAGE_OUTGOING;
939                 info->last_dir = 1;
940                 isdn_info_update();
941                 spin_unlock_irqrestore(&dev->lock, flags);
942                 cmd.driver = info->isdn_driver;
943                 cmd.arg = info->isdn_channel;
944                 cmd.command = ISDN_CMD_CLREAZ;
945                 isdn_command(&cmd);
946                 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
947                 cmd.driver = info->isdn_driver;
948                 cmd.command = ISDN_CMD_SETEAZ;
949                 isdn_command(&cmd);
950                 cmd.driver = info->isdn_driver;
951                 cmd.command = ISDN_CMD_SETL2;
952                 info->last_l2 = l2;
953                 cmd.arg = info->isdn_channel + (l2 << 8);
954                 isdn_command(&cmd);
955                 cmd.driver = info->isdn_driver;
956                 cmd.command = ISDN_CMD_SETL3;
957                 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
958                 isdn_command(&cmd);
959                 cmd.driver = info->isdn_driver;
960                 cmd.arg = info->isdn_channel;
961                 cmd.parm.cmsg.Length = l + 14;
962                 cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
963                 cmd.parm.cmsg.Subcommand = CAPI_REQ;
964                 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
965                 cmd.parm.cmsg.para[0] = l + 1;
966                 strncpy(&cmd.parm.cmsg.para[1], msg, l);
967                 cmd.parm.cmsg.para[l + 1] = 0xd;
968                 cmd.command = CAPI_PUT_MESSAGE;
969 /*              info->dialing = 1;
970                 strcpy(dev->num[i], n);
971                 isdn_info_update();
972 */
973                 isdn_command(&cmd);
974         }
975 }
976
977 static inline int
978 isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
979 {
980 #ifdef MODEM_PARANOIA_CHECK
981         if (!info) {
982                 printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
983                        name, routine);
984                 return 1;
985         }
986         if (info->magic != ISDN_ASYNC_MAGIC) {
987                 printk(KERN_WARNING "isdn_tty: bad magic for modem struct %s in %s\n",
988                        name, routine);
989                 return 1;
990         }
991 #endif
992         return 0;
993 }
994
995 /*
996  * This routine is called to set the UART divisor registers to match
997  * the specified baud rate for a serial port.
998  */
999 static void
1000 isdn_tty_change_speed(modem_info *info)
1001 {
1002         struct tty_port *port = &info->port;
1003         uint cflag,
1004                 cval,
1005                 quot;
1006         int i;
1007
1008         if (!port->tty)
1009                 return;
1010         cflag = port->tty->termios.c_cflag;
1011
1012         quot = i = cflag & CBAUD;
1013         if (i & CBAUDEX) {
1014                 i &= ~CBAUDEX;
1015                 if (i < 1 || i > 2)
1016                         port->tty->termios.c_cflag &= ~CBAUDEX;
1017                 else
1018                         i += 15;
1019         }
1020         if (quot) {
1021                 info->mcr |= UART_MCR_DTR;
1022                 isdn_tty_modem_ncarrier(info);
1023         } else {
1024                 info->mcr &= ~UART_MCR_DTR;
1025                 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1026 #ifdef ISDN_DEBUG_MODEM_HUP
1027                         printk(KERN_DEBUG "Mhup in changespeed\n");
1028 #endif
1029                         if (info->online)
1030                                 info->ncarrier = 1;
1031                         isdn_tty_modem_reset_regs(info, 0);
1032                         isdn_tty_modem_hup(info, 1);
1033                 }
1034                 return;
1035         }
1036         /* byte size and parity */
1037         cval = cflag & (CSIZE | CSTOPB);
1038         cval >>= 4;
1039         if (cflag & PARENB)
1040                 cval |= UART_LCR_PARITY;
1041         if (!(cflag & PARODD))
1042                 cval |= UART_LCR_EPAR;
1043
1044         /* CTS flow control flag and modem status interrupts */
1045         if (cflag & CRTSCTS) {
1046                 port->flags |= ASYNC_CTS_FLOW;
1047         } else
1048                 port->flags &= ~ASYNC_CTS_FLOW;
1049         if (cflag & CLOCAL)
1050                 port->flags &= ~ASYNC_CHECK_CD;
1051         else {
1052                 port->flags |= ASYNC_CHECK_CD;
1053         }
1054 }
1055
1056 static int
1057 isdn_tty_startup(modem_info *info)
1058 {
1059         if (info->port.flags & ASYNC_INITIALIZED)
1060                 return 0;
1061         isdn_lock_drivers();
1062 #ifdef ISDN_DEBUG_MODEM_OPEN
1063         printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
1064 #endif
1065         /*
1066          * Now, initialize the UART
1067          */
1068         info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
1069         if (info->port.tty)
1070                 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
1071         /*
1072          * and set the speed of the serial port
1073          */
1074         isdn_tty_change_speed(info);
1075
1076         info->port.flags |= ASYNC_INITIALIZED;
1077         info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
1078         info->send_outstanding = 0;
1079         return 0;
1080 }
1081
1082 /*
1083  * This routine will shutdown a serial port; interrupts are disabled, and
1084  * DTR is dropped if the hangup on close termio flag is on.
1085  */
1086 static void
1087 isdn_tty_shutdown(modem_info *info)
1088 {
1089         if (!(info->port.flags & ASYNC_INITIALIZED))
1090                 return;
1091 #ifdef ISDN_DEBUG_MODEM_OPEN
1092         printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
1093 #endif
1094         isdn_unlock_drivers();
1095         info->msr &= ~UART_MSR_RI;
1096         if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
1097                 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
1098                 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1099                         isdn_tty_modem_reset_regs(info, 0);
1100 #ifdef ISDN_DEBUG_MODEM_HUP
1101                         printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
1102 #endif
1103                         isdn_tty_modem_hup(info, 1);
1104                 }
1105         }
1106         if (info->port.tty)
1107                 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1108
1109         info->port.flags &= ~ASYNC_INITIALIZED;
1110 }
1111
1112 /* isdn_tty_write() is the main send-routine. It is called from the upper
1113  * levels within the kernel to perform sending data. Depending on the
1114  * online-flag it either directs output to the at-command-interpreter or
1115  * to the lower level. Additional tasks done here:
1116  *  - If online, check for escape-sequence (+++)
1117  *  - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
1118  *  - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
1119  *  - If dialing, abort dial.
1120  */
1121 static int
1122 isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
1123 {
1124         int c;
1125         int total = 0;
1126         modem_info *info = (modem_info *) tty->driver_data;
1127         atemu *m = &info->emu;
1128
1129         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write"))
1130                 return 0;
1131         /* See isdn_tty_senddown() */
1132         atomic_inc(&info->xmit_lock);
1133         while (1) {
1134                 c = count;
1135                 if (c > info->xmit_size - info->xmit_count)
1136                         c = info->xmit_size - info->xmit_count;
1137                 if (info->isdn_driver >= 0 && c > dev->drv[info->isdn_driver]->maxbufsize)
1138                         c = dev->drv[info->isdn_driver]->maxbufsize;
1139                 if (c <= 0)
1140                         break;
1141                 if ((info->online > 1)
1142 #ifdef CONFIG_ISDN_AUDIO
1143                     || (info->vonline & 3)
1144 #endif
1145                         ) {
1146 #ifdef CONFIG_ISDN_AUDIO
1147                         if (!info->vonline)
1148 #endif
1149                                 isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
1150                                                    &(m->pluscount),
1151                                                    &(m->lastplus));
1152                         memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
1153 #ifdef CONFIG_ISDN_AUDIO
1154                         if (info->vonline) {
1155                                 int cc = isdn_tty_handleDLEdown(info, m, c);
1156                                 if (info->vonline & 2) {
1157                                         if (!cc) {
1158                                                 /* If DLE decoding results in zero-transmit, but
1159                                                  * c originally was non-zero, do a wakeup.
1160                                                  */
1161                                                 tty_wakeup(tty);
1162                                                 info->msr |= UART_MSR_CTS;
1163                                                 info->lsr |= UART_LSR_TEMT;
1164                                         }
1165                                         info->xmit_count += cc;
1166                                 }
1167                                 if ((info->vonline & 3) == 1) {
1168                                         /* Do NOT handle Ctrl-Q or Ctrl-S
1169                                          * when in full-duplex audio mode.
1170                                          */
1171                                         if (isdn_tty_end_vrx(buf, c)) {
1172                                                 info->vonline &= ~1;
1173 #ifdef ISDN_DEBUG_MODEM_VOICE
1174                                                 printk(KERN_DEBUG
1175                                                        "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
1176                                                        info->line);
1177 #endif
1178                                                 isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
1179                                         }
1180                                 }
1181                         } else
1182                                 if (TTY_IS_FCLASS1(info)) {
1183                                         int cc = isdn_tty_handleDLEdown(info, m, c);
1184
1185                                         if (info->vonline & 4) { /* ETX seen */
1186                                                 isdn_ctrl c;
1187
1188                                                 c.command = ISDN_CMD_FAXCMD;
1189                                                 c.driver = info->isdn_driver;
1190                                                 c.arg = info->isdn_channel;
1191                                                 c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
1192                                                 c.parm.aux.subcmd = ETX;
1193                                                 isdn_command(&c);
1194                                         }
1195                                         info->vonline = 0;
1196 #ifdef ISDN_DEBUG_MODEM_VOICE
1197                                         printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
1198 #endif
1199                                         info->xmit_count += cc;
1200                                 } else
1201 #endif
1202                                         info->xmit_count += c;
1203                 } else {
1204                         info->msr |= UART_MSR_CTS;
1205                         info->lsr |= UART_LSR_TEMT;
1206                         if (info->dialing) {
1207                                 info->dialing = 0;
1208 #ifdef ISDN_DEBUG_MODEM_HUP
1209                                 printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
1210 #endif
1211                                 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
1212                                 isdn_tty_modem_hup(info, 1);
1213                         } else
1214                                 c = isdn_tty_edit_at(buf, c, info);
1215                 }
1216                 buf += c;
1217                 count -= c;
1218                 total += c;
1219         }
1220         atomic_dec(&info->xmit_lock);
1221         if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
1222                 if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
1223                         isdn_tty_senddown(info);
1224                         isdn_tty_tint(info);
1225                 }
1226                 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1227         }
1228         return total;
1229 }
1230
1231 static int
1232 isdn_tty_write_room(struct tty_struct *tty)
1233 {
1234         modem_info *info = (modem_info *) tty->driver_data;
1235         int ret;
1236
1237         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write_room"))
1238                 return 0;
1239         if (!info->online)
1240                 return info->xmit_size;
1241         ret = info->xmit_size - info->xmit_count;
1242         return (ret < 0) ? 0 : ret;
1243 }
1244
1245 static int
1246 isdn_tty_chars_in_buffer(struct tty_struct *tty)
1247 {
1248         modem_info *info = (modem_info *) tty->driver_data;
1249
1250         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_chars_in_buffer"))
1251                 return 0;
1252         if (!info->online)
1253                 return 0;
1254         return (info->xmit_count);
1255 }
1256
1257 static void
1258 isdn_tty_flush_buffer(struct tty_struct *tty)
1259 {
1260         modem_info *info;
1261
1262         if (!tty) {
1263                 return;
1264         }
1265         info = (modem_info *) tty->driver_data;
1266         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
1267                 return;
1268         }
1269         isdn_tty_cleanup_xmit(info);
1270         info->xmit_count = 0;
1271         tty_wakeup(tty);
1272 }
1273
1274 static void
1275 isdn_tty_flush_chars(struct tty_struct *tty)
1276 {
1277         modem_info *info = (modem_info *) tty->driver_data;
1278
1279         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
1280                 return;
1281         if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
1282                 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1283 }
1284
1285 /*
1286  * ------------------------------------------------------------
1287  * isdn_tty_throttle()
1288  *
1289  * This routine is called by the upper-layer tty layer to signal that
1290  * incoming characters should be throttled.
1291  * ------------------------------------------------------------
1292  */
1293 static void
1294 isdn_tty_throttle(struct tty_struct *tty)
1295 {
1296         modem_info *info = (modem_info *) tty->driver_data;
1297
1298         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_throttle"))
1299                 return;
1300         if (I_IXOFF(tty))
1301                 info->x_char = STOP_CHAR(tty);
1302         info->mcr &= ~UART_MCR_RTS;
1303 }
1304
1305 static void
1306 isdn_tty_unthrottle(struct tty_struct *tty)
1307 {
1308         modem_info *info = (modem_info *) tty->driver_data;
1309
1310         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_unthrottle"))
1311                 return;
1312         if (I_IXOFF(tty)) {
1313                 if (info->x_char)
1314                         info->x_char = 0;
1315                 else
1316                         info->x_char = START_CHAR(tty);
1317         }
1318         info->mcr |= UART_MCR_RTS;
1319 }
1320
1321 /*
1322  * ------------------------------------------------------------
1323  * isdn_tty_ioctl() and friends
1324  * ------------------------------------------------------------
1325  */
1326
1327 /*
1328  * isdn_tty_get_lsr_info - get line status register info
1329  *
1330  * Purpose: Let user call ioctl() to get info when the UART physically
1331  *          is emptied.  On bus types like RS485, the transmitter must
1332  *          release the bus after transmitting. This must be done when
1333  *          the transmit shift register is empty, not be done when the
1334  *          transmit holding register is empty.  This functionality
1335  *          allows RS485 driver to be written in user space.
1336  */
1337 static int
1338 isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
1339 {
1340         u_char status;
1341         uint result;
1342
1343         status = info->lsr;
1344         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1345         return put_user(result, value);
1346 }
1347
1348
1349 static int
1350 isdn_tty_tiocmget(struct tty_struct *tty)
1351 {
1352         modem_info *info = (modem_info *) tty->driver_data;
1353         u_char control, status;
1354
1355         if (isdn_tty_paranoia_check(info, tty->name, __func__))
1356                 return -ENODEV;
1357         if (tty->flags & (1 << TTY_IO_ERROR))
1358                 return -EIO;
1359
1360         mutex_lock(&modem_info_mutex);
1361 #ifdef ISDN_DEBUG_MODEM_IOCTL
1362         printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
1363 #endif
1364
1365         control = info->mcr;
1366         status = info->msr;
1367         mutex_unlock(&modem_info_mutex);
1368         return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1369                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1370                 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1371                 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1372                 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1373                 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1374 }
1375
1376 static int
1377 isdn_tty_tiocmset(struct tty_struct *tty,
1378                   unsigned int set, unsigned int clear)
1379 {
1380         modem_info *info = (modem_info *) tty->driver_data;
1381
1382         if (isdn_tty_paranoia_check(info, tty->name, __func__))
1383                 return -ENODEV;
1384         if (tty->flags & (1 << TTY_IO_ERROR))
1385                 return -EIO;
1386
1387 #ifdef ISDN_DEBUG_MODEM_IOCTL
1388         printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
1389 #endif
1390
1391         mutex_lock(&modem_info_mutex);
1392         if (set & TIOCM_RTS)
1393                 info->mcr |= UART_MCR_RTS;
1394         if (set & TIOCM_DTR) {
1395                 info->mcr |= UART_MCR_DTR;
1396                 isdn_tty_modem_ncarrier(info);
1397         }
1398
1399         if (clear & TIOCM_RTS)
1400                 info->mcr &= ~UART_MCR_RTS;
1401         if (clear & TIOCM_DTR) {
1402                 info->mcr &= ~UART_MCR_DTR;
1403                 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1404                         isdn_tty_modem_reset_regs(info, 0);
1405 #ifdef ISDN_DEBUG_MODEM_HUP
1406                         printk(KERN_DEBUG "Mhup in TIOCMSET\n");
1407 #endif
1408                         if (info->online)
1409                                 info->ncarrier = 1;
1410                         isdn_tty_modem_hup(info, 1);
1411                 }
1412         }
1413         mutex_unlock(&modem_info_mutex);
1414         return 0;
1415 }
1416
1417 static int
1418 isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
1419 {
1420         modem_info *info = (modem_info *) tty->driver_data;
1421         int retval;
1422
1423         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
1424                 return -ENODEV;
1425         if (tty->flags & (1 << TTY_IO_ERROR))
1426                 return -EIO;
1427         switch (cmd) {
1428         case TCSBRK:   /* SVID version: non-zero arg --> no break */
1429 #ifdef ISDN_DEBUG_MODEM_IOCTL
1430                 printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
1431 #endif
1432                 retval = tty_check_change(tty);
1433                 if (retval)
1434                         return retval;
1435                 tty_wait_until_sent(tty, 0);
1436                 return 0;
1437         case TCSBRKP:  /* support for POSIX tcsendbreak() */
1438 #ifdef ISDN_DEBUG_MODEM_IOCTL
1439                 printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
1440 #endif
1441                 retval = tty_check_change(tty);
1442                 if (retval)
1443                         return retval;
1444                 tty_wait_until_sent(tty, 0);
1445                 return 0;
1446         case TIOCSERGETLSR:     /* Get line status register */
1447 #ifdef ISDN_DEBUG_MODEM_IOCTL
1448                 printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
1449 #endif
1450                 return isdn_tty_get_lsr_info(info, (uint __user *) arg);
1451         default:
1452 #ifdef ISDN_DEBUG_MODEM_IOCTL
1453                 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
1454 #endif
1455                 return -ENOIOCTLCMD;
1456         }
1457         return 0;
1458 }
1459
1460 static void
1461 isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1462 {
1463         modem_info *info = (modem_info *) tty->driver_data;
1464
1465         if (!old_termios)
1466                 isdn_tty_change_speed(info);
1467         else {
1468                 if (tty->termios.c_cflag == old_termios->c_cflag &&
1469                     tty->termios.c_ispeed == old_termios->c_ispeed &&
1470                     tty->termios.c_ospeed == old_termios->c_ospeed)
1471                         return;
1472                 isdn_tty_change_speed(info);
1473                 if ((old_termios->c_cflag & CRTSCTS) &&
1474                     !(tty->termios.c_cflag & CRTSCTS))
1475                         tty->hw_stopped = 0;
1476         }
1477 }
1478
1479 /*
1480  * ------------------------------------------------------------
1481  * isdn_tty_open() and friends
1482  * ------------------------------------------------------------
1483  */
1484
1485 static int isdn_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1486 {
1487         modem_info *info = &dev->mdm.info[tty->index];
1488
1489         if (isdn_tty_paranoia_check(info, tty->name, __func__))
1490                 return -ENODEV;
1491
1492         tty->driver_data = info;
1493
1494         return tty_port_install(&info->port, driver, tty);
1495 }
1496
1497 /*
1498  * This routine is called whenever a serial port is opened.  It
1499  * enables interrupts for a serial port, linking in its async structure into
1500  * the IRQ chain.   It also performs the serial-specific
1501  * initialization for the tty structure.
1502  */
1503 static int
1504 isdn_tty_open(struct tty_struct *tty, struct file *filp)
1505 {
1506         modem_info *info = tty->driver_data;
1507         struct tty_port *port = &info->port;
1508         int retval;
1509
1510 #ifdef ISDN_DEBUG_MODEM_OPEN
1511         printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
1512                port->count);
1513 #endif
1514         port->count++;
1515         port->tty = tty;
1516         /*
1517          * Start up serial port
1518          */
1519         retval = isdn_tty_startup(info);
1520         if (retval) {
1521 #ifdef ISDN_DEBUG_MODEM_OPEN
1522                 printk(KERN_DEBUG "isdn_tty_open return after startup\n");
1523 #endif
1524                 return retval;
1525         }
1526         retval = tty_port_block_til_ready(port, tty, filp);
1527         if (retval) {
1528 #ifdef ISDN_DEBUG_MODEM_OPEN
1529                 printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
1530 #endif
1531                 return retval;
1532         }
1533 #ifdef ISDN_DEBUG_MODEM_OPEN
1534         printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
1535 #endif
1536         dev->modempoll++;
1537 #ifdef ISDN_DEBUG_MODEM_OPEN
1538         printk(KERN_DEBUG "isdn_tty_open normal exit\n");
1539 #endif
1540         return 0;
1541 }
1542
1543 static void
1544 isdn_tty_close(struct tty_struct *tty, struct file *filp)
1545 {
1546         modem_info *info = (modem_info *) tty->driver_data;
1547         struct tty_port *port = &info->port;
1548         ulong timeout;
1549
1550         if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
1551                 return;
1552         if (tty_hung_up_p(filp)) {
1553 #ifdef ISDN_DEBUG_MODEM_OPEN
1554                 printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
1555 #endif
1556                 return;
1557         }
1558         if ((tty->count == 1) && (port->count != 1)) {
1559                 /*
1560                  * Uh, oh.  tty->count is 1, which means that the tty
1561                  * structure will be freed.  Info->count should always
1562                  * be one in these conditions.  If it's greater than
1563                  * one, we've got real problems, since it means the
1564                  * serial port won't be shutdown.
1565                  */
1566                 printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
1567                        "info->count is %d\n", port->count);
1568                 port->count = 1;
1569         }
1570         if (--port->count < 0) {
1571                 printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
1572                        info->line, port->count);
1573                 port->count = 0;
1574         }
1575         if (port->count) {
1576 #ifdef ISDN_DEBUG_MODEM_OPEN
1577                 printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
1578 #endif
1579                 return;
1580         }
1581         port->flags |= ASYNC_CLOSING;
1582
1583         tty->closing = 1;
1584         /*
1585          * At this point we stop accepting input.  To do this, we
1586          * disable the receive line status interrupts, and tell the
1587          * interrupt driver to stop checking the data ready bit in the
1588          * line status register.
1589          */
1590         if (port->flags & ASYNC_INITIALIZED) {
1591                 tty_wait_until_sent_from_close(tty, 3000);      /* 30 seconds timeout */
1592                 /*
1593                  * Before we drop DTR, make sure the UART transmitter
1594                  * has completely drained; this is especially
1595                  * important if there is a transmit FIFO!
1596                  */
1597                 timeout = jiffies + HZ;
1598                 while (!(info->lsr & UART_LSR_TEMT)) {
1599                         schedule_timeout_interruptible(20);
1600                         if (time_after(jiffies, timeout))
1601                                 break;
1602                 }
1603         }
1604         dev->modempoll--;
1605         isdn_tty_shutdown(info);
1606         isdn_tty_flush_buffer(tty);
1607         tty_ldisc_flush(tty);
1608         port->tty = NULL;
1609         info->ncarrier = 0;
1610
1611         tty_port_close_end(port, tty);
1612 #ifdef ISDN_DEBUG_MODEM_OPEN
1613         printk(KERN_DEBUG "isdn_tty_close normal exit\n");
1614 #endif
1615 }
1616
1617 /*
1618  * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1619  */
1620 static void
1621 isdn_tty_hangup(struct tty_struct *tty)
1622 {
1623         modem_info *info = (modem_info *) tty->driver_data;
1624         struct tty_port *port = &info->port;
1625
1626         if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
1627                 return;
1628         isdn_tty_shutdown(info);
1629         port->count = 0;
1630         port->flags &= ~ASYNC_NORMAL_ACTIVE;
1631         port->tty = NULL;
1632         wake_up_interruptible(&port->open_wait);
1633 }
1634
1635 /* This routine initializes all emulator-data.
1636  */
1637 static void
1638 isdn_tty_reset_profile(atemu *m)
1639 {
1640         m->profile[0] = 0;
1641         m->profile[1] = 0;
1642         m->profile[2] = 43;
1643         m->profile[3] = 13;
1644         m->profile[4] = 10;
1645         m->profile[5] = 8;
1646         m->profile[6] = 3;
1647         m->profile[7] = 60;
1648         m->profile[8] = 2;
1649         m->profile[9] = 6;
1650         m->profile[10] = 7;
1651         m->profile[11] = 70;
1652         m->profile[12] = 0x45;
1653         m->profile[13] = 4;
1654         m->profile[14] = ISDN_PROTO_L2_X75I;
1655         m->profile[15] = ISDN_PROTO_L3_TRANS;
1656         m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
1657         m->profile[17] = ISDN_MODEM_WINSIZE;
1658         m->profile[18] = 4;
1659         m->profile[19] = 0;
1660         m->profile[20] = 0;
1661         m->profile[23] = 0;
1662         m->pmsn[0] = '\0';
1663         m->plmsn[0] = '\0';
1664 }
1665
1666 #ifdef CONFIG_ISDN_AUDIO
1667 static void
1668 isdn_tty_modem_reset_vpar(atemu *m)
1669 {
1670         m->vpar[0] = 2;         /* Voice-device            (2 = phone line) */
1671         m->vpar[1] = 0;         /* Silence detection level (0 = none      ) */
1672         m->vpar[2] = 70;        /* Silence interval        (7 sec.        ) */
1673         m->vpar[3] = 2;         /* Compression type        (1 = ADPCM-2   ) */
1674         m->vpar[4] = 0;         /* DTMF detection level    (0 = softcode  ) */
1675         m->vpar[5] = 8;         /* DTMF interval           (8 * 5 ms.     ) */
1676 }
1677 #endif
1678
1679 #ifdef CONFIG_ISDN_TTY_FAX
1680 static void
1681 isdn_tty_modem_reset_faxpar(modem_info *info)
1682 {
1683         T30_s *f = info->fax;
1684
1685         f->code = 0;
1686         f->phase = ISDN_FAX_PHASE_IDLE;
1687         f->direction = 0;
1688         f->resolution = 1;      /* fine */
1689         f->rate = 5;            /* 14400 bit/s */
1690         f->width = 0;
1691         f->length = 0;
1692         f->compression = 0;
1693         f->ecm = 0;
1694         f->binary = 0;
1695         f->scantime = 0;
1696         memset(&f->id[0], 32, FAXIDLEN - 1);
1697         f->id[FAXIDLEN - 1] = 0;
1698         f->badlin = 0;
1699         f->badmul = 0;
1700         f->bor = 0;
1701         f->nbc = 0;
1702         f->cq = 0;
1703         f->cr = 0;
1704         f->ctcrty = 0;
1705         f->minsp = 0;
1706         f->phcto = 30;
1707         f->rel = 0;
1708         memset(&f->pollid[0], 32, FAXIDLEN - 1);
1709         f->pollid[FAXIDLEN - 1] = 0;
1710 }
1711 #endif
1712
1713 static void
1714 isdn_tty_modem_reset_regs(modem_info *info, int force)
1715 {
1716         atemu *m = &info->emu;
1717         if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
1718                 memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
1719                 memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
1720                 memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
1721                 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
1722         }
1723 #ifdef CONFIG_ISDN_AUDIO
1724         isdn_tty_modem_reset_vpar(m);
1725 #endif
1726 #ifdef CONFIG_ISDN_TTY_FAX
1727         isdn_tty_modem_reset_faxpar(info);
1728 #endif
1729         m->mdmcmdl = 0;
1730 }
1731
1732 static void
1733 modem_write_profile(atemu *m)
1734 {
1735         memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
1736         memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
1737         memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
1738         if (dev->profd)
1739                 send_sig(SIGIO, dev->profd, 1);
1740 }
1741
1742 static const struct tty_operations modem_ops = {
1743         .install = isdn_tty_install,
1744         .open = isdn_tty_open,
1745         .close = isdn_tty_close,
1746         .write = isdn_tty_write,
1747         .flush_chars = isdn_tty_flush_chars,
1748         .write_room = isdn_tty_write_room,
1749         .chars_in_buffer = isdn_tty_chars_in_buffer,
1750         .flush_buffer = isdn_tty_flush_buffer,
1751         .ioctl = isdn_tty_ioctl,
1752         .throttle = isdn_tty_throttle,
1753         .unthrottle = isdn_tty_unthrottle,
1754         .set_termios = isdn_tty_set_termios,
1755         .hangup = isdn_tty_hangup,
1756         .tiocmget = isdn_tty_tiocmget,
1757         .tiocmset = isdn_tty_tiocmset,
1758 };
1759
1760 static int isdn_tty_carrier_raised(struct tty_port *port)
1761 {
1762         modem_info *info = container_of(port, modem_info, port);
1763         return info->msr & UART_MSR_DCD;
1764 }
1765
1766 static const struct tty_port_operations isdn_tty_port_ops = {
1767         .carrier_raised = isdn_tty_carrier_raised,
1768 };
1769
1770 int
1771 isdn_tty_modem_init(void)
1772 {
1773         isdn_modem_t    *m;
1774         int             i, retval;
1775         modem_info      *info;
1776
1777         m = &dev->mdm;
1778         m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
1779         if (!m->tty_modem)
1780                 return -ENOMEM;
1781         m->tty_modem->name = "ttyI";
1782         m->tty_modem->major = ISDN_TTY_MAJOR;
1783         m->tty_modem->minor_start = 0;
1784         m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
1785         m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
1786         m->tty_modem->init_termios = tty_std_termios;
1787         m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1788         m->tty_modem->flags = TTY_DRIVER_REAL_RAW;
1789         m->tty_modem->driver_name = "isdn_tty";
1790         tty_set_operations(m->tty_modem, &modem_ops);
1791         retval = tty_register_driver(m->tty_modem);
1792         if (retval) {
1793                 printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
1794                 goto err;
1795         }
1796         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1797                 info = &m->info[i];
1798 #ifdef CONFIG_ISDN_TTY_FAX
1799                 if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
1800                         printk(KERN_ERR "Could not allocate fax t30-buffer\n");
1801                         retval = -ENOMEM;
1802                         goto err_unregister;
1803                 }
1804 #endif
1805                 tty_port_init(&info->port);
1806                 info->port.ops = &isdn_tty_port_ops;
1807                 spin_lock_init(&info->readlock);
1808                 sprintf(info->last_cause, "0000");
1809                 sprintf(info->last_num, "none");
1810                 info->last_dir = 0;
1811                 info->last_lhup = 1;
1812                 info->last_l2 = -1;
1813                 info->last_si = 0;
1814                 isdn_tty_reset_profile(&info->emu);
1815                 isdn_tty_modem_reset_regs(info, 1);
1816                 info->magic = ISDN_ASYNC_MAGIC;
1817                 info->line = i;
1818                 info->x_char = 0;
1819                 info->isdn_driver = -1;
1820                 info->isdn_channel = -1;
1821                 info->drv_index = -1;
1822                 info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
1823                 init_timer(&info->nc_timer);
1824                 info->nc_timer.function = isdn_tty_modem_do_ncarrier;
1825                 info->nc_timer.data = (unsigned long) info;
1826                 skb_queue_head_init(&info->xmit_queue);
1827 #ifdef CONFIG_ISDN_AUDIO
1828                 skb_queue_head_init(&info->dtmf_queue);
1829 #endif
1830                 info->port.xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5,
1831                                 GFP_KERNEL);
1832                 if (!info->port.xmit_buf) {
1833                         printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
1834                         retval = -ENOMEM;
1835                         goto err_unregister;
1836                 }
1837                 /* Make room for T.70 header */
1838                 info->port.xmit_buf += 4;
1839         }
1840         return 0;
1841 err_unregister:
1842         for (i--; i >= 0; i--) {
1843                 info = &m->info[i];
1844 #ifdef CONFIG_ISDN_TTY_FAX
1845                 kfree(info->fax);
1846 #endif
1847                 kfree(info->port.xmit_buf - 4);
1848                 info->port.xmit_buf = NULL;
1849                 tty_port_destroy(&info->port);
1850         }
1851         tty_unregister_driver(m->tty_modem);
1852 err:
1853         put_tty_driver(m->tty_modem);
1854         m->tty_modem = NULL;
1855         return retval;
1856 }
1857
1858 void
1859 isdn_tty_exit(void)
1860 {
1861         modem_info *info;
1862         int i;
1863
1864         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1865                 info = &dev->mdm.info[i];
1866                 isdn_tty_cleanup_xmit(info);
1867 #ifdef CONFIG_ISDN_TTY_FAX
1868                 kfree(info->fax);
1869 #endif
1870                 kfree(info->port.xmit_buf - 4);
1871                 info->port.xmit_buf = NULL;
1872                 tty_port_destroy(&info->port);
1873         }
1874         tty_unregister_driver(dev->mdm.tty_modem);
1875         put_tty_driver(dev->mdm.tty_modem);
1876         dev->mdm.tty_modem = NULL;
1877 }
1878
1879
1880 /*
1881  * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
1882  *      match the MSN against the MSNs (glob patterns) defined for tty_emulator,
1883  *      and return 0 for match, 1 for no match, 2 if MSN could match if longer.
1884  */
1885
1886 static int
1887 isdn_tty_match_icall(char *cid, atemu *emu, int di)
1888 {
1889 #ifdef ISDN_DEBUG_MODEM_ICALL
1890         printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
1891                emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
1892                emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
1893 #endif
1894         if (strlen(emu->lmsn)) {
1895                 char *p = emu->lmsn;
1896                 char *q;
1897                 int  tmp;
1898                 int  ret = 0;
1899
1900                 while (1) {
1901                         if ((q = strchr(p, ';')))
1902                                 *q = '\0';
1903                         if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
1904                                 ret = tmp;
1905 #ifdef ISDN_DEBUG_MODEM_ICALL
1906                         printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
1907                                p, isdn_map_eaz2msn(emu->msn, di), tmp);
1908 #endif
1909                         if (q) {
1910                                 *q = ';';
1911                                 p = q;
1912                                 p++;
1913                         }
1914                         if (!tmp)
1915                                 return 0;
1916                         if (!q)
1917                                 break;
1918                 }
1919                 return ret;
1920         } else {
1921                 int tmp;
1922                 tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
1923 #ifdef ISDN_DEBUG_MODEM_ICALL
1924                 printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
1925                        isdn_map_eaz2msn(emu->msn, di), tmp);
1926 #endif
1927                 return tmp;
1928         }
1929 }
1930
1931 /*
1932  * An incoming call-request has arrived.
1933  * Search the tty-devices for an appropriate device and bind
1934  * it to the ISDN-Channel.
1935  * Return:
1936  *
1937  *  0 = No matching device found.
1938  *  1 = A matching device found.
1939  *  3 = No match found, but eventually would match, if
1940  *      CID is longer.
1941  */
1942 int
1943 isdn_tty_find_icall(int di, int ch, setup_parm *setup)
1944 {
1945         char *eaz;
1946         int i;
1947         int wret;
1948         int idx;
1949         int si1;
1950         int si2;
1951         char *nr;
1952         ulong flags;
1953
1954         if (!setup->phone[0]) {
1955                 nr = "0";
1956                 printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
1957         } else
1958                 nr = setup->phone;
1959         si1 = (int) setup->si1;
1960         si2 = (int) setup->si2;
1961         if (!setup->eazmsn[0]) {
1962                 printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
1963                 eaz = "0";
1964         } else
1965                 eaz = setup->eazmsn;
1966 #ifdef ISDN_DEBUG_MODEM_ICALL
1967         printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
1968 #endif
1969         wret = 0;
1970         spin_lock_irqsave(&dev->lock, flags);
1971         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1972                 modem_info *info = &dev->mdm.info[i];
1973
1974                 if (info->port.count == 0)
1975                         continue;
1976                 if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) &&  /* SI1 is matching */
1977                     (info->emu.mdmreg[REG_SI2] == si2)) {         /* SI2 is matching */
1978                         idx = isdn_dc2minor(di, ch);
1979 #ifdef ISDN_DEBUG_MODEM_ICALL
1980                         printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
1981                         printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
1982                                info->port.flags, info->isdn_driver,
1983                                info->isdn_channel, dev->usage[idx]);
1984 #endif
1985                         if (
1986 #ifndef FIX_FILE_TRANSFER
1987                                 (info->port.flags & ASYNC_NORMAL_ACTIVE) &&
1988 #endif
1989                                 (info->isdn_driver == -1) &&
1990                                 (info->isdn_channel == -1) &&
1991                                 (USG_NONE(dev->usage[idx]))) {
1992                                 int matchret;
1993
1994                                 if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
1995                                         wret = matchret;
1996                                 if (!matchret) {                  /* EAZ is matching */
1997                                         info->isdn_driver = di;
1998                                         info->isdn_channel = ch;
1999                                         info->drv_index = idx;
2000                                         dev->m_idx[idx] = info->line;
2001                                         dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
2002                                         dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
2003                                         strcpy(dev->num[idx], nr);
2004                                         strcpy(info->emu.cpn, eaz);
2005                                         info->emu.mdmreg[REG_SI1I] = si2bit[si1];
2006                                         info->emu.mdmreg[REG_PLAN] = setup->plan;
2007                                         info->emu.mdmreg[REG_SCREEN] = setup->screen;
2008                                         isdn_info_update();
2009                                         spin_unlock_irqrestore(&dev->lock, flags);
2010                                         printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2011                                                info->line);
2012                                         info->msr |= UART_MSR_RI;
2013                                         isdn_tty_modem_result(RESULT_RING, info);
2014                                         isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2015                                         return 1;
2016                                 }
2017                         }
2018                 }
2019         }
2020         spin_unlock_irqrestore(&dev->lock, flags);
2021         printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
2022                ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
2023         return (wret == 2) ? 3 : 0;
2024 }
2025
2026 #define TTY_IS_ACTIVE(info)     (info->port.flags & ASYNC_NORMAL_ACTIVE)
2027
2028 int
2029 isdn_tty_stat_callback(int i, isdn_ctrl *c)
2030 {
2031         int mi;
2032         modem_info *info;
2033         char *e;
2034
2035         if (i < 0)
2036                 return 0;
2037         if ((mi = dev->m_idx[i]) >= 0) {
2038                 info = &dev->mdm.info[mi];
2039                 switch (c->command) {
2040                 case ISDN_STAT_CINF:
2041                         printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2042                         info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2043                         if (e == (char *)c->parm.num)
2044                                 info->emu.charge = 0;
2045
2046                         break;
2047                 case ISDN_STAT_BSENT:
2048 #ifdef ISDN_TTY_STAT_DEBUG
2049                         printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
2050 #endif
2051                         if ((info->isdn_driver == c->driver) &&
2052                             (info->isdn_channel == c->arg)) {
2053                                 info->msr |= UART_MSR_CTS;
2054                                 if (info->send_outstanding)
2055                                         if (!(--info->send_outstanding))
2056                                                 info->lsr |= UART_LSR_TEMT;
2057                                 isdn_tty_tint(info);
2058                                 return 1;
2059                         }
2060                         break;
2061                 case ISDN_STAT_CAUSE:
2062 #ifdef ISDN_TTY_STAT_DEBUG
2063                         printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2064 #endif
2065                         /* Signal cause to tty-device */
2066                         strncpy(info->last_cause, c->parm.num, 5);
2067                         return 1;
2068                 case ISDN_STAT_DISPLAY:
2069 #ifdef ISDN_TTY_STAT_DEBUG
2070                         printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2071 #endif
2072                         /* Signal display to tty-device */
2073                         if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2074                             !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2075                                 isdn_tty_at_cout("\r\n", info);
2076                                 isdn_tty_at_cout("DISPLAY: ", info);
2077                                 isdn_tty_at_cout(c->parm.display, info);
2078                                 isdn_tty_at_cout("\r\n", info);
2079                         }
2080                         return 1;
2081                 case ISDN_STAT_DCONN:
2082 #ifdef ISDN_TTY_STAT_DEBUG
2083                         printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2084 #endif
2085                         if (TTY_IS_ACTIVE(info)) {
2086                                 if (info->dialing == 1) {
2087                                         info->dialing = 2;
2088                                         return 1;
2089                                 }
2090                         }
2091                         break;
2092                 case ISDN_STAT_DHUP:
2093 #ifdef ISDN_TTY_STAT_DEBUG
2094                         printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2095 #endif
2096                         if (TTY_IS_ACTIVE(info)) {
2097                                 if (info->dialing == 1)
2098                                         isdn_tty_modem_result(RESULT_BUSY, info);
2099                                 if (info->dialing > 1)
2100                                         isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2101                                 info->dialing = 0;
2102 #ifdef ISDN_DEBUG_MODEM_HUP
2103                                 printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2104 #endif
2105                                 isdn_tty_modem_hup(info, 0);
2106                                 return 1;
2107                         }
2108                         break;
2109                 case ISDN_STAT_BCONN:
2110 #ifdef ISDN_TTY_STAT_DEBUG
2111                         printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2112 #endif
2113                         /* Wake up any processes waiting
2114                          * for incoming call of this device when
2115                          * DCD follow the state of incoming carrier
2116                          */
2117                         if (info->port.blocked_open &&
2118                             (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
2119                                 wake_up_interruptible(&info->port.open_wait);
2120                         }
2121
2122                         /* Schedule CONNECT-Message to any tty
2123                          * waiting for it and
2124                          * set DCD-bit of its modem-status.
2125                          */
2126                         if (TTY_IS_ACTIVE(info) ||
2127                             (info->port.blocked_open &&
2128                              (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
2129                                 info->msr |= UART_MSR_DCD;
2130                                 info->emu.charge = 0;
2131                                 if (info->dialing & 0xf)
2132                                         info->last_dir = 1;
2133                                 else
2134                                         info->last_dir = 0;
2135                                 info->dialing = 0;
2136                                 info->rcvsched = 1;
2137                                 if (USG_MODEM(dev->usage[i])) {
2138                                         if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2139                                                 strcpy(info->emu.connmsg, c->parm.num);
2140                                                 isdn_tty_modem_result(RESULT_CONNECT, info);
2141                                         } else
2142                                                 isdn_tty_modem_result(RESULT_CONNECT64000, info);
2143                                 }
2144                                 if (USG_VOICE(dev->usage[i]))
2145                                         isdn_tty_modem_result(RESULT_VCON, info);
2146                                 return 1;
2147                         }
2148                         break;
2149                 case ISDN_STAT_BHUP:
2150 #ifdef ISDN_TTY_STAT_DEBUG
2151                         printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2152 #endif
2153                         if (TTY_IS_ACTIVE(info)) {
2154 #ifdef ISDN_DEBUG_MODEM_HUP
2155                                 printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2156 #endif
2157                                 isdn_tty_modem_hup(info, 0);
2158                                 return 1;
2159                         }
2160                         break;
2161                 case ISDN_STAT_NODCH:
2162 #ifdef ISDN_TTY_STAT_DEBUG
2163                         printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2164 #endif
2165                         if (TTY_IS_ACTIVE(info)) {
2166                                 if (info->dialing) {
2167                                         info->dialing = 0;
2168                                         info->last_l2 = -1;
2169                                         info->last_si = 0;
2170                                         sprintf(info->last_cause, "0000");
2171                                         isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2172                                 }
2173                                 isdn_tty_modem_hup(info, 0);
2174                                 return 1;
2175                         }
2176                         break;
2177                 case ISDN_STAT_UNLOAD:
2178 #ifdef ISDN_TTY_STAT_DEBUG
2179                         printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2180 #endif
2181                         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2182                                 info = &dev->mdm.info[i];
2183                                 if (info->isdn_driver == c->driver) {
2184                                         if (info->online)
2185                                                 isdn_tty_modem_hup(info, 1);
2186                                 }
2187                         }
2188                         return 1;
2189 #ifdef CONFIG_ISDN_TTY_FAX
2190                 case ISDN_STAT_FAXIND:
2191                         if (TTY_IS_ACTIVE(info)) {
2192                                 isdn_tty_fax_command(info, c);
2193                         }
2194                         break;
2195 #endif
2196 #ifdef CONFIG_ISDN_AUDIO
2197                 case ISDN_STAT_AUDIO:
2198                         if (TTY_IS_ACTIVE(info)) {
2199                                 switch (c->parm.num[0]) {
2200                                 case ISDN_AUDIO_DTMF:
2201                                         if (info->vonline) {
2202                                                 isdn_audio_put_dle_code(info,
2203                                                                         c->parm.num[1]);
2204                                         }
2205                                         break;
2206                                 }
2207                         }
2208                         break;
2209 #endif
2210                 }
2211         }
2212         return 0;
2213 }
2214
2215 /*********************************************************************
2216  Modem-Emulator-Routines
2217 *********************************************************************/
2218
2219 #define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
2220
2221 /*
2222  * Put a message from the AT-emulator into receive-buffer of tty,
2223  * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2224  */
2225 void
2226 isdn_tty_at_cout(char *msg, modem_info *info)
2227 {
2228         struct tty_port *port = &info->port;
2229         atemu *m = &info->emu;
2230         char *p;
2231         char c;
2232         u_long flags;
2233         struct sk_buff *skb = NULL;
2234         char *sp = NULL;
2235         int l;
2236
2237         if (!msg) {
2238                 printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2239                 return;
2240         }
2241
2242         l = strlen(msg);
2243
2244         spin_lock_irqsave(&info->readlock, flags);
2245         if (port->flags & ASYNC_CLOSING) {
2246                 spin_unlock_irqrestore(&info->readlock, flags);
2247                 return;
2248         }
2249
2250         /* use queue instead of direct, if online and */
2251         /* data is in queue or buffer is full */
2252         if (info->online && ((tty_buffer_request_room(port, l) < l) ||
2253                              !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
2254                 skb = alloc_skb(l, GFP_ATOMIC);
2255                 if (!skb) {
2256                         spin_unlock_irqrestore(&info->readlock, flags);
2257                         return;
2258                 }
2259                 sp = skb_put(skb, l);
2260 #ifdef CONFIG_ISDN_AUDIO
2261                 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2262                 ISDN_AUDIO_SKB_LOCK(skb) = 0;
2263 #endif
2264         }
2265
2266         for (p = msg; *p; p++) {
2267                 switch (*p) {
2268                 case '\r':
2269                         c = m->mdmreg[REG_CR];
2270                         break;
2271                 case '\n':
2272                         c = m->mdmreg[REG_LF];
2273                         break;
2274                 case '\b':
2275                         c = m->mdmreg[REG_BS];
2276                         break;
2277                 default:
2278                         c = *p;
2279                 }
2280                 if (skb) {
2281                         *sp++ = c;
2282                 } else {
2283                         if (tty_insert_flip_char(port, c, TTY_NORMAL) == 0)
2284                                 break;
2285                 }
2286         }
2287         if (skb) {
2288                 __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2289                 dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2290                 spin_unlock_irqrestore(&info->readlock, flags);
2291                 /* Schedule dequeuing */
2292                 if (dev->modempoll && info->rcvsched)
2293                         isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2294
2295         } else {
2296                 spin_unlock_irqrestore(&info->readlock, flags);
2297                 tty_flip_buffer_push(port);
2298         }
2299 }
2300
2301 /*
2302  * Perform ATH Hangup
2303  */
2304 static void
2305 isdn_tty_on_hook(modem_info *info)
2306 {
2307         if (info->isdn_channel >= 0) {
2308 #ifdef ISDN_DEBUG_MODEM_HUP
2309                 printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2310 #endif
2311                 isdn_tty_modem_hup(info, 1);
2312         }
2313 }
2314
2315 static void
2316 isdn_tty_off_hook(void)
2317 {
2318         printk(KERN_DEBUG "isdn_tty_off_hook\n");
2319 }
2320
2321 #define PLUSWAIT1 (HZ / 2)      /* 0.5 sec. */
2322 #define PLUSWAIT2 (HZ * 3 / 2)  /* 1.5 sec */
2323
2324 /*
2325  * Check Buffer for Modem-escape-sequence, activate timer-callback to
2326  * isdn_tty_modem_escape() if sequence found.
2327  *
2328  * Parameters:
2329  *   p          pointer to databuffer
2330  *   plus       escape-character
2331  *   count      length of buffer
2332  *   pluscount  count of valid escape-characters so far
2333  *   lastplus   timestamp of last character
2334  */
2335 static void
2336 isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
2337                    u_long *lastplus)
2338 {
2339         if (plus > 127)
2340                 return;
2341         if (count > 3) {
2342                 p += count - 3;
2343                 count = 3;
2344                 *pluscount = 0;
2345         }
2346         while (count > 0) {
2347                 if (*(p++) == plus) {
2348                         if ((*pluscount)++) {
2349                                 /* Time since last '+' > 0.5 sec. ? */
2350                                 if (time_after(jiffies, *lastplus + PLUSWAIT1))
2351                                         *pluscount = 1;
2352                         } else {
2353                                 /* Time since last non-'+' < 1.5 sec. ? */
2354                                 if (time_before(jiffies, *lastplus + PLUSWAIT2))
2355                                         *pluscount = 0;
2356                         }
2357                         if ((*pluscount == 3) && (count == 1))
2358                                 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2359                         if (*pluscount > 3)
2360                                 *pluscount = 1;
2361                 } else
2362                         *pluscount = 0;
2363                 *lastplus = jiffies;
2364                 count--;
2365         }
2366 }
2367
2368 /*
2369  * Return result of AT-emulator to tty-receive-buffer, depending on
2370  * modem-register 12, bit 0 and 1.
2371  * For CONNECT-messages also switch to online-mode.
2372  * For RING-message handle auto-ATA if register 0 != 0
2373  */
2374
2375 static void
2376 isdn_tty_modem_result(int code, modem_info *info)
2377 {
2378         atemu *m = &info->emu;
2379         static char *msg[] =
2380                 {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2381                  "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2382                  "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2383         char s[ISDN_MSNLEN + 10];
2384
2385         switch (code) {
2386         case RESULT_RING:
2387                 m->mdmreg[REG_RINGCNT]++;
2388                 if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2389                         /* Automatically accept incoming call */
2390                         isdn_tty_cmd_ATA(info);
2391                 break;
2392         case RESULT_NO_CARRIER:
2393 #ifdef ISDN_DEBUG_MODEM_HUP
2394                 printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
2395                        (info->port.flags & ASYNC_CLOSING),
2396                        (!info->port.tty));
2397 #endif
2398                 m->mdmreg[REG_RINGCNT] = 0;
2399                 del_timer(&info->nc_timer);
2400                 info->ncarrier = 0;
2401                 if ((info->port.flags & ASYNC_CLOSING) || (!info->port.tty))
2402                         return;
2403
2404 #ifdef CONFIG_ISDN_AUDIO
2405                 if (info->vonline & 1) {
2406 #ifdef ISDN_DEBUG_MODEM_VOICE
2407                         printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
2408                                info->line);
2409 #endif
2410                         /* voice-recording, add DLE-ETX */
2411                         isdn_tty_at_cout("\020\003", info);
2412                 }
2413                 if (info->vonline & 2) {
2414 #ifdef ISDN_DEBUG_MODEM_VOICE
2415                         printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
2416                                info->line);
2417 #endif
2418                         /* voice-playing, add DLE-DC4 */
2419                         isdn_tty_at_cout("\020\024", info);
2420                 }
2421 #endif
2422                 break;
2423         case RESULT_CONNECT:
2424         case RESULT_CONNECT64000:
2425                 sprintf(info->last_cause, "0000");
2426                 if (!info->online)
2427                         info->online = 2;
2428                 break;
2429         case RESULT_VCON:
2430 #ifdef ISDN_DEBUG_MODEM_VOICE
2431                 printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
2432                        info->line);
2433 #endif
2434                 sprintf(info->last_cause, "0000");
2435                 if (!info->online)
2436                         info->online = 1;
2437                 break;
2438         } /* switch (code) */
2439
2440         if (m->mdmreg[REG_RESP] & BIT_RESP) {
2441                 /* Show results */
2442                 if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
2443                         /* Show numeric results only */
2444                         sprintf(s, "\r\n%d\r\n", code);
2445                         isdn_tty_at_cout(s, info);
2446                 } else {
2447                         if (code == RESULT_RING) {
2448                                 /* return if "show RUNG" and ringcounter>1 */
2449                                 if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
2450                                     (m->mdmreg[REG_RINGCNT] > 1))
2451                                         return;
2452                                 /* print CID, _before_ _every_ ring */
2453                                 if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
2454                                         isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
2455                                         isdn_tty_at_cout(dev->num[info->drv_index], info);
2456                                         if (m->mdmreg[REG_CDN] & BIT_CDN) {
2457                                                 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2458                                                 isdn_tty_at_cout(info->emu.cpn, info);
2459                                         }
2460                                 }
2461                         }
2462                         isdn_tty_at_cout("\r\n", info);
2463                         isdn_tty_at_cout(msg[code], info);
2464                         switch (code) {
2465                         case RESULT_CONNECT:
2466                                 switch (m->mdmreg[REG_L2PROT]) {
2467                                 case ISDN_PROTO_L2_MODEM:
2468                                         isdn_tty_at_cout(" ", info);
2469                                         isdn_tty_at_cout(m->connmsg, info);
2470                                         break;
2471                                 }
2472                                 break;
2473                         case RESULT_RING:
2474                                 /* Append CPN, if enabled */
2475                                 if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
2476                                         sprintf(s, "/%s", m->cpn);
2477                                         isdn_tty_at_cout(s, info);
2478                                 }
2479                                 /* Print CID only once, _after_ 1st RING */
2480                                 if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
2481                                     (m->mdmreg[REG_RINGCNT] == 1)) {
2482                                         isdn_tty_at_cout("\r\n", info);
2483                                         isdn_tty_at_cout("CALLER NUMBER: ", info);
2484                                         isdn_tty_at_cout(dev->num[info->drv_index], info);
2485                                         if (m->mdmreg[REG_CDN] & BIT_CDN) {
2486                                                 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2487                                                 isdn_tty_at_cout(info->emu.cpn, info);
2488                                         }
2489                                 }
2490                                 break;
2491                         case RESULT_NO_CARRIER:
2492                         case RESULT_NO_DIALTONE:
2493                         case RESULT_BUSY:
2494                         case RESULT_NO_ANSWER:
2495                                 m->mdmreg[REG_RINGCNT] = 0;
2496                                 /* Append Cause-Message if enabled */
2497                                 if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
2498                                         sprintf(s, "/%s", info->last_cause);
2499                                         isdn_tty_at_cout(s, info);
2500                                 }
2501                                 break;
2502                         case RESULT_CONNECT64000:
2503                                 /* Append Protocol to CONNECT message */
2504                                 switch (m->mdmreg[REG_L2PROT]) {
2505                                 case ISDN_PROTO_L2_X75I:
2506                                 case ISDN_PROTO_L2_X75UI:
2507                                 case ISDN_PROTO_L2_X75BUI:
2508                                         isdn_tty_at_cout("/X.75", info);
2509                                         break;
2510                                 case ISDN_PROTO_L2_HDLC:
2511                                         isdn_tty_at_cout("/HDLC", info);
2512                                         break;
2513                                 case ISDN_PROTO_L2_V11096:
2514                                         isdn_tty_at_cout("/V110/9600", info);
2515                                         break;
2516                                 case ISDN_PROTO_L2_V11019:
2517                                         isdn_tty_at_cout("/V110/19200", info);
2518                                         break;
2519                                 case ISDN_PROTO_L2_V11038:
2520                                         isdn_tty_at_cout("/V110/38400", info);
2521                                         break;
2522                                 }
2523                                 if (m->mdmreg[REG_T70] & BIT_T70) {
2524                                         isdn_tty_at_cout("/T.70", info);
2525                                         if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2526                                                 isdn_tty_at_cout("+", info);
2527                                 }
2528                                 break;
2529                         }
2530                         isdn_tty_at_cout("\r\n", info);
2531                 }
2532         }
2533         if (code == RESULT_NO_CARRIER) {
2534                 if ((info->port.flags & ASYNC_CLOSING) || (!info->port.tty))
2535                         return;
2536
2537                 if (info->port.flags & ASYNC_CHECK_CD)
2538                         tty_hangup(info->port.tty);
2539         }
2540 }
2541
2542
2543 /*
2544  * Display a modem-register-value.
2545  */
2546 static void
2547 isdn_tty_show_profile(int ridx, modem_info *info)
2548 {
2549         char v[6];
2550
2551         sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
2552         isdn_tty_at_cout(v, info);
2553 }
2554
2555 /*
2556  * Get MSN-string from char-pointer, set pointer to end of number
2557  */
2558 static void
2559 isdn_tty_get_msnstr(char *n, char **p)
2560 {
2561         int limit = ISDN_MSNLEN - 1;
2562
2563         while (((*p[0] >= '0' && *p[0] <= '9') ||
2564                 /* Why a comma ??? */
2565                 (*p[0] == ',') || (*p[0] == ':')) &&
2566                (limit--))
2567                 *n++ = *p[0]++;
2568         *n = '\0';
2569 }
2570
2571 /*
2572  * Get phone-number from modem-commandbuffer
2573  */
2574 static void
2575 isdn_tty_getdial(char *p, char *q, int cnt)
2576 {
2577         int first = 1;
2578         int limit = ISDN_MSNLEN - 1;    /* MUST match the size of interface var to avoid
2579                                            buffer overflow */
2580
2581         while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
2582                 if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
2583                     ((*p == 'R') && first) ||
2584                     (*p == '*') || (*p == '#')) {
2585                         *q++ = *p;
2586                         limit--;
2587                 }
2588                 if (!limit)
2589                         break;
2590                 p++;
2591                 first = 0;
2592         }
2593         *q = 0;
2594 }
2595
2596 #define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
2597 #define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
2598
2599 static void
2600 isdn_tty_report(modem_info *info)
2601 {
2602         atemu *m = &info->emu;
2603         char s[80];
2604
2605         isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
2606         sprintf(s, "    Remote Number:    %s\r\n", info->last_num);
2607         isdn_tty_at_cout(s, info);
2608         sprintf(s, "    Direction:        %s\r\n", info->last_dir ? "outgoing" : "incoming");
2609         isdn_tty_at_cout(s, info);
2610         isdn_tty_at_cout("    Layer-2 Protocol: ", info);
2611         switch (info->last_l2) {
2612         case ISDN_PROTO_L2_X75I:
2613                 isdn_tty_at_cout("X.75i", info);
2614                 break;
2615         case ISDN_PROTO_L2_X75UI:
2616                 isdn_tty_at_cout("X.75ui", info);
2617                 break;
2618         case ISDN_PROTO_L2_X75BUI:
2619                 isdn_tty_at_cout("X.75bui", info);
2620                 break;
2621         case ISDN_PROTO_L2_HDLC:
2622                 isdn_tty_at_cout("HDLC", info);
2623                 break;
2624         case ISDN_PROTO_L2_V11096:
2625                 isdn_tty_at_cout("V.110 9600 Baud", info);
2626                 break;
2627         case ISDN_PROTO_L2_V11019:
2628                 isdn_tty_at_cout("V.110 19200 Baud", info);
2629                 break;
2630         case ISDN_PROTO_L2_V11038:
2631                 isdn_tty_at_cout("V.110 38400 Baud", info);
2632                 break;
2633         case ISDN_PROTO_L2_TRANS:
2634                 isdn_tty_at_cout("transparent", info);
2635                 break;
2636         case ISDN_PROTO_L2_MODEM:
2637                 isdn_tty_at_cout("modem", info);
2638                 break;
2639         case ISDN_PROTO_L2_FAX:
2640                 isdn_tty_at_cout("fax", info);
2641                 break;
2642         default:
2643                 isdn_tty_at_cout("unknown", info);
2644                 break;
2645         }
2646         if (m->mdmreg[REG_T70] & BIT_T70) {
2647                 isdn_tty_at_cout("/T.70", info);
2648                 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2649                         isdn_tty_at_cout("+", info);
2650         }
2651         isdn_tty_at_cout("\r\n", info);
2652         isdn_tty_at_cout("    Service:          ", info);
2653         switch (info->last_si) {
2654         case 1:
2655                 isdn_tty_at_cout("audio\r\n", info);
2656                 break;
2657         case 5:
2658                 isdn_tty_at_cout("btx\r\n", info);
2659                 break;
2660         case 7:
2661                 isdn_tty_at_cout("data\r\n", info);
2662                 break;
2663         default:
2664                 sprintf(s, "%d\r\n", info->last_si);
2665                 isdn_tty_at_cout(s, info);
2666                 break;
2667         }
2668         sprintf(s, "    Hangup location:  %s\r\n", info->last_lhup ? "local" : "remote");
2669         isdn_tty_at_cout(s, info);
2670         sprintf(s, "    Last cause:       %s\r\n", info->last_cause);
2671         isdn_tty_at_cout(s, info);
2672 }
2673
2674 /*
2675  * Parse AT&.. commands.
2676  */
2677 static int
2678 isdn_tty_cmd_ATand(char **p, modem_info *info)
2679 {
2680         atemu *m = &info->emu;
2681         int i;
2682         char rb[100];
2683
2684 #define MAXRB (sizeof(rb) - 1)
2685
2686         switch (*p[0]) {
2687         case 'B':
2688                 /* &B - Set Buffersize */
2689                 p[0]++;
2690                 i = isdn_getnum(p);
2691                 if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
2692                         PARSE_ERROR1;
2693 #ifdef CONFIG_ISDN_AUDIO
2694                 if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
2695                         PARSE_ERROR1;
2696 #endif
2697                 m->mdmreg[REG_PSIZE] = i / 16;
2698                 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2699                 switch (m->mdmreg[REG_L2PROT]) {
2700                 case ISDN_PROTO_L2_V11096:
2701                 case ISDN_PROTO_L2_V11019:
2702                 case ISDN_PROTO_L2_V11038:
2703                         info->xmit_size /= 10;
2704                 }
2705                 break;
2706         case 'C':
2707                 /* &C - DCD Status */
2708                 p[0]++;
2709                 switch (isdn_getnum(p)) {
2710                 case 0:
2711                         m->mdmreg[REG_DCD] &= ~BIT_DCD;
2712                         break;
2713                 case 1:
2714                         m->mdmreg[REG_DCD] |= BIT_DCD;
2715                         break;
2716                 default:
2717                         PARSE_ERROR1
2718                                 }
2719                 break;
2720         case 'D':
2721                 /* &D - Set DTR-Low-behavior */
2722                 p[0]++;
2723                 switch (isdn_getnum(p)) {
2724                 case 0:
2725                         m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
2726                         m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2727                         break;
2728                 case 2:
2729                         m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2730                         m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2731                         break;
2732                 case 3:
2733                         m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2734                         m->mdmreg[REG_DTRR] |= BIT_DTRR;
2735                         break;
2736                 default:
2737                         PARSE_ERROR1
2738                                 }
2739                 break;
2740         case 'E':
2741                 /* &E -Set EAZ/MSN */
2742                 p[0]++;
2743                 isdn_tty_get_msnstr(m->msn, p);
2744                 break;
2745         case 'F':
2746                 /* &F -Set Factory-Defaults */
2747                 p[0]++;
2748                 if (info->msr & UART_MSR_DCD)
2749                         PARSE_ERROR1;
2750                 isdn_tty_reset_profile(m);
2751                 isdn_tty_modem_reset_regs(info, 1);
2752                 break;
2753 #ifdef DUMMY_HAYES_AT
2754         case 'K':
2755                 /* only for be compilant with common scripts */
2756                 /* &K Flowcontrol - no function */
2757                 p[0]++;
2758                 isdn_getnum(p);
2759                 break;
2760 #endif
2761         case 'L':
2762                 /* &L -Set Numbers to listen on */
2763                 p[0]++;
2764                 i = 0;
2765                 while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
2766                        (i < ISDN_LMSNLEN - 1))
2767                         m->lmsn[i++] = *p[0]++;
2768                 m->lmsn[i] = '\0';
2769                 break;
2770         case 'R':
2771                 /* &R - Set V.110 bitrate adaption */
2772                 p[0]++;
2773                 i = isdn_getnum(p);
2774                 switch (i) {
2775                 case 0:
2776                         /* Switch off V.110, back to X.75 */
2777                         m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2778                         m->mdmreg[REG_SI2] = 0;
2779                         info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2780                         break;
2781                 case 9600:
2782                         m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
2783                         m->mdmreg[REG_SI2] = 197;
2784                         info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2785                         break;
2786                 case 19200:
2787                         m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
2788                         m->mdmreg[REG_SI2] = 199;
2789                         info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2790                         break;
2791                 case 38400:
2792                         m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
2793                         m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
2794                         info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2795                         break;
2796                 default:
2797                         PARSE_ERROR1;
2798                 }
2799                 /* Switch off T.70 */
2800                 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2801                 /* Set Service 7 */
2802                 m->mdmreg[REG_SI1] |= 4;
2803                 break;
2804         case 'S':
2805                 /* &S - Set Windowsize */
2806                 p[0]++;
2807                 i = isdn_getnum(p);
2808                 if ((i > 0) && (i < 9))
2809                         m->mdmreg[REG_WSIZE] = i;
2810                 else
2811                         PARSE_ERROR1;
2812                 break;
2813         case 'V':
2814                 /* &V - Show registers */
2815                 p[0]++;
2816                 isdn_tty_at_cout("\r\n", info);
2817                 for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
2818                         sprintf(rb, "S%02d=%03d%s", i,
2819                                 m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
2820                         isdn_tty_at_cout(rb, info);
2821                 }
2822                 sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
2823                         strlen(m->msn) ? m->msn : "None");
2824                 isdn_tty_at_cout(rb, info);
2825                 if (strlen(m->lmsn)) {
2826                         isdn_tty_at_cout("\r\nListen: ", info);
2827                         isdn_tty_at_cout(m->lmsn, info);
2828                         isdn_tty_at_cout("\r\n", info);
2829                 }
2830                 break;
2831         case 'W':
2832                 /* &W - Write Profile */
2833                 p[0]++;
2834                 switch (*p[0]) {
2835                 case '0':
2836                         p[0]++;
2837                         modem_write_profile(m);
2838                         break;
2839                 default:
2840                         PARSE_ERROR1;
2841                 }
2842                 break;
2843         case 'X':
2844                 /* &X - Switch to BTX-Mode and T.70 */
2845                 p[0]++;
2846                 switch (isdn_getnum(p)) {
2847                 case 0:
2848                         m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2849                         info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2850                         break;
2851                 case 1:
2852                         m->mdmreg[REG_T70] |= BIT_T70;
2853                         m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
2854                         m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2855                         info->xmit_size = 112;
2856                         m->mdmreg[REG_SI1] = 4;
2857                         m->mdmreg[REG_SI2] = 0;
2858                         break;
2859                 case 2:
2860                         m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
2861                         m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2862                         info->xmit_size = 112;
2863                         m->mdmreg[REG_SI1] = 4;
2864                         m->mdmreg[REG_SI2] = 0;
2865                         break;
2866                 default:
2867                         PARSE_ERROR1;
2868                 }
2869                 break;
2870         default:
2871                 PARSE_ERROR1;
2872         }
2873         return 0;
2874 }
2875
2876 static int
2877 isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
2878 {
2879         /* Some plausibility checks */
2880         switch (mreg) {
2881         case REG_L2PROT:
2882                 if (mval > ISDN_PROTO_L2_MAX)
2883                         return 1;
2884                 break;
2885         case REG_PSIZE:
2886                 if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
2887                         return 1;
2888 #ifdef CONFIG_ISDN_AUDIO
2889                 if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
2890                         return 1;
2891 #endif
2892                 info->xmit_size = mval * 16;
2893                 switch (m->mdmreg[REG_L2PROT]) {
2894                 case ISDN_PROTO_L2_V11096:
2895                 case ISDN_PROTO_L2_V11019:
2896                 case ISDN_PROTO_L2_V11038:
2897                         info->xmit_size /= 10;
2898                 }
2899                 break;
2900         case REG_SI1I:
2901         case REG_PLAN:
2902         case REG_SCREEN:
2903                 /* readonly registers */
2904                 return 1;
2905         }
2906         return 0;
2907 }
2908
2909 /*
2910  * Perform ATS command
2911  */
2912 static int
2913 isdn_tty_cmd_ATS(char **p, modem_info *info)
2914 {
2915         atemu *m = &info->emu;
2916         int bitpos;
2917         int mreg;
2918         int mval;
2919         int bval;
2920
2921         mreg = isdn_getnum(p);
2922         if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
2923                 PARSE_ERROR1;
2924         switch (*p[0]) {
2925         case '=':
2926                 p[0]++;
2927                 mval = isdn_getnum(p);
2928                 if (mval < 0 || mval > 255)
2929                         PARSE_ERROR1;
2930                 if (isdn_tty_check_ats(mreg, mval, info, m))
2931                         PARSE_ERROR1;
2932                 m->mdmreg[mreg] = mval;
2933                 break;
2934         case '.':
2935                 /* Set/Clear a single bit */
2936                 p[0]++;
2937                 bitpos = isdn_getnum(p);
2938                 if ((bitpos < 0) || (bitpos > 7))
2939                         PARSE_ERROR1;
2940                 switch (*p[0]) {
2941                 case '=':
2942                         p[0]++;
2943                         bval = isdn_getnum(p);
2944                         if (bval < 0 || bval > 1)
2945                                 PARSE_ERROR1;
2946                         if (bval)
2947                                 mval = m->mdmreg[mreg] | (1 << bitpos);
2948                         else
2949                                 mval = m->mdmreg[mreg] & ~(1 << bitpos);
2950                         if (isdn_tty_check_ats(mreg, mval, info, m))
2951                                 PARSE_ERROR1;
2952                         m->mdmreg[mreg] = mval;
2953                         break;
2954                 case '?':
2955                         p[0]++;
2956                         isdn_tty_at_cout("\r\n", info);
2957                         isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
2958                                          info);
2959                         break;
2960                 default:
2961                         PARSE_ERROR1;
2962                 }
2963                 break;
2964         case '?':
2965                 p[0]++;
2966                 isdn_tty_show_profile(mreg, info);
2967                 break;
2968         default:
2969                 PARSE_ERROR1;
2970                 break;
2971         }
2972         return 0;
2973 }
2974
2975 /*
2976  * Perform ATA command
2977  */
2978 static void
2979 isdn_tty_cmd_ATA(modem_info *info)
2980 {
2981         atemu *m = &info->emu;
2982         isdn_ctrl cmd;
2983         int l2;
2984
2985         if (info->msr & UART_MSR_RI) {
2986                 /* Accept incoming call */
2987                 info->last_dir = 0;
2988                 strcpy(info->last_num, dev->num[info->drv_index]);
2989                 m->mdmreg[REG_RINGCNT] = 0;
2990                 info->msr &= ~UART_MSR_RI;
2991                 l2 = m->mdmreg[REG_L2PROT];
2992 #ifdef CONFIG_ISDN_AUDIO
2993                 /* If more than one bit set in reg18, autoselect Layer2 */
2994                 if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
2995                         if (m->mdmreg[REG_SI1I] == 1) {
2996                                 if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
2997                                         l2 = ISDN_PROTO_L2_TRANS;
2998                         } else
2999                                 l2 = ISDN_PROTO_L2_X75I;
3000                 }
3001 #endif
3002                 cmd.driver = info->isdn_driver;
3003                 cmd.command = ISDN_CMD_SETL2;
3004                 cmd.arg = info->isdn_channel + (l2 << 8);
3005                 info->last_l2 = l2;
3006                 isdn_command(&cmd);
3007                 cmd.driver = info->isdn_driver;
3008                 cmd.command = ISDN_CMD_SETL3;
3009                 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
3010 #ifdef CONFIG_ISDN_TTY_FAX
3011                 if (l2 == ISDN_PROTO_L2_FAX) {
3012                         cmd.parm.fax = info->fax;
3013                         info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3014                 }
3015 #endif
3016                 isdn_command(&cmd);
3017                 cmd.driver = info->isdn_driver;
3018                 cmd.arg = info->isdn_channel;
3019                 cmd.command = ISDN_CMD_ACCEPTD;
3020                 info->dialing = 16;
3021                 info->emu.carrierwait = 0;
3022                 isdn_command(&cmd);
3023                 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3024         } else
3025                 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3026 }
3027
3028 #ifdef CONFIG_ISDN_AUDIO
3029 /*
3030  * Parse AT+F.. commands
3031  */
3032 static int
3033 isdn_tty_cmd_PLUSF(char **p, modem_info *info)
3034 {
3035         atemu *m = &info->emu;
3036         char rs[20];
3037
3038         if (!strncmp(p[0], "CLASS", 5)) {
3039                 p[0] += 5;
3040                 switch (*p[0]) {
3041                 case '?':
3042                         p[0]++;
3043                         sprintf(rs, "\r\n%d",
3044                                 (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3045 #ifdef CONFIG_ISDN_TTY_FAX
3046                         if (TTY_IS_FCLASS2(info))
3047                                 sprintf(rs, "\r\n2");
3048                         else if (TTY_IS_FCLASS1(info))
3049                                 sprintf(rs, "\r\n1");
3050 #endif
3051                         isdn_tty_at_cout(rs, info);
3052                         break;
3053                 case '=':
3054                         p[0]++;
3055                         switch (*p[0]) {
3056                         case '0':
3057                                 p[0]++;
3058                                 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3059                                 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3060                                 m->mdmreg[REG_SI1] = 4;
3061                                 info->xmit_size =
3062                                         m->mdmreg[REG_PSIZE] * 16;
3063                                 break;
3064 #ifdef CONFIG_ISDN_TTY_FAX
3065                         case '1':
3066                                 p[0]++;
3067                                 if (!(dev->global_features &
3068                                       ISDN_FEATURE_L3_FCLASS1))
3069                                         PARSE_ERROR1;
3070                                 m->mdmreg[REG_SI1] = 1;
3071                                 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3072                                 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3073                                 info->xmit_size =
3074                                         m->mdmreg[REG_PSIZE] * 16;
3075                                 break;
3076                         case '2':
3077                                 p[0]++;
3078                                 if (!(dev->global_features &
3079                                       ISDN_FEATURE_L3_FCLASS2))
3080                                         PARSE_ERROR1;
3081                                 m->mdmreg[REG_SI1] = 1;
3082                                 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3083                                 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3084                                 info->xmit_size =
3085                                         m->mdmreg[REG_PSIZE] * 16;
3086                                 break;
3087 #endif
3088                         case '8':
3089                                 p[0]++;
3090                                 /* L2 will change on dialout with si=1 */
3091                                 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3092                                 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3093                                 m->mdmreg[REG_SI1] = 5;
3094                                 info->xmit_size = VBUF;
3095                                 break;
3096                         case '?':
3097                                 p[0]++;
3098                                 strcpy(rs, "\r\n0,");
3099 #ifdef CONFIG_ISDN_TTY_FAX
3100                                 if (dev->global_features &
3101                                     ISDN_FEATURE_L3_FCLASS1)
3102                                         strcat(rs, "1,");
3103                                 if (dev->global_features &
3104                                     ISDN_FEATURE_L3_FCLASS2)
3105                                         strcat(rs, "2,");
3106 #endif
3107                                 strcat(rs, "8");
3108                                 isdn_tty_at_cout(rs, info);
3109                                 break;
3110                         default:
3111                                 PARSE_ERROR1;
3112                         }
3113                         break;
3114                 default:
3115                         PARSE_ERROR1;
3116                 }
3117                 return 0;
3118         }
3119 #ifdef CONFIG_ISDN_TTY_FAX
3120         return (isdn_tty_cmd_PLUSF_FAX(p, info));
3121 #else
3122         PARSE_ERROR1;
3123 #endif
3124 }
3125
3126 /*
3127  * Parse AT+V.. commands
3128  */
3129 static int
3130 isdn_tty_cmd_PLUSV(char **p, modem_info *info)
3131 {
3132         atemu *m = &info->emu;
3133         isdn_ctrl cmd;
3134         static char *vcmd[] =
3135                 {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
3136         int i;
3137         int par1;
3138         int par2;
3139         char rs[20];
3140
3141         i = 0;
3142         while (vcmd[i]) {
3143                 if (!strncmp(vcmd[i], p[0], 2)) {
3144                         p[0] += 2;
3145                         break;
3146                 }
3147                 i++;
3148         }
3149         switch (i) {
3150         case 0:
3151                 /* AT+VNH - Auto hangup feature */
3152                 switch (*p[0]) {
3153                 case '?':
3154                         p[0]++;
3155                         isdn_tty_at_cout("\r\n1", info);
3156                         break;
3157                 case '=':
3158                         p[0]++;
3159                         switch (*p[0]) {
3160                         case '1':
3161                                 p[0]++;
3162                                 break;
3163                         case '?':
3164                                 p[0]++;
3165                                 isdn_tty_at_cout("\r\n1", info);
3166                                 break;
3167                         default:
3168                                 PARSE_ERROR1;
3169                         }
3170                         break;
3171                 default:
3172                         PARSE_ERROR1;
3173                 }
3174                 break;
3175         case 1:
3176                 /* AT+VIP - Reset all voice parameters */
3177                 isdn_tty_modem_reset_vpar(m);
3178                 break;
3179         case 2:
3180                 /* AT+VLS - Select device, accept incoming call */
3181                 switch (*p[0]) {
3182                 case '?':
3183                         p[0]++;
3184                         sprintf(rs, "\r\n%d", m->vpar[0]);
3185                         isdn_tty_at_cout(rs, info);
3186                         break;
3187                 case '=':
3188                         p[0]++;
3189                         switch (*p[0]) {
3190                         case '0':
3191                                 p[0]++;
3192                                 m->vpar[0] = 0;
3193                                 break;
3194                         case '2':
3195                                 p[0]++;
3196                                 m->vpar[0] = 2;
3197                                 break;
3198                         case '?':
3199                                 p[0]++;
3200                                 isdn_tty_at_cout("\r\n0,2", info);
3201                                 break;
3202                         default:
3203                                 PARSE_ERROR1;
3204                         }
3205                         break;
3206                 default:
3207                         PARSE_ERROR1;
3208                 }
3209                 break;
3210         case 3:
3211                 /* AT+VRX - Start recording */
3212                 if (!m->vpar[0])
3213                         PARSE_ERROR1;
3214                 if (info->online != 1) {
3215                         isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3216                         return 1;
3217                 }
3218                 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3219                 if (!info->dtmf_state) {
3220                         printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3221                         PARSE_ERROR1;
3222                 }
3223                 info->silence_state = isdn_audio_silence_init(info->silence_state);
3224                 if (!info->silence_state) {
3225                         printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3226                         PARSE_ERROR1;
3227                 }
3228                 if (m->vpar[3] < 5) {
3229                         info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3230                         if (!info->adpcmr) {
3231                                 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3232                                 PARSE_ERROR1;
3233                         }
3234                 }
3235 #ifdef ISDN_DEBUG_AT
3236                 printk(KERN_DEBUG "AT: +VRX\n");
3237 #endif
3238                 info->vonline |= 1;
3239                 isdn_tty_modem_result(RESULT_CONNECT, info);
3240                 return 0;
3241                 break;
3242         case 4:
3243                 /* AT+VSD - Silence detection */
3244                 switch (*p[0]) {
3245                 case '?':
3246                         p[0]++;
3247                         sprintf(rs, "\r\n<%d>,<%d>",
3248                                 m->vpar[1],
3249                                 m->vpar[2]);
3250                         isdn_tty_at_cout(rs, info);
3251                         break;
3252                 case '=':
3253                         p[0]++;
3254                         if ((*p[0] >= '0') && (*p[0] <= '9')) {
3255                                 par1 = isdn_getnum(p);
3256                                 if ((par1 < 0) || (par1 > 31))
3257                                         PARSE_ERROR1;
3258                                 if (*p[0] != ',')
3259                                         PARSE_ERROR1;
3260                                 p[0]++;
3261                                 par2 = isdn_getnum(p);
3262                                 if ((par2 < 0) || (par2 > 255))
3263                                         PARSE_ERROR1;
3264                                 m->vpar[1] = par1;
3265                                 m->vpar[2] = par2;
3266                                 break;
3267                         } else
3268                                 if (*p[0] == '?') {
3269                                         p[0]++;
3270                                         isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3271                                                          info);
3272                                         break;
3273                                 } else
3274                                         PARSE_ERROR1;
3275                         break;
3276                 default:
3277                         PARSE_ERROR1;
3278                 }
3279                 break;
3280         case 5:
3281                 /* AT+VSM - Select compression */
3282                 switch (*p[0]) {
3283                 case '?':
3284                         p[0]++;
3285                         sprintf(rs, "\r\n<%d>,<%d><8000>",
3286                                 m->vpar[3],
3287                                 m->vpar[1]);
3288                         isdn_tty_at_cout(rs, info);
3289                         break;
3290                 case '=':
3291                         p[0]++;
3292                         switch (*p[0]) {
3293                         case '2':
3294                         case '3':
3295                         case '4':
3296                         case '5':
3297                         case '6':
3298                                 par1 = isdn_getnum(p);
3299                                 if ((par1 < 2) || (par1 > 6))
3300                                         PARSE_ERROR1;
3301                                 m->vpar[3] = par1;
3302                                 break;
3303                         case '?':
3304                                 p[0]++;
3305                                 isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3306                                                  info);
3307                                 isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3308                                                  info);
3309                                 isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3310                                                  info);
3311                                 isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3312                                                  info);
3313                                 isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3314                                                  info);
3315                                 break;
3316                         default:
3317                                 PARSE_ERROR1;
3318                         }
3319                         break;
3320                 default:
3321                         PARSE_ERROR1;
3322                 }
3323                 break;
3324         case 6:
3325                 /* AT+VTX - Start sending */
3326                 if (!m->vpar[0])
3327                         PARSE_ERROR1;
3328                 if (info->online != 1) {
3329                         isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3330                         return 1;
3331                 }
3332                 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3333                 if (!info->dtmf_state) {
3334                         printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3335                         PARSE_ERROR1;
3336                 }
3337                 if (m->vpar[3] < 5) {
3338                         info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3339                         if (!info->adpcms) {
3340                                 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3341                                 PARSE_ERROR1;
3342                         }
3343                 }
3344 #ifdef ISDN_DEBUG_AT
3345                 printk(KERN_DEBUG "AT: +VTX\n");
3346 #endif
3347                 m->lastDLE = 0;
3348                 info->vonline |= 2;
3349                 isdn_tty_modem_result(RESULT_CONNECT, info);
3350                 return 0;
3351                 break;
3352         case 7:
3353                 /* AT+VDD - DTMF detection */
3354                 switch (*p[0]) {
3355                 case '?':
3356                         p[0]++;
3357                         sprintf(rs, "\r\n<%d>,<%d>",
3358                                 m->vpar[4],
3359                                 m->vpar[5]);
3360                         isdn_tty_at_cout(rs, info);
3361                         break;
3362                 case '=':
3363                         p[0]++;
3364                         if ((*p[0] >= '0') && (*p[0] <= '9')) {
3365                                 if (info->online != 1)
3366                                         PARSE_ERROR1;
3367                                 par1 = isdn_getnum(p);
3368                                 if ((par1 < 0) || (par1 > 15))
3369                                         PARSE_ERROR1;
3370                                 if (*p[0] != ',')
3371                                         PARSE_ERROR1;
3372                                 p[0]++;
3373                                 par2 = isdn_getnum(p);
3374                                 if ((par2 < 0) || (par2 > 255))
3375                                         PARSE_ERROR1;
3376                                 m->vpar[4] = par1;
3377                                 m->vpar[5] = par2;
3378                                 cmd.driver = info->isdn_driver;
3379                                 cmd.command = ISDN_CMD_AUDIO;
3380                                 cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3381                                 cmd.parm.num[0] = par1;
3382                                 cmd.parm.num[1] = par2;
3383                                 isdn_command(&cmd);
3384                                 break;
3385                         } else
3386                                 if (*p[0] == '?') {
3387                                         p[0]++;
3388                                         isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3389                                                          info);
3390                                         break;
3391                                 } else
3392                                         PARSE_ERROR1;
3393                         break;
3394                 default:
3395                         PARSE_ERROR1;
3396                 }
3397                 break;
3398         default:
3399                 PARSE_ERROR1;
3400         }
3401         return 0;
3402 }
3403 #endif                          /* CONFIG_ISDN_AUDIO */
3404
3405 /*
3406  * Parse and perform an AT-command-line.
3407  */
3408 static void
3409 isdn_tty_parse_at(modem_info *info)
3410 {
3411         atemu *m = &info->emu;
3412         char *p;
3413         char ds[ISDN_MSNLEN];
3414
3415 #ifdef ISDN_DEBUG_AT
3416         printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
3417 #endif
3418         for (p = &m->mdmcmd[2]; *p;) {
3419                 switch (*p) {
3420                 case ' ':
3421                         p++;
3422                         break;
3423                 case 'A':
3424                         /* A - Accept incoming call */
3425                         p++;
3426                         isdn_tty_cmd_ATA(info);
3427                         return;
3428                         break;
3429                 case 'D':
3430                         /* D - Dial */
3431                         if (info->msr & UART_MSR_DCD)
3432                                 PARSE_ERROR;
3433                         if (info->msr & UART_MSR_RI) {
3434                                 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3435                                 return;
3436                         }
3437                         isdn_tty_getdial(++p, ds, sizeof ds);
3438                         p += strlen(p);
3439                         if (!strlen(m->msn))
3440                                 isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
3441                         else if (strlen(ds))
3442                                 isdn_tty_dial(ds, info, m);
3443                         else
3444                                 PARSE_ERROR;
3445                         return;
3446                 case 'E':
3447                         /* E - Turn Echo on/off */
3448                         p++;
3449                         switch (isdn_getnum(&p)) {
3450                         case 0:
3451                                 m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
3452                                 break;
3453                         case 1:
3454                                 m->mdmreg[REG_ECHO] |= BIT_ECHO;
3455                                 break;
3456                         default:
3457                                 PARSE_ERROR;
3458                         }
3459                         break;
3460                 case 'H':
3461                         /* H - On/Off-hook */
3462                         p++;
3463                         switch (*p) {
3464                         case '0':
3465                                 p++;
3466                                 isdn_tty_on_hook(info);
3467                                 break;
3468                         case '1':
3469                                 p++;
3470                                 isdn_tty_off_hook();
3471                                 break;
3472                         default:
3473                                 isdn_tty_on_hook(info);
3474                                 break;
3475                         }
3476                         break;
3477                 case 'I':
3478                         /* I - Information */
3479                         p++;
3480                         isdn_tty_at_cout("\r\nLinux ISDN", info);
3481                         switch (*p) {
3482                         case '0':
3483                         case '1':
3484                                 p++;
3485                                 break;
3486                         case '2':
3487                                 p++;
3488                                 isdn_tty_report(info);
3489                                 break;
3490                         case '3':
3491                                 p++;
3492                                 snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
3493                                 isdn_tty_at_cout(ds, info);
3494                                 break;
3495                         default:;
3496                         }
3497                         break;
3498 #ifdef DUMMY_HAYES_AT
3499                 case 'L':
3500                 case 'M':
3501                         /* only for be compilant with common scripts */
3502                         /* no function */
3503                         p++;
3504                         isdn_getnum(&p);
3505                         break;
3506 #endif
3507                 case 'O':
3508                         /* O - Go online */
3509                         p++;
3510                         if (info->msr & UART_MSR_DCD)
3511                                 /* if B-Channel is up */
3512                                 isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
3513                         else
3514                                 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3515                         return;
3516                 case 'Q':
3517                         /* Q - Turn Emulator messages on/off */
3518                         p++;
3519                         switch (isdn_getnum(&p)) {
3520                         case 0:
3521                                 m->mdmreg[REG_RESP] |= BIT_RESP;
3522                                 break;
3523                         case 1:
3524                                 m->mdmreg[REG_RESP] &= ~BIT_RESP;
3525                                 break;
3526                         default:
3527                                 PARSE_ERROR;
3528                         }
3529                         break;
3530                 case 'S':
3531                         /* S - Set/Get Register */
3532                         p++;
3533                         if (isdn_tty_cmd_ATS(&p, info))
3534                                 return;
3535                         break;
3536                 case 'V':
3537                         /* V - Numeric or ASCII Emulator-messages */
3538                         p++;
3539                         switch (isdn_getnum(&p)) {
3540                         case 0:
3541                                 m->mdmreg[REG_RESP] |= BIT_RESPNUM;
3542                                 break;
3543                         case 1:
3544                                 m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
3545                                 break;
3546                         default:
3547                                 PARSE_ERROR;
3548                         }
3549                         break;
3550                 case 'Z':
3551                         /* Z - Load Registers from Profile */
3552                         p++;
3553                         if (info->msr & UART_MSR_DCD) {
3554                                 info->online = 0;
3555                                 isdn_tty_on_hook(info);
3556                         }
3557                         isdn_tty_modem_reset_regs(info, 1);
3558                         break;
3559                 case '+':
3560                         p++;
3561                         switch (*p) {
3562 #ifdef CONFIG_ISDN_AUDIO
3563                         case 'F':
3564                                 p++;
3565                                 if (isdn_tty_cmd_PLUSF(&p, info))
3566                                         return;
3567                                 break;
3568                         case 'V':
3569                                 if ((!(m->mdmreg[REG_SI1] & 1)) ||
3570                                     (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
3571                                         PARSE_ERROR;
3572                                 p++;
3573                                 if (isdn_tty_cmd_PLUSV(&p, info))
3574                                         return;
3575                                 break;
3576 #endif                          /* CONFIG_ISDN_AUDIO */
3577                         case 'S':       /* SUSPEND */
3578                                 p++;
3579                                 isdn_tty_get_msnstr(ds, &p);
3580                                 isdn_tty_suspend(ds, info, m);
3581                                 break;
3582                         case 'R':       /* RESUME */
3583                                 p++;
3584                                 isdn_tty_get_msnstr(ds, &p);
3585                                 isdn_tty_resume(ds, info, m);
3586                                 break;
3587                         case 'M':       /* MESSAGE */
3588                                 p++;
3589                                 isdn_tty_send_msg(info, m, p);
3590                                 break;
3591                         default:
3592                                 PARSE_ERROR;
3593                         }
3594                         break;
3595                 case '&':
3596                         p++;
3597                         if (isdn_tty_cmd_ATand(&p, info))
3598                                 return;
3599                         break;
3600                 default:
3601                         PARSE_ERROR;
3602                 }
3603         }
3604 #ifdef CONFIG_ISDN_AUDIO
3605         if (!info->vonline)
3606 #endif
3607                 isdn_tty_modem_result(RESULT_OK, info);
3608 }
3609
3610 /* Need own toupper() because standard-toupper is not available
3611  * within modules.
3612  */
3613 #define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
3614
3615 /*
3616  * Perform line-editing of AT-commands
3617  *
3618  * Parameters:
3619  *   p        inputbuffer
3620  *   count    length of buffer
3621  *   channel  index to line (minor-device)
3622  */
3623 static int
3624 isdn_tty_edit_at(const char *p, int count, modem_info *info)
3625 {
3626         atemu *m = &info->emu;
3627         int total = 0;
3628         u_char c;
3629         char eb[2];
3630         int cnt;
3631
3632         for (cnt = count; cnt > 0; p++, cnt--) {
3633                 c = *p;
3634                 total++;
3635                 if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
3636                         /* Separator (CR or LF) */
3637                         m->mdmcmd[m->mdmcmdl] = 0;
3638                         if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3639                                 eb[0] = c;
3640                                 eb[1] = 0;
3641                                 isdn_tty_at_cout(eb, info);
3642                         }
3643                         if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
3644                                 isdn_tty_parse_at(info);
3645                         m->mdmcmdl = 0;
3646                         continue;
3647                 }
3648                 if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
3649                         /* Backspace-Function */
3650                         if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
3651                                 if (m->mdmcmdl)
3652                                         m->mdmcmdl--;
3653                                 if (m->mdmreg[REG_ECHO] & BIT_ECHO)
3654                                         isdn_tty_at_cout("\b", info);
3655                         }
3656                         continue;
3657                 }
3658                 if (cmdchar(c)) {
3659                         if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3660                                 eb[0] = c;
3661                                 eb[1] = 0;
3662                                 isdn_tty_at_cout(eb, info);
3663                         }
3664                         if (m->mdmcmdl < 255) {
3665                                 c = my_toupper(c);
3666                                 switch (m->mdmcmdl) {
3667                                 case 1:
3668                                         if (c == 'T') {
3669                                                 m->mdmcmd[m->mdmcmdl] = c;
3670                                                 m->mdmcmd[++m->mdmcmdl] = 0;
3671                                                 break;
3672                                         } else
3673                                                 m->mdmcmdl = 0;
3674                                         /* Fall through, check for 'A' */
3675                                 case 0:
3676                                         if (c == 'A') {
3677                                                 m->mdmcmd[m->mdmcmdl] = c;
3678                                                 m->mdmcmd[++m->mdmcmdl] = 0;
3679                                         }
3680                                         break;
3681                                 default:
3682                                         m->mdmcmd[m->mdmcmdl] = c;
3683                                         m->mdmcmd[++m->mdmcmdl] = 0;
3684                                 }
3685                         }
3686                 }
3687         }
3688         return total;
3689 }
3690
3691 /*
3692  * Switch all modem-channels who are online and got a valid
3693  * escape-sequence 1.5 seconds ago, to command-mode.
3694  * This function is called every second via timer-interrupt from within
3695  * timer-dispatcher isdn_timer_function()
3696  */
3697 void
3698 isdn_tty_modem_escape(void)
3699 {
3700         int ton = 0;
3701         int i;
3702         int midx;
3703
3704         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
3705                 if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
3706                         modem_info *info = &dev->mdm.info[midx];
3707                         if (info->online) {
3708                                 ton = 1;
3709                                 if ((info->emu.pluscount == 3) &&
3710                                     time_after(jiffies,
3711                                             info->emu.lastplus + PLUSWAIT2)) {
3712                                         info->emu.pluscount = 0;
3713                                         info->online = 0;
3714                                         isdn_tty_modem_result(RESULT_OK, info);
3715                                 }
3716                         }
3717                 }
3718         isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
3719 }
3720
3721 /*
3722  * Put a RING-message to all modem-channels who have the RI-bit set.
3723  * This function is called every second via timer-interrupt from within
3724  * timer-dispatcher isdn_timer_function()
3725  */
3726 void
3727 isdn_tty_modem_ring(void)
3728 {
3729         int ton = 0;
3730         int i;
3731
3732         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3733                 modem_info *info = &dev->mdm.info[i];
3734                 if (info->msr & UART_MSR_RI) {
3735                         ton = 1;
3736                         isdn_tty_modem_result(RESULT_RING, info);
3737                 }
3738         }
3739         isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
3740 }
3741
3742 /*
3743  * For all online tty's, try sending data to
3744  * the lower levels.
3745  */
3746 void
3747 isdn_tty_modem_xmit(void)
3748 {
3749         int ton = 1;
3750         int i;
3751
3752         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3753                 modem_info *info = &dev->mdm.info[i];
3754                 if (info->online) {
3755                         ton = 1;
3756                         isdn_tty_senddown(info);
3757                         isdn_tty_tint(info);
3758                 }
3759         }
3760         isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
3761 }
3762
3763 /*
3764  * Check all channels if we have a 'no carrier' timeout.
3765  * Timeout value is set by Register S7.
3766  */
3767 void
3768 isdn_tty_carrier_timeout(void)
3769 {
3770         int ton = 0;
3771         int i;
3772
3773         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3774                 modem_info *info = &dev->mdm.info[i];
3775                 if (!info->dialing)
3776                         continue;
3777                 if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
3778                         info->dialing = 0;
3779                         isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3780                         isdn_tty_modem_hup(info, 1);
3781                 } else
3782                         ton = 1;
3783         }
3784         isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
3785 }