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