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