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