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