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