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