x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / isdn / i4l / isdn_tty.c
blob1b169559a240b0c41b9fb8c34d4837ea55d96f77
1 /*
2 * Linux ISDN subsystem, tty functions and AT-command emulator (linklevel).
4 * Copyright 1994-1999 by Fritz Elfert (fritz@isdn4linux.de)
5 * Copyright 1995,96 by Thinking Objects Software GmbH Wuerzburg
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
11 #undef ISDN_TTY_STAT_DEBUG
13 #include <linux/isdn.h>
14 #include <linux/serial.h> /* ASYNC_* flags */
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/mutex.h>
18 #include <linux/sched/signal.h>
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
27 #define FIX_FILE_TRANSFER
28 #define DUMMY_HAYES_AT
30 /* Prototypes */
32 static DEFINE_MUTEX(modem_info_mutex);
33 static int isdn_tty_edit_at(const char *, int, modem_info *);
34 static void isdn_tty_check_esc(const u_char *, u_char, int, int *, u_long *);
35 static void isdn_tty_modem_reset_regs(modem_info *, int);
36 static void isdn_tty_cmd_ATA(modem_info *);
37 static void isdn_tty_flush_buffer(struct tty_struct *);
38 static void isdn_tty_modem_result(int, modem_info *);
39 #ifdef CONFIG_ISDN_AUDIO
40 static int isdn_tty_countDLE(unsigned char *, int);
41 #endif
43 /* Leave this unchanged unless you know what you do! */
44 #define MODEM_PARANOIA_CHECK
45 #define MODEM_DO_RESTART
47 static int bit2si[8] =
48 {1, 5, 7, 7, 7, 7, 7, 7};
49 static int si2bit[8] =
50 {4, 1, 4, 4, 4, 4, 4, 4};
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().
61 static int
62 isdn_tty_try_read(modem_info *info, struct sk_buff *skb)
64 struct tty_port *port = &info->port;
65 int c;
66 int len;
67 char last;
69 if (!info->online)
70 return 0;
72 if (!(info->mcr & UART_MCR_RTS))
73 return 0;
75 len = skb->len
76 #ifdef CONFIG_ISDN_AUDIO
77 + ISDN_AUDIO_SKB_DLECOUNT(skb)
78 #endif
81 c = tty_buffer_request_room(port, len);
82 if (c < len)
83 return 0;
85 #ifdef CONFIG_ISDN_AUDIO
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)
91 tty_insert_flip_char(port, DLE, 0);
92 tty_insert_flip_char(port, *dp++, 0);
94 if (*dp == DLE)
95 tty_insert_flip_char(port, DLE, 0);
96 last = *dp;
97 } else {
98 #endif
99 if (len > 1)
100 tty_insert_flip_string(port, skb->data, len - 1);
101 last = skb->data[len - 1];
102 #ifdef CONFIG_ISDN_AUDIO
104 #endif
105 if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
106 tty_insert_flip_char(port, last, 0xFF);
107 else
108 tty_insert_flip_char(port, last, TTY_NORMAL);
109 tty_flip_buffer_push(port);
110 kfree_skb(skb);
112 return 1;
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.
119 void
120 isdn_tty_readmodem(void)
122 int resched = 0;
123 int midx;
124 int i;
125 int r;
126 modem_info *info;
128 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
129 midx = dev->m_idx[i];
130 if (midx < 0)
131 continue;
133 info = &dev->mdm.info[midx];
134 if (!info->online)
135 continue;
137 r = 0;
138 #ifdef CONFIG_ISDN_AUDIO
139 isdn_audio_eval_dtmf(info);
140 if ((info->vonline & 1) && (info->emu.vpar[1]))
141 isdn_audio_eval_silence(info);
142 #endif
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);
155 } else
156 r = 1;
158 if (r) {
159 info->rcvsched = 0;
160 resched = 1;
161 } else
162 info->rcvsched = 1;
164 if (!resched)
165 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
169 isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
171 ulong flags;
172 int midx;
173 #ifdef CONFIG_ISDN_AUDIO
174 int ifmt;
175 #endif
176 modem_info *info;
178 if ((midx = dev->m_idx[i]) < 0) {
179 /* if midx is invalid, packet is not for tty */
180 return 0;
182 info = &dev->mdm.info[midx];
183 #ifdef CONFIG_ISDN_AUDIO
184 ifmt = 1;
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
196 /* If Modem not listening, drop data */
197 kfree_skb(skb);
198 return 1;
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);
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]) {
219 case 2:
220 case 3:
221 case 4:
222 /* adpcm
223 * Since compressed data takes less
224 * space, we can overwrite the buffer.
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;
243 ISDN_AUDIO_SKB_DLECOUNT(skb) =
244 isdn_tty_countDLE(skb->data, skb->len);
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);
254 #endif
255 #endif
256 /* Try to deliver directly via tty-buf if queue is empty */
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;
263 /* Direct deliver failed or queue wasn't empty.
264 * Queue up for later dequeueing via timer-irq.
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
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;
280 static void
281 isdn_tty_cleanup_xmit(modem_info *info)
283 skb_queue_purge(&info->xmit_queue);
284 #ifdef CONFIG_ISDN_AUDIO
285 skb_queue_purge(&info->dtmf_queue);
286 #endif
289 static void
290 isdn_tty_tint(modem_info *info)
292 struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
293 int len, slen;
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) {
300 struct tty_struct *tty = info->port.tty;
301 info->send_outstanding++;
302 info->msr &= ~UART_MSR_CTS;
303 info->lsr &= ~UART_LSR_TEMT;
304 tty_wakeup(tty);
305 return;
307 if (slen < 0) {
308 /* Error: no channel, already shutdown, or wrong parameter */
309 dev_kfree_skb(skb);
310 return;
312 skb_queue_head(&info->xmit_queue, skb);
315 #ifdef CONFIG_ISDN_AUDIO
316 static int
317 isdn_tty_countDLE(unsigned char *buf, int len)
319 int count = 0;
321 while (len--)
322 if (*buf++ == DLE)
323 count++;
324 return count;
327 /* This routine is called from within isdn_tty_write() to perform
328 * DLE-decoding when sending audio-data.
330 static int
331 isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
333 unsigned char *p = &info->port.xmit_buf[info->xmit_count];
334 int count = 0;
336 while (len > 0) {
337 if (m->lastDLE) {
338 m->lastDLE = 0;
339 switch (*p) {
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;
354 #ifdef ISDN_DEBUG_MODEM_VOICE
355 printk(KERN_DEBUG
356 "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
357 info->line);
358 #endif
359 isdn_tty_at_cout("\020\003", info);
360 if (!info->vonline) {
361 #ifdef ISDN_DEBUG_MODEM_VOICE
362 printk(KERN_DEBUG
363 "DLEdown: send VCON on ttyI%d\n",
364 info->line);
365 #endif
366 isdn_tty_at_cout("\r\nVCON\r\n", info);
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;
377 } else {
378 if (*p == DLE)
379 m->lastDLE = 1;
380 else
381 count++;
383 p++;
384 len--;
386 if (len < 0) {
387 printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
388 return 0;
390 return count;
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.
397 static int
398 isdn_tty_end_vrx(const char *buf, int c)
400 char ch;
402 while (c--) {
403 ch = *buf;
404 if ((ch != 0x11) && (ch != 0x13))
405 return 1;
406 buf++;
408 return 0;
411 static int voice_cf[7] =
412 {0, 0, 4, 3, 2, 0, 0};
414 #endif /* CONFIG_ISDN_AUDIO */
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.
421 static void
422 isdn_tty_senddown(modem_info *info)
424 int buflen;
425 int skb_res;
426 #ifdef CONFIG_ISDN_AUDIO
427 int audio_len;
428 #endif
429 struct sk_buff *skb;
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);
443 #endif
444 if (!(buflen = info->xmit_count))
445 return;
446 if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
447 info->msr &= ~UART_MSR_CTS;
448 info->lsr &= ~UART_LSR_TEMT;
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.
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;
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;
476 skb_reserve(skb, skb_res);
477 memcpy(skb_put(skb, buflen), info->port.xmit_buf, buflen);
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.
487 int ifmt = 1;
489 /* voice conversion/decompression */
490 switch (info->emu.vpar[3]) {
491 case 2:
492 case 3:
493 case 4:
494 /* adpcm, compatible to ZyXel 1496 modem
495 * with ROM revision 6.01
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;
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);
527 skb_queue_tail(&info->xmit_queue, skb);
530 /************************************************************
532 * Modem-functions
534 * mostly "stolen" from original Linux-serial.c and friends.
536 ************************************************************/
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
541 * into the tty's buffer.
543 static void
544 isdn_tty_modem_do_ncarrier(unsigned long data)
546 modem_info *info = (modem_info *) data;
547 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
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.
555 static void
556 isdn_tty_modem_ncarrier(modem_info *info)
558 if (info->ncarrier) {
559 info->nc_timer.expires = jiffies + HZ;
560 add_timer(&info->nc_timer);
565 * return the usage calculated by si and layer 2 protocol
567 static int
568 isdn_calc_usage(int si, int l2)
570 int usg = ISDN_USAGE_MODEM;
572 #ifdef CONFIG_ISDN_AUDIO
573 if (si == 1) {
574 switch (l2) {
575 case ISDN_PROTO_L2_MODEM:
576 usg = ISDN_USAGE_MODEM;
577 break;
578 #ifdef CONFIG_ISDN_TTY_FAX
579 case ISDN_PROTO_L2_FAX:
580 usg = ISDN_USAGE_FAX;
581 break;
582 #endif
583 case ISDN_PROTO_L2_TRANS:
584 default:
585 usg = ISDN_USAGE_VOICE;
586 break;
589 #endif
590 return (usg);
593 /* isdn_tty_dial() performs dialing of a tty an the necessary
594 * setup of the lower levels before that.
596 static void
597 isdn_tty_dial(char *n, modem_info *info, atemu *m)
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;
607 for (j = 7; j >= 0; j--)
608 if (m->mdmreg[REG_SI1] & (1 << j)) {
609 si = bit2si[j];
610 break;
612 usg = isdn_calc_usage(si, l2);
613 #ifdef CONFIG_ISDN_AUDIO
614 if ((si == 1) &&
615 (l2 != ISDN_PROTO_L2_MODEM)
616 #ifdef CONFIG_ISDN_TTY_FAX
617 && (l2 != ISDN_PROTO_L2_FAX)
618 #endif
620 l2 = ISDN_PROTO_L2_TRANS;
621 usg = ISDN_USAGE_VOICE;
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;
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);
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.
684 void
685 isdn_tty_modem_hup(modem_info *info, int local)
687 isdn_ctrl cmd;
688 int di, ch;
690 if (!info)
691 return;
693 di = info->isdn_driver;
694 ch = info->isdn_channel;
695 if (di < 0 || ch < 0)
696 return;
698 info->isdn_driver = -1;
699 info->isdn_channel = -1;
701 #ifdef ISDN_DEBUG_MODEM_HUP
702 printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
703 #endif
704 info->rcvsched = 0;
705 isdn_tty_flush_buffer(info->port.tty);
706 if (info->online) {
707 info->last_lhup = local;
708 info->online = 0;
709 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
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;
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;
727 #endif
728 if ((info->msr & UART_MSR_RI) &&
729 (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
730 isdn_tty_modem_result(RESULT_RUNG, info);
731 info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
732 info->lsr |= UART_LSR_TEMT;
734 if (local) {
735 cmd.driver = di;
736 cmd.command = ISDN_CMD_HANGUP;
737 cmd.arg = ch;
738 isdn_command(&cmd);
741 isdn_all_eaz(di, ch);
742 info->emu.mdmreg[REG_RINGCNT] = 0;
743 isdn_free_channel(di, ch, 0);
745 if (info->drv_index >= 0) {
746 dev->m_idx[info->drv_index] = -1;
747 info->drv_index = -1;
752 * Begin of a CAPI like interface, currently used only for
753 * supplementary service (CAPI 2.0 part III)
755 #include <linux/isdn/capicmd.h>
756 #include <linux/module.h>
759 isdn_tty_capi_facility(capi_msg *cm) {
760 return (-1); /* dummy */
763 /* isdn_tty_suspend() tries to suspend the current tty connection
765 static void
766 isdn_tty_suspend(char *id, modem_info *info, atemu *m)
768 isdn_ctrl cmd;
770 int l;
772 if (!info)
773 return;
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)) {
780 cmd.parm.cmsg.Length = l + 18;
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;
790 strncpy(&cmd.parm.cmsg.para[6], id, l);
791 cmd.command = CAPI_PUT_MESSAGE;
792 cmd.driver = info->isdn_driver;
793 cmd.arg = info->isdn_channel;
794 isdn_command(&cmd);
798 /* isdn_tty_resume() tries to resume a suspended call
799 * setup of the lower levels before that. unfortunately here is no
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.
805 static void
806 isdn_tty_resume(char *id, modem_info *info, atemu *m)
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;
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;
823 usg = isdn_calc_usage(si, l2);
824 #ifdef CONFIG_ISDN_AUDIO
825 if ((si == 1) &&
826 (l2 != ISDN_PROTO_L2_MODEM)
827 #ifdef CONFIG_ISDN_TTY_FAX
828 && (l2 != ISDN_PROTO_L2_FAX)
829 #endif
831 l2 = ISDN_PROTO_L2_TRANS;
832 usg = ISDN_USAGE_VOICE;
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;
870 cmd.parm.cmsg.Length = l + 18;
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;
876 cmd.parm.cmsg.para[2] = l + 3;
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;
880 strncpy(&cmd.parm.cmsg.para[6], id, l);
881 cmd.command = CAPI_PUT_MESSAGE;
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);
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
894 static void
895 isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
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;
906 l = min(strlen(msg), sizeof(cmd.parm) - sizeof(cmd.parm.cmsg)
907 + sizeof(cmd.parm.cmsg.para) - 2);
909 if (!l) {
910 isdn_tty_modem_result(RESULT_ERROR, info);
911 return;
913 for (j = 7; j >= 0; j--)
914 if (m->mdmreg[REG_SI1] & (1 << j)) {
915 si = bit2si[j];
916 break;
918 usg = isdn_calc_usage(si, l2);
919 #ifdef CONFIG_ISDN_AUDIO
920 if ((si == 1) &&
921 (l2 != ISDN_PROTO_L2_MODEM)
922 #ifdef CONFIG_ISDN_TTY_FAX
923 && (l2 != ISDN_PROTO_L2_FAX)
924 #endif
926 l2 = ISDN_PROTO_L2_TRANS;
927 usg = ISDN_USAGE_VOICE;
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;
964 cmd.parm.cmsg.Length = l + 14;
965 cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
966 cmd.parm.cmsg.Subcommand = CAPI_REQ;
967 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
968 cmd.parm.cmsg.para[0] = l + 1;
969 strncpy(&cmd.parm.cmsg.para[1], msg, l);
970 cmd.parm.cmsg.para[l + 1] = 0xd;
971 cmd.command = CAPI_PUT_MESSAGE;
972 /* info->dialing = 1;
973 strcpy(dev->num[i], n);
974 isdn_info_update();
976 isdn_command(&cmd);
980 static inline int
981 isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
983 #ifdef MODEM_PARANOIA_CHECK
984 if (!info) {
985 printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
986 name, routine);
987 return 1;
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;
994 #endif
995 return 0;
999 * This routine is called to set the UART divisor registers to match
1000 * the specified baud rate for a serial port.
1002 static void
1003 isdn_tty_change_speed(modem_info *info)
1005 struct tty_port *port = &info->port;
1006 uint cflag,
1007 cval,
1008 quot;
1009 int i;
1011 if (!port->tty)
1012 return;
1013 cflag = port->tty->termios.c_cflag;
1015 quot = i = cflag & CBAUD;
1016 if (i & CBAUDEX) {
1017 i &= ~CBAUDEX;
1018 if (i < 1 || i > 2)
1019 port->tty->termios.c_cflag &= ~CBAUDEX;
1020 else
1021 i += 15;
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);
1037 return;
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;
1047 tty_port_set_check_carrier(port, ~cflag & CLOCAL);
1050 static int
1051 isdn_tty_startup(modem_info *info)
1053 if (tty_port_initialized(&info->port))
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
1060 * Now, initialize the UART
1062 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
1063 if (info->port.tty)
1064 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
1066 * and set the speed of the serial port
1068 isdn_tty_change_speed(info);
1070 tty_port_set_initialized(&info->port, 1);
1071 info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
1072 info->send_outstanding = 0;
1073 return 0;
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.
1080 static void
1081 isdn_tty_shutdown(modem_info *info)
1083 if (!tty_port_initialized(&info->port))
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;
1090 if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
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);
1100 if (info->port.tty)
1101 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1103 tty_port_set_initialized(&info->port, 0);
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.
1115 static int
1116 isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
1118 int c;
1119 int total = 0;
1120 modem_info *info = (modem_info *) tty->driver_data;
1121 atemu *m = &info->emu;
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
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));
1146 memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
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.
1155 tty_wakeup(tty);
1156 info->msr |= UART_MSR_CTS;
1157 info->lsr |= UART_LSR_TEMT;
1159 info->xmit_count += cc;
1161 if ((info->vonline & 3) == 1) {
1162 /* Do NOT handle Ctrl-Q or Ctrl-S
1163 * when in full-duplex audio mode.
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);
1175 } else
1176 if (TTY_IS_FCLASS1(info)) {
1177 int cc = isdn_tty_handleDLEdown(info, m, c);
1179 if (info->vonline & 4) { /* ETX seen */
1180 isdn_ctrl c;
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);
1189 info->vonline = 0;
1190 #ifdef ISDN_DEBUG_MODEM_VOICE
1191 printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
1192 #endif
1193 info->xmit_count += cc;
1194 } else
1195 #endif
1196 info->xmit_count += c;
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);
1210 buf += c;
1211 count -= c;
1212 total += c;
1214 atomic_dec(&info->xmit_lock);
1215 if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
1216 if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
1217 isdn_tty_senddown(info);
1218 isdn_tty_tint(info);
1220 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1222 return total;
1225 static int
1226 isdn_tty_write_room(struct tty_struct *tty)
1228 modem_info *info = (modem_info *) tty->driver_data;
1229 int ret;
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;
1239 static int
1240 isdn_tty_chars_in_buffer(struct tty_struct *tty)
1242 modem_info *info = (modem_info *) tty->driver_data;
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);
1251 static void
1252 isdn_tty_flush_buffer(struct tty_struct *tty)
1254 modem_info *info;
1256 if (!tty) {
1257 return;
1259 info = (modem_info *) tty->driver_data;
1260 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
1261 return;
1263 isdn_tty_cleanup_xmit(info);
1264 info->xmit_count = 0;
1265 tty_wakeup(tty);
1268 static void
1269 isdn_tty_flush_chars(struct tty_struct *tty)
1271 modem_info *info = (modem_info *) tty->driver_data;
1273 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
1274 return;
1275 if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
1276 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1280 * ------------------------------------------------------------
1281 * isdn_tty_throttle()
1283 * This routine is called by the upper-layer tty layer to signal that
1284 * incoming characters should be throttled.
1285 * ------------------------------------------------------------
1287 static void
1288 isdn_tty_throttle(struct tty_struct *tty)
1290 modem_info *info = (modem_info *) tty->driver_data;
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;
1299 static void
1300 isdn_tty_unthrottle(struct tty_struct *tty)
1302 modem_info *info = (modem_info *) tty->driver_data;
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);
1312 info->mcr |= UART_MCR_RTS;
1316 * ------------------------------------------------------------
1317 * isdn_tty_ioctl() and friends
1318 * ------------------------------------------------------------
1322 * isdn_tty_get_lsr_info - get line status register info
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.
1331 static int
1332 isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
1334 u_char status;
1335 uint result;
1337 status = info->lsr;
1338 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1339 return put_user(result, value);
1343 static int
1344 isdn_tty_tiocmget(struct tty_struct *tty)
1346 modem_info *info = (modem_info *) tty->driver_data;
1347 u_char control, status;
1349 if (isdn_tty_paranoia_check(info, tty->name, __func__))
1350 return -ENODEV;
1351 if (tty_io_error(tty))
1352 return -EIO;
1354 mutex_lock(&modem_info_mutex);
1355 #ifdef ISDN_DEBUG_MODEM_IOCTL
1356 printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
1357 #endif
1359 control = info->mcr;
1360 status = info->msr;
1361 mutex_unlock(&modem_info_mutex);
1362 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
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);
1370 static int
1371 isdn_tty_tiocmset(struct tty_struct *tty,
1372 unsigned int set, unsigned int clear)
1374 modem_info *info = (modem_info *) tty->driver_data;
1376 if (isdn_tty_paranoia_check(info, tty->name, __func__))
1377 return -ENODEV;
1378 if (tty_io_error(tty))
1379 return -EIO;
1381 #ifdef ISDN_DEBUG_MODEM_IOCTL
1382 printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
1383 #endif
1385 mutex_lock(&modem_info_mutex);
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);
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);
1407 mutex_unlock(&modem_info_mutex);
1408 return 0;
1411 static int
1412 isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
1414 modem_info *info = (modem_info *) tty->driver_data;
1415 int retval;
1417 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
1418 return -ENODEV;
1419 if (tty_io_error(tty))
1420 return -EIO;
1421 switch (cmd) {
1422 case TCSBRK: /* SVID version: non-zero arg --> no break */
1423 #ifdef ISDN_DEBUG_MODEM_IOCTL
1424 printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
1425 #endif
1426 retval = tty_check_change(tty);
1427 if (retval)
1428 return retval;
1429 tty_wait_until_sent(tty, 0);
1430 return 0;
1431 case TCSBRKP: /* support for POSIX tcsendbreak() */
1432 #ifdef ISDN_DEBUG_MODEM_IOCTL
1433 printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
1434 #endif
1435 retval = tty_check_change(tty);
1436 if (retval)
1437 return retval;
1438 tty_wait_until_sent(tty, 0);
1439 return 0;
1440 case TIOCSERGETLSR: /* Get line status register */
1441 #ifdef ISDN_DEBUG_MODEM_IOCTL
1442 printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
1443 #endif
1444 return isdn_tty_get_lsr_info(info, (uint __user *) arg);
1445 default:
1446 #ifdef ISDN_DEBUG_MODEM_IOCTL
1447 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
1448 #endif
1449 return -ENOIOCTLCMD;
1451 return 0;
1454 static void
1455 isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1457 modem_info *info = (modem_info *) tty->driver_data;
1459 if (!old_termios)
1460 isdn_tty_change_speed(info);
1461 else {
1462 if (tty->termios.c_cflag == old_termios->c_cflag &&
1463 tty->termios.c_ispeed == old_termios->c_ispeed &&
1464 tty->termios.c_ospeed == old_termios->c_ospeed)
1465 return;
1466 isdn_tty_change_speed(info);
1471 * ------------------------------------------------------------
1472 * isdn_tty_open() and friends
1473 * ------------------------------------------------------------
1476 static int isdn_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1478 modem_info *info = &dev->mdm.info[tty->index];
1480 if (isdn_tty_paranoia_check(info, tty->name, __func__))
1481 return -ENODEV;
1483 tty->driver_data = info;
1485 return tty_port_install(&info->port, driver, tty);
1489 * This routine is called whenever a serial port is opened. It
1490 * enables interrupts for a serial port, linking in its async structure into
1491 * the IRQ chain. It also performs the serial-specific
1492 * initialization for the tty structure.
1494 static int
1495 isdn_tty_open(struct tty_struct *tty, struct file *filp)
1497 modem_info *info = tty->driver_data;
1498 struct tty_port *port = &info->port;
1499 int retval;
1501 #ifdef ISDN_DEBUG_MODEM_OPEN
1502 printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
1503 port->count);
1504 #endif
1505 port->count++;
1506 port->tty = tty;
1508 * Start up serial port
1510 retval = isdn_tty_startup(info);
1511 if (retval) {
1512 #ifdef ISDN_DEBUG_MODEM_OPEN
1513 printk(KERN_DEBUG "isdn_tty_open return after startup\n");
1514 #endif
1515 return retval;
1517 retval = tty_port_block_til_ready(port, tty, filp);
1518 if (retval) {
1519 #ifdef ISDN_DEBUG_MODEM_OPEN
1520 printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
1521 #endif
1522 return retval;
1524 #ifdef ISDN_DEBUG_MODEM_OPEN
1525 printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
1526 #endif
1527 dev->modempoll++;
1528 #ifdef ISDN_DEBUG_MODEM_OPEN
1529 printk(KERN_DEBUG "isdn_tty_open normal exit\n");
1530 #endif
1531 return 0;
1534 static void
1535 isdn_tty_close(struct tty_struct *tty, struct file *filp)
1537 modem_info *info = (modem_info *) tty->driver_data;
1538 struct tty_port *port = &info->port;
1539 ulong timeout;
1541 if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
1542 return;
1543 if (tty_hung_up_p(filp)) {
1544 #ifdef ISDN_DEBUG_MODEM_OPEN
1545 printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
1546 #endif
1547 return;
1549 if ((tty->count == 1) && (port->count != 1)) {
1551 * Uh, oh. tty->count is 1, which means that the tty
1552 * structure will be freed. Info->count should always
1553 * be one in these conditions. If it's greater than
1554 * one, we've got real problems, since it means the
1555 * serial port won't be shutdown.
1557 printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
1558 "info->count is %d\n", port->count);
1559 port->count = 1;
1561 if (--port->count < 0) {
1562 printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
1563 info->line, port->count);
1564 port->count = 0;
1566 if (port->count) {
1567 #ifdef ISDN_DEBUG_MODEM_OPEN
1568 printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
1569 #endif
1570 return;
1572 info->closing = 1;
1574 tty->closing = 1;
1576 * At this point we stop accepting input. To do this, we
1577 * disable the receive line status interrupts, and tell the
1578 * interrupt driver to stop checking the data ready bit in the
1579 * line status register.
1581 if (tty_port_initialized(port)) {
1582 tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
1584 * Before we drop DTR, make sure the UART transmitter
1585 * has completely drained; this is especially
1586 * important if there is a transmit FIFO!
1588 timeout = jiffies + HZ;
1589 while (!(info->lsr & UART_LSR_TEMT)) {
1590 schedule_timeout_interruptible(20);
1591 if (time_after(jiffies, timeout))
1592 break;
1595 dev->modempoll--;
1596 isdn_tty_shutdown(info);
1597 isdn_tty_flush_buffer(tty);
1598 tty_ldisc_flush(tty);
1599 port->tty = NULL;
1600 info->ncarrier = 0;
1602 tty_port_close_end(port, tty);
1603 info->closing = 0;
1604 #ifdef ISDN_DEBUG_MODEM_OPEN
1605 printk(KERN_DEBUG "isdn_tty_close normal exit\n");
1606 #endif
1610 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1612 static void
1613 isdn_tty_hangup(struct tty_struct *tty)
1615 modem_info *info = (modem_info *) tty->driver_data;
1616 struct tty_port *port = &info->port;
1618 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
1619 return;
1620 isdn_tty_shutdown(info);
1621 port->count = 0;
1622 tty_port_set_active(port, 0);
1623 port->tty = NULL;
1624 wake_up_interruptible(&port->open_wait);
1627 /* This routine initializes all emulator-data.
1629 static void
1630 isdn_tty_reset_profile(atemu *m)
1632 m->profile[0] = 0;
1633 m->profile[1] = 0;
1634 m->profile[2] = 43;
1635 m->profile[3] = 13;
1636 m->profile[4] = 10;
1637 m->profile[5] = 8;
1638 m->profile[6] = 3;
1639 m->profile[7] = 60;
1640 m->profile[8] = 2;
1641 m->profile[9] = 6;
1642 m->profile[10] = 7;
1643 m->profile[11] = 70;
1644 m->profile[12] = 0x45;
1645 m->profile[13] = 4;
1646 m->profile[14] = ISDN_PROTO_L2_X75I;
1647 m->profile[15] = ISDN_PROTO_L3_TRANS;
1648 m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
1649 m->profile[17] = ISDN_MODEM_WINSIZE;
1650 m->profile[18] = 4;
1651 m->profile[19] = 0;
1652 m->profile[20] = 0;
1653 m->profile[23] = 0;
1654 m->pmsn[0] = '\0';
1655 m->plmsn[0] = '\0';
1658 #ifdef CONFIG_ISDN_AUDIO
1659 static void
1660 isdn_tty_modem_reset_vpar(atemu *m)
1662 m->vpar[0] = 2; /* Voice-device (2 = phone line) */
1663 m->vpar[1] = 0; /* Silence detection level (0 = none ) */
1664 m->vpar[2] = 70; /* Silence interval (7 sec. ) */
1665 m->vpar[3] = 2; /* Compression type (1 = ADPCM-2 ) */
1666 m->vpar[4] = 0; /* DTMF detection level (0 = softcode ) */
1667 m->vpar[5] = 8; /* DTMF interval (8 * 5 ms. ) */
1669 #endif
1671 #ifdef CONFIG_ISDN_TTY_FAX
1672 static void
1673 isdn_tty_modem_reset_faxpar(modem_info *info)
1675 T30_s *f = info->fax;
1677 f->code = 0;
1678 f->phase = ISDN_FAX_PHASE_IDLE;
1679 f->direction = 0;
1680 f->resolution = 1; /* fine */
1681 f->rate = 5; /* 14400 bit/s */
1682 f->width = 0;
1683 f->length = 0;
1684 f->compression = 0;
1685 f->ecm = 0;
1686 f->binary = 0;
1687 f->scantime = 0;
1688 memset(&f->id[0], 32, FAXIDLEN - 1);
1689 f->id[FAXIDLEN - 1] = 0;
1690 f->badlin = 0;
1691 f->badmul = 0;
1692 f->bor = 0;
1693 f->nbc = 0;
1694 f->cq = 0;
1695 f->cr = 0;
1696 f->ctcrty = 0;
1697 f->minsp = 0;
1698 f->phcto = 30;
1699 f->rel = 0;
1700 memset(&f->pollid[0], 32, FAXIDLEN - 1);
1701 f->pollid[FAXIDLEN - 1] = 0;
1703 #endif
1705 static void
1706 isdn_tty_modem_reset_regs(modem_info *info, int force)
1708 atemu *m = &info->emu;
1709 if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
1710 memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
1711 memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
1712 memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
1713 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
1715 #ifdef CONFIG_ISDN_AUDIO
1716 isdn_tty_modem_reset_vpar(m);
1717 #endif
1718 #ifdef CONFIG_ISDN_TTY_FAX
1719 isdn_tty_modem_reset_faxpar(info);
1720 #endif
1721 m->mdmcmdl = 0;
1724 static void
1725 modem_write_profile(atemu *m)
1727 memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
1728 memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
1729 memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
1730 if (dev->profd)
1731 send_sig(SIGIO, dev->profd, 1);
1734 static const struct tty_operations modem_ops = {
1735 .install = isdn_tty_install,
1736 .open = isdn_tty_open,
1737 .close = isdn_tty_close,
1738 .write = isdn_tty_write,
1739 .flush_chars = isdn_tty_flush_chars,
1740 .write_room = isdn_tty_write_room,
1741 .chars_in_buffer = isdn_tty_chars_in_buffer,
1742 .flush_buffer = isdn_tty_flush_buffer,
1743 .ioctl = isdn_tty_ioctl,
1744 .throttle = isdn_tty_throttle,
1745 .unthrottle = isdn_tty_unthrottle,
1746 .set_termios = isdn_tty_set_termios,
1747 .hangup = isdn_tty_hangup,
1748 .tiocmget = isdn_tty_tiocmget,
1749 .tiocmset = isdn_tty_tiocmset,
1752 static int isdn_tty_carrier_raised(struct tty_port *port)
1754 modem_info *info = container_of(port, modem_info, port);
1755 return info->msr & UART_MSR_DCD;
1758 static const struct tty_port_operations isdn_tty_port_ops = {
1759 .carrier_raised = isdn_tty_carrier_raised,
1763 isdn_tty_modem_init(void)
1765 isdn_modem_t *m;
1766 int i, retval;
1767 modem_info *info;
1769 m = &dev->mdm;
1770 m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
1771 if (!m->tty_modem)
1772 return -ENOMEM;
1773 m->tty_modem->name = "ttyI";
1774 m->tty_modem->major = ISDN_TTY_MAJOR;
1775 m->tty_modem->minor_start = 0;
1776 m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
1777 m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
1778 m->tty_modem->init_termios = tty_std_termios;
1779 m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1780 m->tty_modem->flags = TTY_DRIVER_REAL_RAW;
1781 m->tty_modem->driver_name = "isdn_tty";
1782 tty_set_operations(m->tty_modem, &modem_ops);
1783 retval = tty_register_driver(m->tty_modem);
1784 if (retval) {
1785 printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
1786 goto err;
1788 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1789 info = &m->info[i];
1790 #ifdef CONFIG_ISDN_TTY_FAX
1791 if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
1792 printk(KERN_ERR "Could not allocate fax t30-buffer\n");
1793 retval = -ENOMEM;
1794 goto err_unregister;
1796 #endif
1797 tty_port_init(&info->port);
1798 info->port.ops = &isdn_tty_port_ops;
1799 spin_lock_init(&info->readlock);
1800 sprintf(info->last_cause, "0000");
1801 sprintf(info->last_num, "none");
1802 info->last_dir = 0;
1803 info->last_lhup = 1;
1804 info->last_l2 = -1;
1805 info->last_si = 0;
1806 isdn_tty_reset_profile(&info->emu);
1807 isdn_tty_modem_reset_regs(info, 1);
1808 info->magic = ISDN_ASYNC_MAGIC;
1809 info->line = i;
1810 info->x_char = 0;
1811 info->isdn_driver = -1;
1812 info->isdn_channel = -1;
1813 info->drv_index = -1;
1814 info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
1815 init_timer(&info->nc_timer);
1816 info->nc_timer.function = isdn_tty_modem_do_ncarrier;
1817 info->nc_timer.data = (unsigned long) info;
1818 skb_queue_head_init(&info->xmit_queue);
1819 #ifdef CONFIG_ISDN_AUDIO
1820 skb_queue_head_init(&info->dtmf_queue);
1821 #endif
1822 info->port.xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5,
1823 GFP_KERNEL);
1824 if (!info->port.xmit_buf) {
1825 printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
1826 retval = -ENOMEM;
1827 goto err_unregister;
1829 /* Make room for T.70 header */
1830 info->port.xmit_buf += 4;
1832 return 0;
1833 err_unregister:
1834 for (i--; i >= 0; i--) {
1835 info = &m->info[i];
1836 #ifdef CONFIG_ISDN_TTY_FAX
1837 kfree(info->fax);
1838 #endif
1839 kfree(info->port.xmit_buf - 4);
1840 info->port.xmit_buf = NULL;
1841 tty_port_destroy(&info->port);
1843 tty_unregister_driver(m->tty_modem);
1844 err:
1845 put_tty_driver(m->tty_modem);
1846 m->tty_modem = NULL;
1847 return retval;
1850 void
1851 isdn_tty_exit(void)
1853 modem_info *info;
1854 int i;
1856 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1857 info = &dev->mdm.info[i];
1858 isdn_tty_cleanup_xmit(info);
1859 #ifdef CONFIG_ISDN_TTY_FAX
1860 kfree(info->fax);
1861 #endif
1862 kfree(info->port.xmit_buf - 4);
1863 info->port.xmit_buf = NULL;
1864 tty_port_destroy(&info->port);
1866 tty_unregister_driver(dev->mdm.tty_modem);
1867 put_tty_driver(dev->mdm.tty_modem);
1868 dev->mdm.tty_modem = NULL;
1873 * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
1874 * match the MSN against the MSNs (glob patterns) defined for tty_emulator,
1875 * and return 0 for match, 1 for no match, 2 if MSN could match if longer.
1878 static int
1879 isdn_tty_match_icall(char *cid, atemu *emu, int di)
1881 #ifdef ISDN_DEBUG_MODEM_ICALL
1882 printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
1883 emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
1884 emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
1885 #endif
1886 if (strlen(emu->lmsn)) {
1887 char *p = emu->lmsn;
1888 char *q;
1889 int tmp;
1890 int ret = 0;
1892 while (1) {
1893 if ((q = strchr(p, ';')))
1894 *q = '\0';
1895 if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
1896 ret = tmp;
1897 #ifdef ISDN_DEBUG_MODEM_ICALL
1898 printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
1899 p, isdn_map_eaz2msn(emu->msn, di), tmp);
1900 #endif
1901 if (q) {
1902 *q = ';';
1903 p = q;
1904 p++;
1906 if (!tmp)
1907 return 0;
1908 if (!q)
1909 break;
1911 return ret;
1912 } else {
1913 int tmp;
1914 tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
1915 #ifdef ISDN_DEBUG_MODEM_ICALL
1916 printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
1917 isdn_map_eaz2msn(emu->msn, di), tmp);
1918 #endif
1919 return tmp;
1924 * An incoming call-request has arrived.
1925 * Search the tty-devices for an appropriate device and bind
1926 * it to the ISDN-Channel.
1927 * Return:
1929 * 0 = No matching device found.
1930 * 1 = A matching device found.
1931 * 3 = No match found, but eventually would match, if
1932 * CID is longer.
1935 isdn_tty_find_icall(int di, int ch, setup_parm *setup)
1937 char *eaz;
1938 int i;
1939 int wret;
1940 int idx;
1941 int si1;
1942 int si2;
1943 char *nr;
1944 ulong flags;
1946 if (!setup->phone[0]) {
1947 nr = "0";
1948 printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
1949 } else
1950 nr = setup->phone;
1951 si1 = (int) setup->si1;
1952 si2 = (int) setup->si2;
1953 if (!setup->eazmsn[0]) {
1954 printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
1955 eaz = "0";
1956 } else
1957 eaz = setup->eazmsn;
1958 #ifdef ISDN_DEBUG_MODEM_ICALL
1959 printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
1960 #endif
1961 wret = 0;
1962 spin_lock_irqsave(&dev->lock, flags);
1963 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1964 modem_info *info = &dev->mdm.info[i];
1966 if (info->port.count == 0)
1967 continue;
1968 if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) && /* SI1 is matching */
1969 (info->emu.mdmreg[REG_SI2] == si2)) { /* SI2 is matching */
1970 idx = isdn_dc2minor(di, ch);
1971 #ifdef ISDN_DEBUG_MODEM_ICALL
1972 printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
1973 printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
1974 info->port.flags, info->isdn_driver,
1975 info->isdn_channel, dev->usage[idx]);
1976 #endif
1977 if (
1978 #ifndef FIX_FILE_TRANSFER
1979 tty_port_active(&info->port) &&
1980 #endif
1981 (info->isdn_driver == -1) &&
1982 (info->isdn_channel == -1) &&
1983 (USG_NONE(dev->usage[idx]))) {
1984 int matchret;
1986 if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
1987 wret = matchret;
1988 if (!matchret) { /* EAZ is matching */
1989 info->isdn_driver = di;
1990 info->isdn_channel = ch;
1991 info->drv_index = idx;
1992 dev->m_idx[idx] = info->line;
1993 dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
1994 dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
1995 strcpy(dev->num[idx], nr);
1996 strcpy(info->emu.cpn, eaz);
1997 info->emu.mdmreg[REG_SI1I] = si2bit[si1];
1998 info->emu.mdmreg[REG_PLAN] = setup->plan;
1999 info->emu.mdmreg[REG_SCREEN] = setup->screen;
2000 isdn_info_update();
2001 spin_unlock_irqrestore(&dev->lock, flags);
2002 printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2003 info->line);
2004 info->msr |= UART_MSR_RI;
2005 isdn_tty_modem_result(RESULT_RING, info);
2006 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2007 return 1;
2012 spin_unlock_irqrestore(&dev->lock, flags);
2013 printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
2014 ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
2015 return (wret == 2) ? 3 : 0;
2019 isdn_tty_stat_callback(int i, isdn_ctrl *c)
2021 int mi;
2022 modem_info *info;
2023 char *e;
2025 if (i < 0)
2026 return 0;
2027 if ((mi = dev->m_idx[i]) >= 0) {
2028 info = &dev->mdm.info[mi];
2029 switch (c->command) {
2030 case ISDN_STAT_CINF:
2031 printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2032 info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2033 if (e == (char *)c->parm.num)
2034 info->emu.charge = 0;
2036 break;
2037 case ISDN_STAT_BSENT:
2038 #ifdef ISDN_TTY_STAT_DEBUG
2039 printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
2040 #endif
2041 if ((info->isdn_driver == c->driver) &&
2042 (info->isdn_channel == c->arg)) {
2043 info->msr |= UART_MSR_CTS;
2044 if (info->send_outstanding)
2045 if (!(--info->send_outstanding))
2046 info->lsr |= UART_LSR_TEMT;
2047 isdn_tty_tint(info);
2048 return 1;
2050 break;
2051 case ISDN_STAT_CAUSE:
2052 #ifdef ISDN_TTY_STAT_DEBUG
2053 printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2054 #endif
2055 /* Signal cause to tty-device */
2056 strncpy(info->last_cause, c->parm.num, 5);
2057 return 1;
2058 case ISDN_STAT_DISPLAY:
2059 #ifdef ISDN_TTY_STAT_DEBUG
2060 printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2061 #endif
2062 /* Signal display to tty-device */
2063 if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2064 !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2065 isdn_tty_at_cout("\r\n", info);
2066 isdn_tty_at_cout("DISPLAY: ", info);
2067 isdn_tty_at_cout(c->parm.display, info);
2068 isdn_tty_at_cout("\r\n", info);
2070 return 1;
2071 case ISDN_STAT_DCONN:
2072 #ifdef ISDN_TTY_STAT_DEBUG
2073 printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2074 #endif
2075 if (tty_port_active(&info->port)) {
2076 if (info->dialing == 1) {
2077 info->dialing = 2;
2078 return 1;
2081 break;
2082 case ISDN_STAT_DHUP:
2083 #ifdef ISDN_TTY_STAT_DEBUG
2084 printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2085 #endif
2086 if (tty_port_active(&info->port)) {
2087 if (info->dialing == 1)
2088 isdn_tty_modem_result(RESULT_BUSY, info);
2089 if (info->dialing > 1)
2090 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2091 info->dialing = 0;
2092 #ifdef ISDN_DEBUG_MODEM_HUP
2093 printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2094 #endif
2095 isdn_tty_modem_hup(info, 0);
2096 return 1;
2098 break;
2099 case ISDN_STAT_BCONN:
2100 #ifdef ISDN_TTY_STAT_DEBUG
2101 printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2102 #endif
2103 /* Wake up any processes waiting
2104 * for incoming call of this device when
2105 * DCD follow the state of incoming carrier
2107 if (info->port.blocked_open &&
2108 (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
2109 wake_up_interruptible(&info->port.open_wait);
2112 /* Schedule CONNECT-Message to any tty
2113 * waiting for it and
2114 * set DCD-bit of its modem-status.
2116 if (tty_port_active(&info->port) ||
2117 (info->port.blocked_open &&
2118 (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
2119 info->msr |= UART_MSR_DCD;
2120 info->emu.charge = 0;
2121 if (info->dialing & 0xf)
2122 info->last_dir = 1;
2123 else
2124 info->last_dir = 0;
2125 info->dialing = 0;
2126 info->rcvsched = 1;
2127 if (USG_MODEM(dev->usage[i])) {
2128 if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2129 strcpy(info->emu.connmsg, c->parm.num);
2130 isdn_tty_modem_result(RESULT_CONNECT, info);
2131 } else
2132 isdn_tty_modem_result(RESULT_CONNECT64000, info);
2134 if (USG_VOICE(dev->usage[i]))
2135 isdn_tty_modem_result(RESULT_VCON, info);
2136 return 1;
2138 break;
2139 case ISDN_STAT_BHUP:
2140 #ifdef ISDN_TTY_STAT_DEBUG
2141 printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2142 #endif
2143 if (tty_port_active(&info->port)) {
2144 #ifdef ISDN_DEBUG_MODEM_HUP
2145 printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2146 #endif
2147 isdn_tty_modem_hup(info, 0);
2148 return 1;
2150 break;
2151 case ISDN_STAT_NODCH:
2152 #ifdef ISDN_TTY_STAT_DEBUG
2153 printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2154 #endif
2155 if (tty_port_active(&info->port)) {
2156 if (info->dialing) {
2157 info->dialing = 0;
2158 info->last_l2 = -1;
2159 info->last_si = 0;
2160 sprintf(info->last_cause, "0000");
2161 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2163 isdn_tty_modem_hup(info, 0);
2164 return 1;
2166 break;
2167 case ISDN_STAT_UNLOAD:
2168 #ifdef ISDN_TTY_STAT_DEBUG
2169 printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2170 #endif
2171 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2172 info = &dev->mdm.info[i];
2173 if (info->isdn_driver == c->driver) {
2174 if (info->online)
2175 isdn_tty_modem_hup(info, 1);
2178 return 1;
2179 #ifdef CONFIG_ISDN_TTY_FAX
2180 case ISDN_STAT_FAXIND:
2181 if (tty_port_active(&info->port)) {
2182 isdn_tty_fax_command(info, c);
2184 break;
2185 #endif
2186 #ifdef CONFIG_ISDN_AUDIO
2187 case ISDN_STAT_AUDIO:
2188 if (tty_port_active(&info->port)) {
2189 switch (c->parm.num[0]) {
2190 case ISDN_AUDIO_DTMF:
2191 if (info->vonline) {
2192 isdn_audio_put_dle_code(info,
2193 c->parm.num[1]);
2195 break;
2198 break;
2199 #endif
2202 return 0;
2205 /*********************************************************************
2206 Modem-Emulator-Routines
2207 *********************************************************************/
2209 #define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
2212 * Put a message from the AT-emulator into receive-buffer of tty,
2213 * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2215 void
2216 isdn_tty_at_cout(char *msg, modem_info *info)
2218 struct tty_port *port = &info->port;
2219 atemu *m = &info->emu;
2220 char *p;
2221 char c;
2222 u_long flags;
2223 struct sk_buff *skb = NULL;
2224 char *sp = NULL;
2225 int l;
2227 if (!msg) {
2228 printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2229 return;
2232 l = strlen(msg);
2234 spin_lock_irqsave(&info->readlock, flags);
2235 if (info->closing) {
2236 spin_unlock_irqrestore(&info->readlock, flags);
2237 return;
2240 /* use queue instead of direct, if online and */
2241 /* data is in queue or buffer is full */
2242 if (info->online && ((tty_buffer_request_room(port, l) < l) ||
2243 !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
2244 skb = alloc_skb(l, GFP_ATOMIC);
2245 if (!skb) {
2246 spin_unlock_irqrestore(&info->readlock, flags);
2247 return;
2249 sp = skb_put(skb, l);
2250 #ifdef CONFIG_ISDN_AUDIO
2251 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2252 ISDN_AUDIO_SKB_LOCK(skb) = 0;
2253 #endif
2256 for (p = msg; *p; p++) {
2257 switch (*p) {
2258 case '\r':
2259 c = m->mdmreg[REG_CR];
2260 break;
2261 case '\n':
2262 c = m->mdmreg[REG_LF];
2263 break;
2264 case '\b':
2265 c = m->mdmreg[REG_BS];
2266 break;
2267 default:
2268 c = *p;
2270 if (skb) {
2271 *sp++ = c;
2272 } else {
2273 if (tty_insert_flip_char(port, c, TTY_NORMAL) == 0)
2274 break;
2277 if (skb) {
2278 __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2279 dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2280 spin_unlock_irqrestore(&info->readlock, flags);
2281 /* Schedule dequeuing */
2282 if (dev->modempoll && info->rcvsched)
2283 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2285 } else {
2286 spin_unlock_irqrestore(&info->readlock, flags);
2287 tty_flip_buffer_push(port);
2292 * Perform ATH Hangup
2294 static void
2295 isdn_tty_on_hook(modem_info *info)
2297 if (info->isdn_channel >= 0) {
2298 #ifdef ISDN_DEBUG_MODEM_HUP
2299 printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2300 #endif
2301 isdn_tty_modem_hup(info, 1);
2305 static void
2306 isdn_tty_off_hook(void)
2308 printk(KERN_DEBUG "isdn_tty_off_hook\n");
2311 #define PLUSWAIT1 (HZ / 2) /* 0.5 sec. */
2312 #define PLUSWAIT2 (HZ * 3 / 2) /* 1.5 sec */
2315 * Check Buffer for Modem-escape-sequence, activate timer-callback to
2316 * isdn_tty_modem_escape() if sequence found.
2318 * Parameters:
2319 * p pointer to databuffer
2320 * plus escape-character
2321 * count length of buffer
2322 * pluscount count of valid escape-characters so far
2323 * lastplus timestamp of last character
2325 static void
2326 isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
2327 u_long *lastplus)
2329 if (plus > 127)
2330 return;
2331 if (count > 3) {
2332 p += count - 3;
2333 count = 3;
2334 *pluscount = 0;
2336 while (count > 0) {
2337 if (*(p++) == plus) {
2338 if ((*pluscount)++) {
2339 /* Time since last '+' > 0.5 sec. ? */
2340 if (time_after(jiffies, *lastplus + PLUSWAIT1))
2341 *pluscount = 1;
2342 } else {
2343 /* Time since last non-'+' < 1.5 sec. ? */
2344 if (time_before(jiffies, *lastplus + PLUSWAIT2))
2345 *pluscount = 0;
2347 if ((*pluscount == 3) && (count == 1))
2348 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2349 if (*pluscount > 3)
2350 *pluscount = 1;
2351 } else
2352 *pluscount = 0;
2353 *lastplus = jiffies;
2354 count--;
2359 * Return result of AT-emulator to tty-receive-buffer, depending on
2360 * modem-register 12, bit 0 and 1.
2361 * For CONNECT-messages also switch to online-mode.
2362 * For RING-message handle auto-ATA if register 0 != 0
2365 static void
2366 isdn_tty_modem_result(int code, modem_info *info)
2368 atemu *m = &info->emu;
2369 static char *msg[] =
2370 {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2371 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2372 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2373 char s[ISDN_MSNLEN + 10];
2375 switch (code) {
2376 case RESULT_RING:
2377 m->mdmreg[REG_RINGCNT]++;
2378 if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2379 /* Automatically accept incoming call */
2380 isdn_tty_cmd_ATA(info);
2381 break;
2382 case RESULT_NO_CARRIER:
2383 #ifdef ISDN_DEBUG_MODEM_HUP
2384 printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
2385 info->closing, !info->port.tty);
2386 #endif
2387 m->mdmreg[REG_RINGCNT] = 0;
2388 del_timer(&info->nc_timer);
2389 info->ncarrier = 0;
2390 if (info->closing || !info->port.tty)
2391 return;
2393 #ifdef CONFIG_ISDN_AUDIO
2394 if (info->vonline & 1) {
2395 #ifdef ISDN_DEBUG_MODEM_VOICE
2396 printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
2397 info->line);
2398 #endif
2399 /* voice-recording, add DLE-ETX */
2400 isdn_tty_at_cout("\020\003", info);
2402 if (info->vonline & 2) {
2403 #ifdef ISDN_DEBUG_MODEM_VOICE
2404 printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
2405 info->line);
2406 #endif
2407 /* voice-playing, add DLE-DC4 */
2408 isdn_tty_at_cout("\020\024", info);
2410 #endif
2411 break;
2412 case RESULT_CONNECT:
2413 case RESULT_CONNECT64000:
2414 sprintf(info->last_cause, "0000");
2415 if (!info->online)
2416 info->online = 2;
2417 break;
2418 case RESULT_VCON:
2419 #ifdef ISDN_DEBUG_MODEM_VOICE
2420 printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
2421 info->line);
2422 #endif
2423 sprintf(info->last_cause, "0000");
2424 if (!info->online)
2425 info->online = 1;
2426 break;
2427 } /* switch (code) */
2429 if (m->mdmreg[REG_RESP] & BIT_RESP) {
2430 /* Show results */
2431 if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
2432 /* Show numeric results only */
2433 sprintf(s, "\r\n%d\r\n", code);
2434 isdn_tty_at_cout(s, info);
2435 } else {
2436 if (code == RESULT_RING) {
2437 /* return if "show RUNG" and ringcounter>1 */
2438 if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
2439 (m->mdmreg[REG_RINGCNT] > 1))
2440 return;
2441 /* print CID, _before_ _every_ ring */
2442 if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
2443 isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
2444 isdn_tty_at_cout(dev->num[info->drv_index], info);
2445 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2446 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2447 isdn_tty_at_cout(info->emu.cpn, info);
2451 isdn_tty_at_cout("\r\n", info);
2452 isdn_tty_at_cout(msg[code], info);
2453 switch (code) {
2454 case RESULT_CONNECT:
2455 switch (m->mdmreg[REG_L2PROT]) {
2456 case ISDN_PROTO_L2_MODEM:
2457 isdn_tty_at_cout(" ", info);
2458 isdn_tty_at_cout(m->connmsg, info);
2459 break;
2461 break;
2462 case RESULT_RING:
2463 /* Append CPN, if enabled */
2464 if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
2465 sprintf(s, "/%s", m->cpn);
2466 isdn_tty_at_cout(s, info);
2468 /* Print CID only once, _after_ 1st RING */
2469 if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
2470 (m->mdmreg[REG_RINGCNT] == 1)) {
2471 isdn_tty_at_cout("\r\n", info);
2472 isdn_tty_at_cout("CALLER NUMBER: ", info);
2473 isdn_tty_at_cout(dev->num[info->drv_index], info);
2474 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2475 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2476 isdn_tty_at_cout(info->emu.cpn, info);
2479 break;
2480 case RESULT_NO_CARRIER:
2481 case RESULT_NO_DIALTONE:
2482 case RESULT_BUSY:
2483 case RESULT_NO_ANSWER:
2484 m->mdmreg[REG_RINGCNT] = 0;
2485 /* Append Cause-Message if enabled */
2486 if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
2487 sprintf(s, "/%s", info->last_cause);
2488 isdn_tty_at_cout(s, info);
2490 break;
2491 case RESULT_CONNECT64000:
2492 /* Append Protocol to CONNECT message */
2493 switch (m->mdmreg[REG_L2PROT]) {
2494 case ISDN_PROTO_L2_X75I:
2495 case ISDN_PROTO_L2_X75UI:
2496 case ISDN_PROTO_L2_X75BUI:
2497 isdn_tty_at_cout("/X.75", info);
2498 break;
2499 case ISDN_PROTO_L2_HDLC:
2500 isdn_tty_at_cout("/HDLC", info);
2501 break;
2502 case ISDN_PROTO_L2_V11096:
2503 isdn_tty_at_cout("/V110/9600", info);
2504 break;
2505 case ISDN_PROTO_L2_V11019:
2506 isdn_tty_at_cout("/V110/19200", info);
2507 break;
2508 case ISDN_PROTO_L2_V11038:
2509 isdn_tty_at_cout("/V110/38400", info);
2510 break;
2512 if (m->mdmreg[REG_T70] & BIT_T70) {
2513 isdn_tty_at_cout("/T.70", info);
2514 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2515 isdn_tty_at_cout("+", info);
2517 break;
2519 isdn_tty_at_cout("\r\n", info);
2522 if (code == RESULT_NO_CARRIER) {
2523 if (info->closing || (!info->port.tty))
2524 return;
2526 if (tty_port_check_carrier(&info->port))
2527 tty_hangup(info->port.tty);
2533 * Display a modem-register-value.
2535 static void
2536 isdn_tty_show_profile(int ridx, modem_info *info)
2538 char v[6];
2540 sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
2541 isdn_tty_at_cout(v, info);
2545 * Get MSN-string from char-pointer, set pointer to end of number
2547 static void
2548 isdn_tty_get_msnstr(char *n, char **p)
2550 int limit = ISDN_MSNLEN - 1;
2552 while (((*p[0] >= '0' && *p[0] <= '9') ||
2553 /* Why a comma ??? */
2554 (*p[0] == ',') || (*p[0] == ':')) &&
2555 (limit--))
2556 *n++ = *p[0]++;
2557 *n = '\0';
2561 * Get phone-number from modem-commandbuffer
2563 static void
2564 isdn_tty_getdial(char *p, char *q, int cnt)
2566 int first = 1;
2567 int limit = ISDN_MSNLEN - 1; /* MUST match the size of interface var to avoid
2568 buffer overflow */
2570 while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
2571 if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
2572 ((*p == 'R') && first) ||
2573 (*p == '*') || (*p == '#')) {
2574 *q++ = *p;
2575 limit--;
2577 if (!limit)
2578 break;
2579 p++;
2580 first = 0;
2582 *q = 0;
2585 #define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
2586 #define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
2588 static void
2589 isdn_tty_report(modem_info *info)
2591 atemu *m = &info->emu;
2592 char s[80];
2594 isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
2595 sprintf(s, " Remote Number: %s\r\n", info->last_num);
2596 isdn_tty_at_cout(s, info);
2597 sprintf(s, " Direction: %s\r\n", info->last_dir ? "outgoing" : "incoming");
2598 isdn_tty_at_cout(s, info);
2599 isdn_tty_at_cout(" Layer-2 Protocol: ", info);
2600 switch (info->last_l2) {
2601 case ISDN_PROTO_L2_X75I:
2602 isdn_tty_at_cout("X.75i", info);
2603 break;
2604 case ISDN_PROTO_L2_X75UI:
2605 isdn_tty_at_cout("X.75ui", info);
2606 break;
2607 case ISDN_PROTO_L2_X75BUI:
2608 isdn_tty_at_cout("X.75bui", info);
2609 break;
2610 case ISDN_PROTO_L2_HDLC:
2611 isdn_tty_at_cout("HDLC", info);
2612 break;
2613 case ISDN_PROTO_L2_V11096:
2614 isdn_tty_at_cout("V.110 9600 Baud", info);
2615 break;
2616 case ISDN_PROTO_L2_V11019:
2617 isdn_tty_at_cout("V.110 19200 Baud", info);
2618 break;
2619 case ISDN_PROTO_L2_V11038:
2620 isdn_tty_at_cout("V.110 38400 Baud", info);
2621 break;
2622 case ISDN_PROTO_L2_TRANS:
2623 isdn_tty_at_cout("transparent", info);
2624 break;
2625 case ISDN_PROTO_L2_MODEM:
2626 isdn_tty_at_cout("modem", info);
2627 break;
2628 case ISDN_PROTO_L2_FAX:
2629 isdn_tty_at_cout("fax", info);
2630 break;
2631 default:
2632 isdn_tty_at_cout("unknown", info);
2633 break;
2635 if (m->mdmreg[REG_T70] & BIT_T70) {
2636 isdn_tty_at_cout("/T.70", info);
2637 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2638 isdn_tty_at_cout("+", info);
2640 isdn_tty_at_cout("\r\n", info);
2641 isdn_tty_at_cout(" Service: ", info);
2642 switch (info->last_si) {
2643 case 1:
2644 isdn_tty_at_cout("audio\r\n", info);
2645 break;
2646 case 5:
2647 isdn_tty_at_cout("btx\r\n", info);
2648 break;
2649 case 7:
2650 isdn_tty_at_cout("data\r\n", info);
2651 break;
2652 default:
2653 sprintf(s, "%d\r\n", info->last_si);
2654 isdn_tty_at_cout(s, info);
2655 break;
2657 sprintf(s, " Hangup location: %s\r\n", info->last_lhup ? "local" : "remote");
2658 isdn_tty_at_cout(s, info);
2659 sprintf(s, " Last cause: %s\r\n", info->last_cause);
2660 isdn_tty_at_cout(s, info);
2664 * Parse AT&.. commands.
2666 static int
2667 isdn_tty_cmd_ATand(char **p, modem_info *info)
2669 atemu *m = &info->emu;
2670 int i;
2671 char rb[100];
2673 #define MAXRB (sizeof(rb) - 1)
2675 switch (*p[0]) {
2676 case 'B':
2677 /* &B - Set Buffersize */
2678 p[0]++;
2679 i = isdn_getnum(p);
2680 if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
2681 PARSE_ERROR1;
2682 #ifdef CONFIG_ISDN_AUDIO
2683 if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
2684 PARSE_ERROR1;
2685 #endif
2686 m->mdmreg[REG_PSIZE] = i / 16;
2687 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2688 switch (m->mdmreg[REG_L2PROT]) {
2689 case ISDN_PROTO_L2_V11096:
2690 case ISDN_PROTO_L2_V11019:
2691 case ISDN_PROTO_L2_V11038:
2692 info->xmit_size /= 10;
2694 break;
2695 case 'C':
2696 /* &C - DCD Status */
2697 p[0]++;
2698 switch (isdn_getnum(p)) {
2699 case 0:
2700 m->mdmreg[REG_DCD] &= ~BIT_DCD;
2701 break;
2702 case 1:
2703 m->mdmreg[REG_DCD] |= BIT_DCD;
2704 break;
2705 default:
2706 PARSE_ERROR1
2708 break;
2709 case 'D':
2710 /* &D - Set DTR-Low-behavior */
2711 p[0]++;
2712 switch (isdn_getnum(p)) {
2713 case 0:
2714 m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
2715 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2716 break;
2717 case 2:
2718 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2719 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2720 break;
2721 case 3:
2722 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2723 m->mdmreg[REG_DTRR] |= BIT_DTRR;
2724 break;
2725 default:
2726 PARSE_ERROR1
2728 break;
2729 case 'E':
2730 /* &E -Set EAZ/MSN */
2731 p[0]++;
2732 isdn_tty_get_msnstr(m->msn, p);
2733 break;
2734 case 'F':
2735 /* &F -Set Factory-Defaults */
2736 p[0]++;
2737 if (info->msr & UART_MSR_DCD)
2738 PARSE_ERROR1;
2739 isdn_tty_reset_profile(m);
2740 isdn_tty_modem_reset_regs(info, 1);
2741 break;
2742 #ifdef DUMMY_HAYES_AT
2743 case 'K':
2744 /* only for be compilant with common scripts */
2745 /* &K Flowcontrol - no function */
2746 p[0]++;
2747 isdn_getnum(p);
2748 break;
2749 #endif
2750 case 'L':
2751 /* &L -Set Numbers to listen on */
2752 p[0]++;
2753 i = 0;
2754 while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
2755 (i < ISDN_LMSNLEN - 1))
2756 m->lmsn[i++] = *p[0]++;
2757 m->lmsn[i] = '\0';
2758 break;
2759 case 'R':
2760 /* &R - Set V.110 bitrate adaption */
2761 p[0]++;
2762 i = isdn_getnum(p);
2763 switch (i) {
2764 case 0:
2765 /* Switch off V.110, back to X.75 */
2766 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2767 m->mdmreg[REG_SI2] = 0;
2768 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2769 break;
2770 case 9600:
2771 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
2772 m->mdmreg[REG_SI2] = 197;
2773 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2774 break;
2775 case 19200:
2776 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
2777 m->mdmreg[REG_SI2] = 199;
2778 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2779 break;
2780 case 38400:
2781 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
2782 m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
2783 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2784 break;
2785 default:
2786 PARSE_ERROR1;
2788 /* Switch off T.70 */
2789 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2790 /* Set Service 7 */
2791 m->mdmreg[REG_SI1] |= 4;
2792 break;
2793 case 'S':
2794 /* &S - Set Windowsize */
2795 p[0]++;
2796 i = isdn_getnum(p);
2797 if ((i > 0) && (i < 9))
2798 m->mdmreg[REG_WSIZE] = i;
2799 else
2800 PARSE_ERROR1;
2801 break;
2802 case 'V':
2803 /* &V - Show registers */
2804 p[0]++;
2805 isdn_tty_at_cout("\r\n", info);
2806 for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
2807 sprintf(rb, "S%02d=%03d%s", i,
2808 m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
2809 isdn_tty_at_cout(rb, info);
2811 sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
2812 strlen(m->msn) ? m->msn : "None");
2813 isdn_tty_at_cout(rb, info);
2814 if (strlen(m->lmsn)) {
2815 isdn_tty_at_cout("\r\nListen: ", info);
2816 isdn_tty_at_cout(m->lmsn, info);
2817 isdn_tty_at_cout("\r\n", info);
2819 break;
2820 case 'W':
2821 /* &W - Write Profile */
2822 p[0]++;
2823 switch (*p[0]) {
2824 case '0':
2825 p[0]++;
2826 modem_write_profile(m);
2827 break;
2828 default:
2829 PARSE_ERROR1;
2831 break;
2832 case 'X':
2833 /* &X - Switch to BTX-Mode and T.70 */
2834 p[0]++;
2835 switch (isdn_getnum(p)) {
2836 case 0:
2837 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2838 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2839 break;
2840 case 1:
2841 m->mdmreg[REG_T70] |= BIT_T70;
2842 m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
2843 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2844 info->xmit_size = 112;
2845 m->mdmreg[REG_SI1] = 4;
2846 m->mdmreg[REG_SI2] = 0;
2847 break;
2848 case 2:
2849 m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
2850 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2851 info->xmit_size = 112;
2852 m->mdmreg[REG_SI1] = 4;
2853 m->mdmreg[REG_SI2] = 0;
2854 break;
2855 default:
2856 PARSE_ERROR1;
2858 break;
2859 default:
2860 PARSE_ERROR1;
2862 return 0;
2865 static int
2866 isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
2868 /* Some plausibility checks */
2869 switch (mreg) {
2870 case REG_L2PROT:
2871 if (mval > ISDN_PROTO_L2_MAX)
2872 return 1;
2873 break;
2874 case REG_PSIZE:
2875 if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
2876 return 1;
2877 #ifdef CONFIG_ISDN_AUDIO
2878 if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
2879 return 1;
2880 #endif
2881 info->xmit_size = mval * 16;
2882 switch (m->mdmreg[REG_L2PROT]) {
2883 case ISDN_PROTO_L2_V11096:
2884 case ISDN_PROTO_L2_V11019:
2885 case ISDN_PROTO_L2_V11038:
2886 info->xmit_size /= 10;
2888 break;
2889 case REG_SI1I:
2890 case REG_PLAN:
2891 case REG_SCREEN:
2892 /* readonly registers */
2893 return 1;
2895 return 0;
2899 * Perform ATS command
2901 static int
2902 isdn_tty_cmd_ATS(char **p, modem_info *info)
2904 atemu *m = &info->emu;
2905 int bitpos;
2906 int mreg;
2907 int mval;
2908 int bval;
2910 mreg = isdn_getnum(p);
2911 if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
2912 PARSE_ERROR1;
2913 switch (*p[0]) {
2914 case '=':
2915 p[0]++;
2916 mval = isdn_getnum(p);
2917 if (mval < 0 || mval > 255)
2918 PARSE_ERROR1;
2919 if (isdn_tty_check_ats(mreg, mval, info, m))
2920 PARSE_ERROR1;
2921 m->mdmreg[mreg] = mval;
2922 break;
2923 case '.':
2924 /* Set/Clear a single bit */
2925 p[0]++;
2926 bitpos = isdn_getnum(p);
2927 if ((bitpos < 0) || (bitpos > 7))
2928 PARSE_ERROR1;
2929 switch (*p[0]) {
2930 case '=':
2931 p[0]++;
2932 bval = isdn_getnum(p);
2933 if (bval < 0 || bval > 1)
2934 PARSE_ERROR1;
2935 if (bval)
2936 mval = m->mdmreg[mreg] | (1 << bitpos);
2937 else
2938 mval = m->mdmreg[mreg] & ~(1 << bitpos);
2939 if (isdn_tty_check_ats(mreg, mval, info, m))
2940 PARSE_ERROR1;
2941 m->mdmreg[mreg] = mval;
2942 break;
2943 case '?':
2944 p[0]++;
2945 isdn_tty_at_cout("\r\n", info);
2946 isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
2947 info);
2948 break;
2949 default:
2950 PARSE_ERROR1;
2952 break;
2953 case '?':
2954 p[0]++;
2955 isdn_tty_show_profile(mreg, info);
2956 break;
2957 default:
2958 PARSE_ERROR1;
2959 break;
2961 return 0;
2965 * Perform ATA command
2967 static void
2968 isdn_tty_cmd_ATA(modem_info *info)
2970 atemu *m = &info->emu;
2971 isdn_ctrl cmd;
2972 int l2;
2974 if (info->msr & UART_MSR_RI) {
2975 /* Accept incoming call */
2976 info->last_dir = 0;
2977 strcpy(info->last_num, dev->num[info->drv_index]);
2978 m->mdmreg[REG_RINGCNT] = 0;
2979 info->msr &= ~UART_MSR_RI;
2980 l2 = m->mdmreg[REG_L2PROT];
2981 #ifdef CONFIG_ISDN_AUDIO
2982 /* If more than one bit set in reg18, autoselect Layer2 */
2983 if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
2984 if (m->mdmreg[REG_SI1I] == 1) {
2985 if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
2986 l2 = ISDN_PROTO_L2_TRANS;
2987 } else
2988 l2 = ISDN_PROTO_L2_X75I;
2990 #endif
2991 cmd.driver = info->isdn_driver;
2992 cmd.command = ISDN_CMD_SETL2;
2993 cmd.arg = info->isdn_channel + (l2 << 8);
2994 info->last_l2 = l2;
2995 isdn_command(&cmd);
2996 cmd.driver = info->isdn_driver;
2997 cmd.command = ISDN_CMD_SETL3;
2998 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
2999 #ifdef CONFIG_ISDN_TTY_FAX
3000 if (l2 == ISDN_PROTO_L2_FAX) {
3001 cmd.parm.fax = info->fax;
3002 info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3004 #endif
3005 isdn_command(&cmd);
3006 cmd.driver = info->isdn_driver;
3007 cmd.arg = info->isdn_channel;
3008 cmd.command = ISDN_CMD_ACCEPTD;
3009 info->dialing = 16;
3010 info->emu.carrierwait = 0;
3011 isdn_command(&cmd);
3012 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3013 } else
3014 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3017 #ifdef CONFIG_ISDN_AUDIO
3019 * Parse AT+F.. commands
3021 static int
3022 isdn_tty_cmd_PLUSF(char **p, modem_info *info)
3024 atemu *m = &info->emu;
3025 char rs[20];
3027 if (!strncmp(p[0], "CLASS", 5)) {
3028 p[0] += 5;
3029 switch (*p[0]) {
3030 case '?':
3031 p[0]++;
3032 sprintf(rs, "\r\n%d",
3033 (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3034 #ifdef CONFIG_ISDN_TTY_FAX
3035 if (TTY_IS_FCLASS2(info))
3036 sprintf(rs, "\r\n2");
3037 else if (TTY_IS_FCLASS1(info))
3038 sprintf(rs, "\r\n1");
3039 #endif
3040 isdn_tty_at_cout(rs, info);
3041 break;
3042 case '=':
3043 p[0]++;
3044 switch (*p[0]) {
3045 case '0':
3046 p[0]++;
3047 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3048 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3049 m->mdmreg[REG_SI1] = 4;
3050 info->xmit_size =
3051 m->mdmreg[REG_PSIZE] * 16;
3052 break;
3053 #ifdef CONFIG_ISDN_TTY_FAX
3054 case '1':
3055 p[0]++;
3056 if (!(dev->global_features &
3057 ISDN_FEATURE_L3_FCLASS1))
3058 PARSE_ERROR1;
3059 m->mdmreg[REG_SI1] = 1;
3060 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3061 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3062 info->xmit_size =
3063 m->mdmreg[REG_PSIZE] * 16;
3064 break;
3065 case '2':
3066 p[0]++;
3067 if (!(dev->global_features &
3068 ISDN_FEATURE_L3_FCLASS2))
3069 PARSE_ERROR1;
3070 m->mdmreg[REG_SI1] = 1;
3071 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3072 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3073 info->xmit_size =
3074 m->mdmreg[REG_PSIZE] * 16;
3075 break;
3076 #endif
3077 case '8':
3078 p[0]++;
3079 /* L2 will change on dialout with si=1 */
3080 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3081 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3082 m->mdmreg[REG_SI1] = 5;
3083 info->xmit_size = VBUF;
3084 break;
3085 case '?':
3086 p[0]++;
3087 strcpy(rs, "\r\n0,");
3088 #ifdef CONFIG_ISDN_TTY_FAX
3089 if (dev->global_features &
3090 ISDN_FEATURE_L3_FCLASS1)
3091 strcat(rs, "1,");
3092 if (dev->global_features &
3093 ISDN_FEATURE_L3_FCLASS2)
3094 strcat(rs, "2,");
3095 #endif
3096 strcat(rs, "8");
3097 isdn_tty_at_cout(rs, info);
3098 break;
3099 default:
3100 PARSE_ERROR1;
3102 break;
3103 default:
3104 PARSE_ERROR1;
3106 return 0;
3108 #ifdef CONFIG_ISDN_TTY_FAX
3109 return (isdn_tty_cmd_PLUSF_FAX(p, info));
3110 #else
3111 PARSE_ERROR1;
3112 #endif
3116 * Parse AT+V.. commands
3118 static int
3119 isdn_tty_cmd_PLUSV(char **p, modem_info *info)
3121 atemu *m = &info->emu;
3122 isdn_ctrl cmd;
3123 static char *vcmd[] =
3124 {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
3125 int i;
3126 int par1;
3127 int par2;
3128 char rs[20];
3130 i = 0;
3131 while (vcmd[i]) {
3132 if (!strncmp(vcmd[i], p[0], 2)) {
3133 p[0] += 2;
3134 break;
3136 i++;
3138 switch (i) {
3139 case 0:
3140 /* AT+VNH - Auto hangup feature */
3141 switch (*p[0]) {
3142 case '?':
3143 p[0]++;
3144 isdn_tty_at_cout("\r\n1", info);
3145 break;
3146 case '=':
3147 p[0]++;
3148 switch (*p[0]) {
3149 case '1':
3150 p[0]++;
3151 break;
3152 case '?':
3153 p[0]++;
3154 isdn_tty_at_cout("\r\n1", info);
3155 break;
3156 default:
3157 PARSE_ERROR1;
3159 break;
3160 default:
3161 PARSE_ERROR1;
3163 break;
3164 case 1:
3165 /* AT+VIP - Reset all voice parameters */
3166 isdn_tty_modem_reset_vpar(m);
3167 break;
3168 case 2:
3169 /* AT+VLS - Select device, accept incoming call */
3170 switch (*p[0]) {
3171 case '?':
3172 p[0]++;
3173 sprintf(rs, "\r\n%d", m->vpar[0]);
3174 isdn_tty_at_cout(rs, info);
3175 break;
3176 case '=':
3177 p[0]++;
3178 switch (*p[0]) {
3179 case '0':
3180 p[0]++;
3181 m->vpar[0] = 0;
3182 break;
3183 case '2':
3184 p[0]++;
3185 m->vpar[0] = 2;
3186 break;
3187 case '?':
3188 p[0]++;
3189 isdn_tty_at_cout("\r\n0,2", info);
3190 break;
3191 default:
3192 PARSE_ERROR1;
3194 break;
3195 default:
3196 PARSE_ERROR1;
3198 break;
3199 case 3:
3200 /* AT+VRX - Start recording */
3201 if (!m->vpar[0])
3202 PARSE_ERROR1;
3203 if (info->online != 1) {
3204 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3205 return 1;
3207 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3208 if (!info->dtmf_state) {
3209 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3210 PARSE_ERROR1;
3212 info->silence_state = isdn_audio_silence_init(info->silence_state);
3213 if (!info->silence_state) {
3214 printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3215 PARSE_ERROR1;
3217 if (m->vpar[3] < 5) {
3218 info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3219 if (!info->adpcmr) {
3220 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3221 PARSE_ERROR1;
3224 #ifdef ISDN_DEBUG_AT
3225 printk(KERN_DEBUG "AT: +VRX\n");
3226 #endif
3227 info->vonline |= 1;
3228 isdn_tty_modem_result(RESULT_CONNECT, info);
3229 return 0;
3230 break;
3231 case 4:
3232 /* AT+VSD - Silence detection */
3233 switch (*p[0]) {
3234 case '?':
3235 p[0]++;
3236 sprintf(rs, "\r\n<%d>,<%d>",
3237 m->vpar[1],
3238 m->vpar[2]);
3239 isdn_tty_at_cout(rs, info);
3240 break;
3241 case '=':
3242 p[0]++;
3243 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3244 par1 = isdn_getnum(p);
3245 if ((par1 < 0) || (par1 > 31))
3246 PARSE_ERROR1;
3247 if (*p[0] != ',')
3248 PARSE_ERROR1;
3249 p[0]++;
3250 par2 = isdn_getnum(p);
3251 if ((par2 < 0) || (par2 > 255))
3252 PARSE_ERROR1;
3253 m->vpar[1] = par1;
3254 m->vpar[2] = par2;
3255 break;
3256 } else
3257 if (*p[0] == '?') {
3258 p[0]++;
3259 isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3260 info);
3261 break;
3262 } else
3263 PARSE_ERROR1;
3264 break;
3265 default:
3266 PARSE_ERROR1;
3268 break;
3269 case 5:
3270 /* AT+VSM - Select compression */
3271 switch (*p[0]) {
3272 case '?':
3273 p[0]++;
3274 sprintf(rs, "\r\n<%d>,<%d><8000>",
3275 m->vpar[3],
3276 m->vpar[1]);
3277 isdn_tty_at_cout(rs, info);
3278 break;
3279 case '=':
3280 p[0]++;
3281 switch (*p[0]) {
3282 case '2':
3283 case '3':
3284 case '4':
3285 case '5':
3286 case '6':
3287 par1 = isdn_getnum(p);
3288 if ((par1 < 2) || (par1 > 6))
3289 PARSE_ERROR1;
3290 m->vpar[3] = par1;
3291 break;
3292 case '?':
3293 p[0]++;
3294 isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3295 info);
3296 isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3297 info);
3298 isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3299 info);
3300 isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3301 info);
3302 isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3303 info);
3304 break;
3305 default:
3306 PARSE_ERROR1;
3308 break;
3309 default:
3310 PARSE_ERROR1;
3312 break;
3313 case 6:
3314 /* AT+VTX - Start sending */
3315 if (!m->vpar[0])
3316 PARSE_ERROR1;
3317 if (info->online != 1) {
3318 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3319 return 1;
3321 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3322 if (!info->dtmf_state) {
3323 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3324 PARSE_ERROR1;
3326 if (m->vpar[3] < 5) {
3327 info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3328 if (!info->adpcms) {
3329 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3330 PARSE_ERROR1;
3333 #ifdef ISDN_DEBUG_AT
3334 printk(KERN_DEBUG "AT: +VTX\n");
3335 #endif
3336 m->lastDLE = 0;
3337 info->vonline |= 2;
3338 isdn_tty_modem_result(RESULT_CONNECT, info);
3339 return 0;
3340 break;
3341 case 7:
3342 /* AT+VDD - DTMF detection */
3343 switch (*p[0]) {
3344 case '?':
3345 p[0]++;
3346 sprintf(rs, "\r\n<%d>,<%d>",
3347 m->vpar[4],
3348 m->vpar[5]);
3349 isdn_tty_at_cout(rs, info);
3350 break;
3351 case '=':
3352 p[0]++;
3353 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3354 if (info->online != 1)
3355 PARSE_ERROR1;
3356 par1 = isdn_getnum(p);
3357 if ((par1 < 0) || (par1 > 15))
3358 PARSE_ERROR1;
3359 if (*p[0] != ',')
3360 PARSE_ERROR1;
3361 p[0]++;
3362 par2 = isdn_getnum(p);
3363 if ((par2 < 0) || (par2 > 255))
3364 PARSE_ERROR1;
3365 m->vpar[4] = par1;
3366 m->vpar[5] = par2;
3367 cmd.driver = info->isdn_driver;
3368 cmd.command = ISDN_CMD_AUDIO;
3369 cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3370 cmd.parm.num[0] = par1;
3371 cmd.parm.num[1] = par2;
3372 isdn_command(&cmd);
3373 break;
3374 } else
3375 if (*p[0] == '?') {
3376 p[0]++;
3377 isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3378 info);
3379 break;
3380 } else
3381 PARSE_ERROR1;
3382 break;
3383 default:
3384 PARSE_ERROR1;
3386 break;
3387 default:
3388 PARSE_ERROR1;
3390 return 0;
3392 #endif /* CONFIG_ISDN_AUDIO */
3395 * Parse and perform an AT-command-line.
3397 static void
3398 isdn_tty_parse_at(modem_info *info)
3400 atemu *m = &info->emu;
3401 char *p;
3402 char ds[ISDN_MSNLEN];
3404 #ifdef ISDN_DEBUG_AT
3405 printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
3406 #endif
3407 for (p = &m->mdmcmd[2]; *p;) {
3408 switch (*p) {
3409 case ' ':
3410 p++;
3411 break;
3412 case 'A':
3413 /* A - Accept incoming call */
3414 p++;
3415 isdn_tty_cmd_ATA(info);
3416 return;
3417 case 'D':
3418 /* D - Dial */
3419 if (info->msr & UART_MSR_DCD)
3420 PARSE_ERROR;
3421 if (info->msr & UART_MSR_RI) {
3422 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3423 return;
3425 isdn_tty_getdial(++p, ds, sizeof ds);
3426 p += strlen(p);
3427 if (!strlen(m->msn))
3428 isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
3429 else if (strlen(ds))
3430 isdn_tty_dial(ds, info, m);
3431 else
3432 PARSE_ERROR;
3433 return;
3434 case 'E':
3435 /* E - Turn Echo on/off */
3436 p++;
3437 switch (isdn_getnum(&p)) {
3438 case 0:
3439 m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
3440 break;
3441 case 1:
3442 m->mdmreg[REG_ECHO] |= BIT_ECHO;
3443 break;
3444 default:
3445 PARSE_ERROR;
3447 break;
3448 case 'H':
3449 /* H - On/Off-hook */
3450 p++;
3451 switch (*p) {
3452 case '0':
3453 p++;
3454 isdn_tty_on_hook(info);
3455 break;
3456 case '1':
3457 p++;
3458 isdn_tty_off_hook();
3459 break;
3460 default:
3461 isdn_tty_on_hook(info);
3462 break;
3464 break;
3465 case 'I':
3466 /* I - Information */
3467 p++;
3468 isdn_tty_at_cout("\r\nLinux ISDN", info);
3469 switch (*p) {
3470 case '0':
3471 case '1':
3472 p++;
3473 break;
3474 case '2':
3475 p++;
3476 isdn_tty_report(info);
3477 break;
3478 case '3':
3479 p++;
3480 snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
3481 isdn_tty_at_cout(ds, info);
3482 break;
3483 default:;
3485 break;
3486 #ifdef DUMMY_HAYES_AT
3487 case 'L':
3488 case 'M':
3489 /* only for be compilant with common scripts */
3490 /* no function */
3491 p++;
3492 isdn_getnum(&p);
3493 break;
3494 #endif
3495 case 'O':
3496 /* O - Go online */
3497 p++;
3498 if (info->msr & UART_MSR_DCD)
3499 /* if B-Channel is up */
3500 isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
3501 else
3502 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3503 return;
3504 case 'Q':
3505 /* Q - Turn Emulator messages on/off */
3506 p++;
3507 switch (isdn_getnum(&p)) {
3508 case 0:
3509 m->mdmreg[REG_RESP] |= BIT_RESP;
3510 break;
3511 case 1:
3512 m->mdmreg[REG_RESP] &= ~BIT_RESP;
3513 break;
3514 default:
3515 PARSE_ERROR;
3517 break;
3518 case 'S':
3519 /* S - Set/Get Register */
3520 p++;
3521 if (isdn_tty_cmd_ATS(&p, info))
3522 return;
3523 break;
3524 case 'V':
3525 /* V - Numeric or ASCII Emulator-messages */
3526 p++;
3527 switch (isdn_getnum(&p)) {
3528 case 0:
3529 m->mdmreg[REG_RESP] |= BIT_RESPNUM;
3530 break;
3531 case 1:
3532 m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
3533 break;
3534 default:
3535 PARSE_ERROR;
3537 break;
3538 case 'Z':
3539 /* Z - Load Registers from Profile */
3540 p++;
3541 if (info->msr & UART_MSR_DCD) {
3542 info->online = 0;
3543 isdn_tty_on_hook(info);
3545 isdn_tty_modem_reset_regs(info, 1);
3546 break;
3547 case '+':
3548 p++;
3549 switch (*p) {
3550 #ifdef CONFIG_ISDN_AUDIO
3551 case 'F':
3552 p++;
3553 if (isdn_tty_cmd_PLUSF(&p, info))
3554 return;
3555 break;
3556 case 'V':
3557 if ((!(m->mdmreg[REG_SI1] & 1)) ||
3558 (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
3559 PARSE_ERROR;
3560 p++;
3561 if (isdn_tty_cmd_PLUSV(&p, info))
3562 return;
3563 break;
3564 #endif /* CONFIG_ISDN_AUDIO */
3565 case 'S': /* SUSPEND */
3566 p++;
3567 isdn_tty_get_msnstr(ds, &p);
3568 isdn_tty_suspend(ds, info, m);
3569 break;
3570 case 'R': /* RESUME */
3571 p++;
3572 isdn_tty_get_msnstr(ds, &p);
3573 isdn_tty_resume(ds, info, m);
3574 break;
3575 case 'M': /* MESSAGE */
3576 p++;
3577 isdn_tty_send_msg(info, m, p);
3578 break;
3579 default:
3580 PARSE_ERROR;
3582 break;
3583 case '&':
3584 p++;
3585 if (isdn_tty_cmd_ATand(&p, info))
3586 return;
3587 break;
3588 default:
3589 PARSE_ERROR;
3592 #ifdef CONFIG_ISDN_AUDIO
3593 if (!info->vonline)
3594 #endif
3595 isdn_tty_modem_result(RESULT_OK, info);
3598 /* Need own toupper() because standard-toupper is not available
3599 * within modules.
3601 #define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
3604 * Perform line-editing of AT-commands
3606 * Parameters:
3607 * p inputbuffer
3608 * count length of buffer
3609 * channel index to line (minor-device)
3611 static int
3612 isdn_tty_edit_at(const char *p, int count, modem_info *info)
3614 atemu *m = &info->emu;
3615 int total = 0;
3616 u_char c;
3617 char eb[2];
3618 int cnt;
3620 for (cnt = count; cnt > 0; p++, cnt--) {
3621 c = *p;
3622 total++;
3623 if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
3624 /* Separator (CR or LF) */
3625 m->mdmcmd[m->mdmcmdl] = 0;
3626 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3627 eb[0] = c;
3628 eb[1] = 0;
3629 isdn_tty_at_cout(eb, info);
3631 if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
3632 isdn_tty_parse_at(info);
3633 m->mdmcmdl = 0;
3634 continue;
3636 if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
3637 /* Backspace-Function */
3638 if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
3639 if (m->mdmcmdl)
3640 m->mdmcmdl--;
3641 if (m->mdmreg[REG_ECHO] & BIT_ECHO)
3642 isdn_tty_at_cout("\b", info);
3644 continue;
3646 if (cmdchar(c)) {
3647 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3648 eb[0] = c;
3649 eb[1] = 0;
3650 isdn_tty_at_cout(eb, info);
3652 if (m->mdmcmdl < 255) {
3653 c = my_toupper(c);
3654 switch (m->mdmcmdl) {
3655 case 1:
3656 if (c == 'T') {
3657 m->mdmcmd[m->mdmcmdl] = c;
3658 m->mdmcmd[++m->mdmcmdl] = 0;
3659 break;
3660 } else
3661 m->mdmcmdl = 0;
3662 /* Fall through, check for 'A' */
3663 case 0:
3664 if (c == 'A') {
3665 m->mdmcmd[m->mdmcmdl] = c;
3666 m->mdmcmd[++m->mdmcmdl] = 0;
3668 break;
3669 default:
3670 m->mdmcmd[m->mdmcmdl] = c;
3671 m->mdmcmd[++m->mdmcmdl] = 0;
3676 return total;
3680 * Switch all modem-channels who are online and got a valid
3681 * escape-sequence 1.5 seconds ago, to command-mode.
3682 * This function is called every second via timer-interrupt from within
3683 * timer-dispatcher isdn_timer_function()
3685 void
3686 isdn_tty_modem_escape(void)
3688 int ton = 0;
3689 int i;
3690 int midx;
3692 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
3693 if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
3694 modem_info *info = &dev->mdm.info[midx];
3695 if (info->online) {
3696 ton = 1;
3697 if ((info->emu.pluscount == 3) &&
3698 time_after(jiffies,
3699 info->emu.lastplus + PLUSWAIT2)) {
3700 info->emu.pluscount = 0;
3701 info->online = 0;
3702 isdn_tty_modem_result(RESULT_OK, info);
3706 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
3710 * Put a RING-message to all modem-channels who have the RI-bit set.
3711 * This function is called every second via timer-interrupt from within
3712 * timer-dispatcher isdn_timer_function()
3714 void
3715 isdn_tty_modem_ring(void)
3717 int ton = 0;
3718 int i;
3720 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3721 modem_info *info = &dev->mdm.info[i];
3722 if (info->msr & UART_MSR_RI) {
3723 ton = 1;
3724 isdn_tty_modem_result(RESULT_RING, info);
3727 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
3731 * For all online tty's, try sending data to
3732 * the lower levels.
3734 void
3735 isdn_tty_modem_xmit(void)
3737 int ton = 1;
3738 int i;
3740 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3741 modem_info *info = &dev->mdm.info[i];
3742 if (info->online) {
3743 ton = 1;
3744 isdn_tty_senddown(info);
3745 isdn_tty_tint(info);
3748 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
3752 * Check all channels if we have a 'no carrier' timeout.
3753 * Timeout value is set by Register S7.
3755 void
3756 isdn_tty_carrier_timeout(void)
3758 int ton = 0;
3759 int i;
3761 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3762 modem_info *info = &dev->mdm.info[i];
3763 if (!info->dialing)
3764 continue;
3765 if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
3766 info->dialing = 0;
3767 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3768 isdn_tty_modem_hup(info, 1);
3769 } else
3770 ton = 1;
3772 isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);