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