Linux-2.4.0-test2
[davej-history.git] / drivers / isdn / isdn_tty.c
blob665ac46e8ba9cae487f0b038e8c8c30ab09a6d2e
1 /* $Id: isdn_tty.c,v 1.84 2000/02/16 15:10:14 paul Exp $
3 * Linux ISDN subsystem, tty functions and AT-command emulator (linklevel).
5 * Copyright 1994-1999 by Fritz Elfert (fritz@isdn4linux.de)
6 * Copyright 1995,96 by Thinking Objects Software GmbH Wuerzburg
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * $Log: isdn_tty.c,v $
23 * Revision 1.84 2000/02/16 15:10:14 paul
24 * If a ttyI has no open FDs, don't connect incoming calls to it.
25 * (Hangup on close of last FD is still to be done.)
27 * Revision 1.83 2000/02/16 14:59:33 paul
28 * translated ISDN_MODEM_ANZREG to ISDN_MODEM_NUMREG for english speakers;
29 * used defines for result codes;
30 * fixed RING ... RUNG problem (no empty lines in between).
32 * Revision 1.82 2000/01/23 18:45:37 keil
33 * Change EAZ mapping to forbit the use of cards (insert a "-" for the MSN)
35 * Revision 1.81 2000/01/20 19:55:33 keil
36 * Add FAX Class 1 support
38 * Revision 1.80 1999/11/07 13:34:30 armin
39 * Fixed AT command line editor
41 * Revision 1.79 1999/10/29 18:35:08 armin
42 * Check number len in isdn_get_msnstr() to avoid buffer overflow.
44 * Revision 1.78 1999/10/28 23:03:51 armin
45 * Bugfix: now freeing channel on modem_hup() even when
46 * usage on ttyI has changed and error-report for
47 * AT-commands on wrong channel-state.
49 * Revision 1.77 1999/10/26 21:13:14 armin
50 * using define for checking phone number len in isdn_tty_getdial()
52 * Revision 1.76 1999/10/11 22:16:26 keil
53 * Suspend/Resume is possible without explicit ID too
55 * Revision 1.75 1999/10/08 18:59:32 armin
56 * Bugfix of too small MSN buffer and checking phone number
57 * in isdn_tty_getdial()
59 * Revision 1.74 1999/09/04 06:20:04 keil
60 * Changes from kernel set_current_state()
62 * Revision 1.73 1999/08/28 21:56:27 keil
63 * misplaced #endif caused ttyI crash in 2.3.X
65 * Revision 1.72 1999/07/31 12:59:45 armin
66 * Added tty fax capabilities.
68 * Revision 1.71 1999/07/27 10:34:34 armin
69 * Fixed last change. Did not compile with AUDIO support off.
71 * Revision 1.70 1999/07/25 16:17:58 keil
72 * Fix Suspend/Resume
74 * Revision 1.69 1999/07/25 12:56:15 armin
75 * isdn_tty_at_cout() now queues the message if online and
76 * data is in queue or flip buffer is full.
77 * needed for fax connections.
79 * Revision 1.68 1999/07/11 17:51:51 armin
80 * Bugfix, "-" was missing for AT&L settings.
82 * Revision 1.67 1999/07/11 17:14:12 armin
83 * Added new layer 2 and 3 protocols for Fax and DSP functions.
84 * Moved "Add CPN to RING message" to new register S23,
85 * "Display message" is now correct on register S13 bit 7.
86 * New audio command AT+VDD implemented (deactivate DTMF decoder and
87 * activate possible existing hardware/DSP decoder).
88 * Moved some tty defines to .h file.
89 * Made whitespace possible in AT command line.
90 * Some AT-emulator output bugfixes.
91 * First Fax G3 implementations.
93 * Revision 1.66 1999/07/07 10:13:46 detabc
94 * remove unused messages
96 * Revision 1.65 1999/07/04 21:01:59 werner
97 * Added support for keypad and display (ported from 2.0)
99 * Revision 1.64 1999/07/01 08:30:00 keil
100 * compatibility to 2.3 kernel
102 * Revision 1.63 1999/04/12 12:33:39 fritz
103 * Changes from 2.0 tree.
105 * Revision 1.62 1999/03/02 12:04:48 armin
106 * -added ISDN_STAT_ADDCH to increase supported channels after
107 * register_isdn().
108 * -ttyI now goes on-hook on ATZ when B-Ch is connected.
109 * -added timer-function for register S7 (Wait for Carrier).
110 * -analog modem (ISDN_PROTO_L2_MODEM) implementations.
111 * -on L2_MODEM a string will be appended to the CONNECT-Message,
112 * which is provided by the HL-Driver in parm.num in ISDN_STAT_BCONN.
113 * -variable "dialing" used for ATA also, for interrupting call
114 * establishment and register S7.
116 * Revision 1.61 1999/01/27 22:53:11 he
117 * minor updates (spellings, jiffies wrap around in isdn_tty)
119 * Revision 1.60 1998/11/15 23:57:32 keil
120 * changes for 2.1.127
122 * Revision 1.59 1998/08/20 13:50:15 keil
123 * More support for hybrid modem (not working yet)
125 * Revision 1.58 1998/07/26 18:48:45 armin
126 * Added silence detection in voice receive mode.
128 * Revision 1.57 1998/06/26 15:12:36 fritz
129 * Added handling of STAT_ICALL with incomplete CPN.
130 * Added AT&L for ttyI emulator.
131 * Added more locking stuff in tty_write.
133 * Revision 1.56 1998/06/18 23:31:51 fritz
134 * Replaced cli()/restore_flags() in isdn_tty_write() by locking.
135 * Removed direct-senddown feature in isdn_tty_write because it will
136 * never succeed with locking and is useless anyway.
138 * Revision 1.55 1998/06/17 19:50:55 he
139 * merged with 2.1.10[34] (cosmetics and udelay() -> mdelay())
140 * brute force fix to avoid Ugh's in isdn_tty_write()
141 * cleaned up some dead code
145 * Revision 1.52 1998/03/19 13:18:21 keil
146 * Start of a CAPI like interface for supplementary Service
147 * first service: SUSPEND
150 * Revision 1.49 1998/03/08 00:01:59 fritz
151 * Bugfix: Lowlevel module usage and channel usage were not
152 * reset on NO DCHANNEL.
154 * Revision 1.48 1998/03/07 12:28:15 tsbogend
155 * fixed kernel unaligned traps on Linux/Alpha
157 * Revision 1.47 1998/02/22 19:44:14 fritz
158 * Bugfixes and improvements regarding V.110, V.110 now running.
160 * Revision 1.46 1998/02/20 17:23:08 fritz
161 * Changes for recent kernels.
162 * Merged in contributions by Thomas Pfeiffer (V.110 T.70+ Extended FAX stuff)
163 * Added symbolic constants for Modem-Registers.
165 * Revision 1.45 1998/01/31 22:07:49 keil
166 * changes for newer kernels
168 * Revision 1.44 1998/01/31 19:30:02 calle
169 * Merged changes from and for 2.1.82, not tested only compiled ...
171 * Revision 1.43 1997/10/09 21:29:04 fritz
172 * New HL<->LL interface:
173 * New BSENT callback with nr. of bytes included.
174 * Sending without ACK.
175 * New L1 error status (not yet in use).
176 * Cleaned up obsolete structures.
177 * Implemented Cisco-SLARP.
178 * Changed local net-interface data to be dynamically allocated.
179 * Removed old 2.0 compatibility stuff.
181 * Revision 1.42 1997/10/01 09:20:49 fritz
182 * Removed old compatibility stuff for 2.0.X kernels.
183 * From now on, this code is for 2.1.X ONLY!
184 * Old stuff is still in the separate branch.
186 * Revision 1.41 1997/05/27 15:17:31 fritz
187 * Added changes for recent 2.1.x kernels:
188 * changed return type of isdn_close
189 * queue_task_* -> queue_task
190 * clear/set_bit -> test_and_... where apropriate.
191 * changed type of hard_header_cache parameter.
193 * Revision 1.40 1997/03/24 22:55:27 fritz
194 * Added debug code for status callbacks.
196 * Revision 1.39 1997/03/21 18:25:56 fritz
197 * Corrected CTS handling.
199 * Revision 1.38 1997/03/07 12:13:35 fritz
200 * Bugfix: Send audio in adpcm format was broken.
201 * Bugfix: CTS handling was wrong.
203 * Revision 1.37 1997/03/07 01:37:34 fritz
204 * Bugfix: Did not compile with CONFIG_ISDN_AUDIO disabled.
205 * Bugfix: isdn_tty_tint() did not handle lowlevel errors correctly.
206 * Bugfix: conversion was wrong when sending ulaw audio.
207 * Added proper ifdef's for CONFIG_ISDN_AUDIO
209 * Revision 1.36 1997/03/04 21:41:55 fritz
210 * Fix: Excessive stack usage of isdn_tty_senddown()
211 * and isdn_tty_end_vrx() could lead to problems.
213 * Revision 1.35 1997/03/02 19:05:52 fritz
214 * Bugfix: Avoid recursion.
216 * Revision 1.34 1997/03/02 14:29:22 fritz
217 * More ttyI related cleanup.
219 * Revision 1.33 1997/02/28 02:32:45 fritz
220 * Cleanup: Moved some tty related stuff from isdn_common.c
221 * to isdn_tty.c
222 * Bugfix: Bisync protocol did not behave like documented.
224 * Revision 1.32 1997/02/23 15:43:03 fritz
225 * Small change in handling of incoming calls
226 * documented in newest version of ttyI.4
228 * Revision 1.31 1997/02/21 13:05:57 fritz
229 * Bugfix: Remote hangup did not set location-info on ttyI's
231 * Revision 1.30 1997/02/18 09:41:05 fritz
232 * Added support for bitwise access to modem registers (ATSx.y=n, ATSx.y?).
233 * Beautified output of AT&V.
235 * Revision 1.29 1997/02/16 12:11:51 fritz
236 * Added S13,Bit4 option.
238 * Revision 1.28 1997/02/10 22:07:08 fritz
239 * Added 2 modem registers for numbering plan and screening info.
241 * Revision 1.27 1997/02/10 21:31:14 fritz
242 * Changed setup-interface (incoming and outgoing).
244 * Revision 1.26 1997/02/10 20:12:48 fritz
245 * Changed interface for reporting incoming calls.
247 * Revision 1.25 1997/02/03 23:04:30 fritz
248 * Reformatted according CodingStyle.
249 * skb->free stuff replaced by macro.
250 * Finished full-duplex audio.
252 * Revision 1.24 1997/01/14 01:32:42 fritz
253 * Changed audio receive not to rely on skb->users and skb->lock.
254 * Added ATI2 and related variables.
255 * Started adding full-duplex audio capability.
257 * Revision 1.23 1996/10/22 23:14:02 fritz
258 * Changes for compatibility to 2.0.X and 2.1.X kernels.
260 * Revision 1.22 1996/10/19 18:56:43 fritz
261 * ATZ did not change the xmitbuf size.
263 * Revision 1.21 1996/06/24 17:40:28 fritz
264 * Bugfix: Did not compile without CONFIG_ISDN_AUDIO
266 * Revision 1.20 1996/06/15 14:59:39 fritz
267 * Fixed isdn_tty_tint() to handle partially sent
268 * sk_buffs.
270 * Revision 1.19 1996/06/12 15:53:56 fritz
271 * Bugfix: AT+VTX and AT+VRX could be executed without
272 * having a connection.
273 * Missing check for NULL tty in isdn_tty_flush_buffer().
275 * Revision 1.18 1996/06/07 11:17:33 tsbogend
276 * added missing #ifdef CONFIG_ISDN_AUDIO to make compiling without
277 * audio support possible
279 * Revision 1.17 1996/06/06 14:55:47 fritz
280 * Changed to support DTMF decoding on audio playback also.
281 * Bugfix: Added check for invalid info->isdn_driver in
282 * isdn_tty_senddown().
283 * Clear ncarrier flag on last close() of a tty.
285 * Revision 1.16 1996/06/05 02:24:12 fritz
286 * Added DTMF decoder for audio mode.
288 * Revision 1.15 1996/06/03 20:35:01 fritz
289 * Fixed typos.
291 * Revision 1.14 1996/06/03 20:12:19 fritz
292 * Fixed typos.
293 * Added call to write_wakeup via isdn_tty_flush_buffer()
294 * in isdn_tty_modem_hup().
296 * Revision 1.13 1996/05/31 01:33:29 fritz
297 * Changed buffering due to bad performance with mgetty.
298 * Now sk_buff is delayed allocated in isdn_tty_senddown
299 * using xmit_buff like in standard serial driver.
300 * Fixed module locking.
301 * Added DLE-DC4 handling in voice mode.
303 * Revision 1.12 1996/05/19 01:34:40 fritz
304 * Bugfix: ATS returned error.
305 * Register 20 made readonly.
307 * Revision 1.11 1996/05/18 01:37:03 fritz
308 * Added spelling corrections and some minor changes
309 * to stay in sync with kernel.
311 * Revision 1.10 1996/05/17 03:51:49 fritz
312 * Changed DLE handling for audio receive.
314 * Revision 1.9 1996/05/11 21:52:07 fritz
315 * Changed queue management to use sk_buffs.
317 * Revision 1.8 1996/05/10 08:49:43 fritz
318 * Checkin before major changes of tty-code.
320 * Revision 1.7 1996/05/07 09:15:09 fritz
321 * Reorganized and general cleanup.
322 * Bugfixes:
323 * - Audio-transmit working now.
324 * - "NO CARRIER" now reported, when hanging up with DTR low.
325 * - Corrected CTS handling.
327 * Revision 1.6 1996/05/02 03:59:25 fritz
328 * Bugfixes:
329 * - On dialout, layer-2 setup had been incomplete
330 * when using new auto-layer2 feature.
331 * - On hangup, "NO CARRIER" message sometimes missing.
333 * Revision 1.5 1996/04/30 21:05:25 fritz
334 * Test commit
336 * Revision 1.4 1996/04/20 16:39:54 fritz
337 * Changed all io to go through generic routines in isdn_common.c
338 * Fixed a real ugly bug in modem-emulator: 'ATA' had been accepted
339 * even when a call has been cancelled from the remote machine.
341 * Revision 1.3 1996/02/11 02:12:32 fritz
342 * Bugfixes according to similar fixes in standard serial.c of kernel.
344 * Revision 1.2 1996/01/22 05:12:25 fritz
345 * replaced my_atoi by simple_strtoul
347 * Revision 1.1 1996/01/09 04:13:18 fritz
348 * Initial revision
351 #undef ISDN_TTY_STAT_DEBUG
353 #define __NO_VERSION__
354 #include <linux/config.h>
355 #include <linux/module.h>
356 #include <linux/isdn.h>
357 #include "isdn_common.h"
358 #include "isdn_tty.h"
359 #ifdef CONFIG_ISDN_AUDIO
360 #include "isdn_audio.h"
361 #define VBUF 0x3e0
362 #define VBUFX (VBUF/16)
363 #endif
365 #define FIX_FILE_TRANSFER
366 #define DUMMY_HAYES_AT
368 /* Prototypes */
370 static int isdn_tty_edit_at(const char *, int, modem_info *, int);
371 static void isdn_tty_check_esc(const u_char *, u_char, int, int *, int *, int);
372 static void isdn_tty_modem_reset_regs(modem_info *, int);
373 static void isdn_tty_cmd_ATA(modem_info *);
374 static void isdn_tty_flush_buffer(struct tty_struct *);
375 static void isdn_tty_modem_result(int, modem_info *);
376 #ifdef CONFIG_ISDN_AUDIO
377 static int isdn_tty_countDLE(unsigned char *, int);
378 #endif
380 /* Leave this unchanged unless you know what you do! */
381 #define MODEM_PARANOIA_CHECK
382 #define MODEM_DO_RESTART
384 #ifdef CONFIG_DEVFS_FS
385 static char *isdn_ttyname_ttyI = "isdn/ttyI%d";
386 static char *isdn_ttyname_cui = "isdn/cui%d";
387 #else
388 static char *isdn_ttyname_ttyI = "ttyI";
389 static char *isdn_ttyname_cui = "cui";
390 #endif
392 static int bit2si[8] =
393 {1, 5, 7, 7, 7, 7, 7, 7};
394 static int si2bit[8] =
395 {4, 1, 4, 4, 4, 4, 4, 4};
397 char *isdn_tty_revision = "$Revision: 1.84 $";
400 /* isdn_tty_try_read() is called from within isdn_tty_rcv_skb()
401 * to stuff incoming data directly into a tty's flip-buffer. This
402 * is done to speed up tty-receiving if the receive-queue is empty.
403 * This routine MUST be called with interrupts off.
404 * Return:
405 * 1 = Success
406 * 0 = Failure, data has to be buffered and later processed by
407 * isdn_tty_readmodem().
409 static int
410 isdn_tty_try_read(modem_info * info, struct sk_buff *skb)
412 int c;
413 int len;
414 struct tty_struct *tty;
416 if (info->online) {
417 if ((tty = info->tty)) {
418 if (info->mcr & UART_MCR_RTS) {
419 c = TTY_FLIPBUF_SIZE - tty->flip.count;
420 len = skb->len
421 #ifdef CONFIG_ISDN_AUDIO
422 + ISDN_AUDIO_SKB_DLECOUNT(skb)
423 #endif
425 if (c >= len) {
426 #ifdef CONFIG_ISDN_AUDIO
427 if (ISDN_AUDIO_SKB_DLECOUNT(skb))
428 while (skb->len--) {
429 if (*skb->data == DLE)
430 tty_insert_flip_char(tty, DLE, 0);
431 tty_insert_flip_char(tty, *skb->data++, 0);
432 } else {
433 #endif
434 memcpy(tty->flip.char_buf_ptr,
435 skb->data, len);
436 tty->flip.count += len;
437 tty->flip.char_buf_ptr += len;
438 memset(tty->flip.flag_buf_ptr, 0, len);
439 tty->flip.flag_buf_ptr += len;
440 #ifdef CONFIG_ISDN_AUDIO
442 #endif
443 if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
444 tty->flip.flag_buf_ptr[len - 1] = 0xff;
445 queue_task(&tty->flip.tqueue, &tq_timer);
446 kfree_skb(skb);
447 return 1;
452 return 0;
455 /* isdn_tty_readmodem() is called periodically from within timer-interrupt.
456 * It tries getting received data from the receive queue an stuff it into
457 * the tty's flip-buffer.
459 void
460 isdn_tty_readmodem(void)
462 int resched = 0;
463 int midx;
464 int i;
465 int c;
466 int r;
467 ulong flags;
468 struct tty_struct *tty;
469 modem_info *info;
471 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
472 if ((midx = dev->m_idx[i]) >= 0) {
473 info = &dev->mdm.info[midx];
474 if (info->online) {
475 r = 0;
476 #ifdef CONFIG_ISDN_AUDIO
477 isdn_audio_eval_dtmf(info);
478 if ((info->vonline & 1) && (info->emu.vpar[1]))
479 isdn_audio_eval_silence(info);
480 #endif
481 if ((tty = info->tty)) {
482 if (info->mcr & UART_MCR_RTS) {
483 c = TTY_FLIPBUF_SIZE - tty->flip.count;
484 if (c > 0) {
485 save_flags(flags);
486 cli();
487 r = isdn_readbchan(info->isdn_driver, info->isdn_channel,
488 tty->flip.char_buf_ptr,
489 tty->flip.flag_buf_ptr, c, 0);
490 /* CISCO AsyncPPP Hack */
491 if (!(info->emu.mdmreg[REG_CPPP] & BIT_CPPP))
492 memset(tty->flip.flag_buf_ptr, 0, r);
493 tty->flip.count += r;
494 tty->flip.flag_buf_ptr += r;
495 tty->flip.char_buf_ptr += r;
496 if (r)
497 queue_task(&tty->flip.tqueue, &tq_timer);
498 restore_flags(flags);
500 } else
501 r = 1;
502 } else
503 r = 1;
504 if (r) {
505 info->rcvsched = 0;
506 resched = 1;
507 } else
508 info->rcvsched = 1;
512 if (!resched)
513 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
517 isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
519 ulong flags;
520 int midx;
521 #ifdef CONFIG_ISDN_AUDIO
522 int ifmt;
523 #endif
524 modem_info *info;
526 if ((midx = dev->m_idx[i]) < 0) {
527 /* if midx is invalid, packet is not for tty */
528 return 0;
530 info = &dev->mdm.info[midx];
531 #ifdef CONFIG_ISDN_AUDIO
532 ifmt = 1;
534 if ((info->vonline) && (!info->emu.vpar[4]))
535 isdn_audio_calc_dtmf(info, skb->data, skb->len, ifmt);
536 if ((info->vonline & 1) && (info->emu.vpar[1]))
537 isdn_audio_calc_silence(info, skb->data, skb->len, ifmt);
538 #endif
539 if ((info->online < 2)
540 #ifdef CONFIG_ISDN_AUDIO
541 && (!(info->vonline & 1))
542 #endif
544 /* If Modem not listening, drop data */
545 kfree_skb(skb);
546 return 1;
548 if (info->emu.mdmreg[REG_T70] & BIT_T70) {
549 if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT) {
550 /* T.70 decoding: throw away the T.70 header (2 or 4 bytes) */
551 if (skb->data[0] == 3) /* pure data packet -> 4 byte headers */
552 skb_pull(skb, 4);
553 else
554 if (skb->data[0] == 1) /* keepalive packet -> 2 byte hdr */
555 skb_pull(skb, 2);
556 } else
557 /* T.70 decoding: Simply throw away the T.70 header (4 bytes) */
558 if ((skb->data[0] == 1) && ((skb->data[1] == 0) || (skb->data[1] == 1)))
559 skb_pull(skb, 4);
561 #ifdef CONFIG_ISDN_AUDIO
562 if (skb_headroom(skb) < sizeof(isdn_audio_skb)) {
563 printk(KERN_WARNING
564 "isdn_audio: insufficient skb_headroom, dropping\n");
565 kfree_skb(skb);
566 return 1;
568 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
569 ISDN_AUDIO_SKB_LOCK(skb) = 0;
570 if (info->vonline & 1) {
571 /* voice conversion/compression */
572 switch (info->emu.vpar[3]) {
573 case 2:
574 case 3:
575 case 4:
576 /* adpcm
577 * Since compressed data takes less
578 * space, we can overwrite the buffer.
580 skb_trim(skb, isdn_audio_xlaw2adpcm(info->adpcmr,
581 ifmt,
582 skb->data,
583 skb->data,
584 skb->len));
585 break;
586 case 5:
587 /* a-law */
588 if (!ifmt)
589 isdn_audio_ulaw2alaw(skb->data, skb->len);
590 break;
591 case 6:
592 /* u-law */
593 if (ifmt)
594 isdn_audio_alaw2ulaw(skb->data, skb->len);
595 break;
597 ISDN_AUDIO_SKB_DLECOUNT(skb) =
598 isdn_tty_countDLE(skb->data, skb->len);
600 #ifdef CONFIG_ISDN_TTY_FAX
601 else {
602 if (info->faxonline & 2) {
603 isdn_tty_fax_bitorder(info, skb);
604 ISDN_AUDIO_SKB_DLECOUNT(skb) =
605 isdn_tty_countDLE(skb->data, skb->len);
608 #endif
609 #endif
610 /* Try to deliver directly via tty-flip-buf if queue is empty */
611 save_flags(flags);
612 cli();
613 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
614 if (isdn_tty_try_read(info, skb)) {
615 restore_flags(flags);
616 return 1;
618 /* Direct deliver failed or queue wasn't empty.
619 * Queue up for later dequeueing via timer-irq.
621 __skb_queue_tail(&dev->drv[di]->rpqueue[channel], skb);
622 dev->drv[di]->rcvcount[channel] +=
623 (skb->len
624 #ifdef CONFIG_ISDN_AUDIO
625 + ISDN_AUDIO_SKB_DLECOUNT(skb)
626 #endif
628 restore_flags(flags);
629 /* Schedule dequeuing */
630 if ((dev->modempoll) && (info->rcvsched))
631 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
632 return 1;
635 void
636 isdn_tty_cleanup_xmit(modem_info * info)
638 struct sk_buff *skb;
639 unsigned long flags;
641 save_flags(flags);
642 cli();
643 if (skb_queue_len(&info->xmit_queue))
644 while ((skb = skb_dequeue(&info->xmit_queue)))
645 kfree_skb(skb);
646 #ifdef CONFIG_ISDN_AUDIO
647 if (skb_queue_len(&info->dtmf_queue))
648 while ((skb = skb_dequeue(&info->dtmf_queue)))
649 kfree_skb(skb);
650 #endif
651 restore_flags(flags);
654 static void
655 isdn_tty_tint(modem_info * info)
657 struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
658 int len,
659 slen;
661 if (!skb)
662 return;
663 len = skb->len;
664 if ((slen = isdn_writebuf_skb_stub(info->isdn_driver,
665 info->isdn_channel, 1, skb)) == len) {
666 struct tty_struct *tty = info->tty;
667 info->send_outstanding++;
668 info->msr &= ~UART_MSR_CTS;
669 info->lsr &= ~UART_LSR_TEMT;
670 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
671 tty->ldisc.write_wakeup)
672 (tty->ldisc.write_wakeup) (tty);
673 wake_up_interruptible(&tty->write_wait);
674 return;
676 if (slen < 0) {
677 /* Error: no channel, already shutdown, or wrong parameter */
678 dev_kfree_skb(skb);
679 return;
681 skb_queue_head(&info->xmit_queue, skb);
684 #ifdef CONFIG_ISDN_AUDIO
685 static int
686 isdn_tty_countDLE(unsigned char *buf, int len)
688 int count = 0;
690 while (len--)
691 if (*buf++ == DLE)
692 count++;
693 return count;
696 /* This routine is called from within isdn_tty_write() to perform
697 * DLE-decoding when sending audio-data.
699 static int
700 isdn_tty_handleDLEdown(modem_info * info, atemu * m, int len)
702 unsigned char *p = &info->xmit_buf[info->xmit_count];
703 int count = 0;
705 while (len > 0) {
706 if (m->lastDLE) {
707 m->lastDLE = 0;
708 switch (*p) {
709 case DLE:
710 /* Escape code */
711 if (len > 1)
712 memmove(p, p + 1, len - 1);
713 p--;
714 count++;
715 break;
716 case ETX:
717 /* End of data */
718 info->vonline |= 4;
719 return count;
720 case DC4:
721 /* Abort RX */
722 info->vonline &= ~1;
723 #ifdef ISDN_DEBUG_MODEM_VOICE
724 printk(KERN_DEBUG
725 "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
726 info->line);
727 #endif
728 isdn_tty_at_cout("\020\003", info);
729 if (!info->vonline) {
730 #ifdef ISDN_DEBUG_MODEM_VOICE
731 printk(KERN_DEBUG
732 "DLEdown: send VCON on ttyI%d\n",
733 info->line);
734 #endif
735 isdn_tty_at_cout("\r\nVCON\r\n", info);
737 /* Fall through */
738 case 'q':
739 case 's':
740 /* Silence */
741 if (len > 1)
742 memmove(p, p + 1, len - 1);
743 p--;
744 break;
746 } else {
747 if (*p == DLE)
748 m->lastDLE = 1;
749 else
750 count++;
752 p++;
753 len--;
755 if (len < 0) {
756 printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
757 return 0;
759 return count;
762 /* This routine is called from within isdn_tty_write() when receiving
763 * audio-data. It interrupts receiving, if an character other than
764 * ^S or ^Q is sent.
766 static int
767 isdn_tty_end_vrx(const char *buf, int c, int from_user)
769 char ch;
771 while (c--) {
772 if (from_user)
773 get_user(ch, buf);
774 else
775 ch = *buf;
776 if ((ch != 0x11) && (ch != 0x13))
777 return 1;
778 buf++;
780 return 0;
783 static int voice_cf[7] =
784 {0, 0, 4, 3, 2, 0, 0};
786 #endif /* CONFIG_ISDN_AUDIO */
788 /* isdn_tty_senddown() is called either directly from within isdn_tty_write()
789 * or via timer-interrupt from within isdn_tty_modem_xmit(). It pulls
790 * outgoing data from the tty's xmit-buffer, handles voice-decompression or
791 * T.70 if necessary, and finally queues it up for sending via isdn_tty_tint.
793 static void
794 isdn_tty_senddown(modem_info * info)
796 int buflen;
797 int skb_res;
798 #ifdef CONFIG_ISDN_AUDIO
799 int audio_len;
800 #endif
801 struct sk_buff *skb;
803 #ifdef CONFIG_ISDN_AUDIO
804 if (info->vonline & 4) {
805 info->vonline &= ~6;
806 if (!info->vonline) {
807 #ifdef ISDN_DEBUG_MODEM_VOICE
808 printk(KERN_DEBUG
809 "senddown: send VCON on ttyI%d\n",
810 info->line);
811 #endif
812 isdn_tty_at_cout("\r\nVCON\r\n", info);
815 #endif
816 if (!(buflen = info->xmit_count))
817 return;
818 if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
819 info->msr &= ~UART_MSR_CTS;
820 info->lsr &= ~UART_LSR_TEMT;
821 /* info->xmit_count is modified here and in isdn_tty_write().
822 * So we return here if isdn_tty_write() is in the
823 * critical section.
825 atomic_inc(&info->xmit_lock);
826 if (!(atomic_dec_and_test(&info->xmit_lock)))
827 return;
828 if (info->isdn_driver < 0) {
829 info->xmit_count = 0;
830 return;
832 skb_res = dev->drv[info->isdn_driver]->interface->hl_hdrlen + 4;
833 #ifdef CONFIG_ISDN_AUDIO
834 if (info->vonline & 2)
835 audio_len = buflen * voice_cf[info->emu.vpar[3]];
836 else
837 audio_len = 0;
838 skb = dev_alloc_skb(skb_res + buflen + audio_len);
839 #else
840 skb = dev_alloc_skb(skb_res + buflen);
841 #endif
842 if (!skb) {
843 printk(KERN_WARNING
844 "isdn_tty: Out of memory in ttyI%d senddown\n",
845 info->line);
846 return;
848 skb_reserve(skb, skb_res);
849 memcpy(skb_put(skb, buflen), info->xmit_buf, buflen);
850 info->xmit_count = 0;
851 #ifdef CONFIG_ISDN_AUDIO
852 if (info->vonline & 2) {
853 /* For now, ifmt is fixed to 1 (alaw), since this
854 * is used with ISDN everywhere in the world, except
855 * US, Canada and Japan.
856 * Later, when US-ISDN protocols are implemented,
857 * this setting will depend on the D-channel protocol.
859 int ifmt = 1;
861 /* voice conversion/decompression */
862 switch (info->emu.vpar[3]) {
863 case 2:
864 case 3:
865 case 4:
866 /* adpcm, compatible to ZyXel 1496 modem
867 * with ROM revision 6.01
869 audio_len = isdn_audio_adpcm2xlaw(info->adpcms,
870 ifmt,
871 skb->data,
872 skb_put(skb, audio_len),
873 buflen);
874 skb_pull(skb, buflen);
875 skb_trim(skb, audio_len);
876 break;
877 case 5:
878 /* a-law */
879 if (!ifmt)
880 isdn_audio_alaw2ulaw(skb->data,
881 buflen);
882 break;
883 case 6:
884 /* u-law */
885 if (ifmt)
886 isdn_audio_ulaw2alaw(skb->data,
887 buflen);
888 break;
891 #endif /* CONFIG_ISDN_AUDIO */
892 if (info->emu.mdmreg[REG_T70] & BIT_T70) {
893 /* Add T.70 simplified header */
894 if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT)
895 memcpy(skb_push(skb, 2), "\1\0", 2);
896 else
897 memcpy(skb_push(skb, 4), "\1\0\1\0", 4);
899 skb_queue_tail(&info->xmit_queue, skb);
902 /************************************************************
904 * Modem-functions
906 * mostly "stolen" from original Linux-serial.c and friends.
908 ************************************************************/
910 /* The next routine is called once from within timer-interrupt
911 * triggered within isdn_tty_modem_ncarrier(). It calls
912 * isdn_tty_modem_result() to stuff a "NO CARRIER" Message
913 * into the tty's flip-buffer.
915 static void
916 isdn_tty_modem_do_ncarrier(unsigned long data)
918 modem_info *info = (modem_info *) data;
919 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
922 /* Next routine is called, whenever the DTR-signal is raised.
923 * It checks the ncarrier-flag, and triggers the above routine
924 * when necessary. The ncarrier-flag is set, whenever DTR goes
925 * low.
927 static void
928 isdn_tty_modem_ncarrier(modem_info * info)
930 if (info->ncarrier) {
931 info->nc_timer.expires = jiffies + HZ;
932 info->nc_timer.function = isdn_tty_modem_do_ncarrier;
933 info->nc_timer.data = (unsigned long) info;
934 add_timer(&info->nc_timer);
939 * return the usage calculated by si and layer 2 protocol
942 isdn_calc_usage(int si, int l2)
944 int usg = ISDN_USAGE_MODEM;
946 #ifdef CONFIG_ISDN_AUDIO
947 if (si == 1) {
948 switch(l2) {
949 case ISDN_PROTO_L2_MODEM:
950 usg = ISDN_USAGE_MODEM;
951 break;
952 #ifdef CONFIG_ISDN_TTY_FAX
953 case ISDN_PROTO_L2_FAX:
954 usg = ISDN_USAGE_FAX;
955 break;
956 #endif
957 case ISDN_PROTO_L2_TRANS:
958 default:
959 usg = ISDN_USAGE_VOICE;
960 break;
963 #endif
964 return(usg);
967 /* isdn_tty_dial() performs dialing of a tty an the necessary
968 * setup of the lower levels before that.
970 static void
971 isdn_tty_dial(char *n, modem_info * info, atemu * m)
973 int usg = ISDN_USAGE_MODEM;
974 int si = 7;
975 int l2 = m->mdmreg[REG_L2PROT];
976 isdn_ctrl cmd;
977 ulong flags;
978 int i;
979 int j;
981 for (j = 7; j >= 0; j--)
982 if (m->mdmreg[REG_SI1] & (1 << j)) {
983 si = bit2si[j];
984 break;
986 usg = isdn_calc_usage(si, l2);
987 #ifdef CONFIG_ISDN_AUDIO
988 if ((si == 1) &&
989 (l2 != ISDN_PROTO_L2_MODEM)
990 #ifdef CONFIG_ISDN_TTY_FAX
991 && (l2 != ISDN_PROTO_L2_FAX)
992 #endif
994 l2 = ISDN_PROTO_L2_TRANS;
995 usg = ISDN_USAGE_VOICE;
997 #endif
998 m->mdmreg[REG_SI1I] = si2bit[si];
999 save_flags(flags);
1000 cli();
1001 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
1002 if (i < 0) {
1003 restore_flags(flags);
1004 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
1005 } else {
1006 info->isdn_driver = dev->drvmap[i];
1007 info->isdn_channel = dev->chanmap[i];
1008 info->drv_index = i;
1009 dev->m_idx[i] = info->line;
1010 dev->usage[i] |= ISDN_USAGE_OUTGOING;
1011 info->last_dir = 1;
1012 strcpy(info->last_num, n);
1013 isdn_info_update();
1014 restore_flags(flags);
1015 cmd.driver = info->isdn_driver;
1016 cmd.arg = info->isdn_channel;
1017 cmd.command = ISDN_CMD_CLREAZ;
1018 isdn_command(&cmd);
1019 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
1020 cmd.driver = info->isdn_driver;
1021 cmd.command = ISDN_CMD_SETEAZ;
1022 isdn_command(&cmd);
1023 cmd.driver = info->isdn_driver;
1024 cmd.command = ISDN_CMD_SETL2;
1025 info->last_l2 = l2;
1026 cmd.arg = info->isdn_channel + (l2 << 8);
1027 isdn_command(&cmd);
1028 cmd.driver = info->isdn_driver;
1029 cmd.command = ISDN_CMD_SETL3;
1030 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
1031 #ifdef CONFIG_ISDN_TTY_FAX
1032 if (l2 == ISDN_PROTO_L2_FAX) {
1033 cmd.parm.fax = info->fax;
1034 info->fax->direction = ISDN_TTY_FAX_CONN_OUT;
1036 #endif
1037 isdn_command(&cmd);
1038 cmd.driver = info->isdn_driver;
1039 cmd.arg = info->isdn_channel;
1040 sprintf(cmd.parm.setup.phone, "%s", n);
1041 sprintf(cmd.parm.setup.eazmsn, "%s",
1042 isdn_map_eaz2msn(m->msn, info->isdn_driver));
1043 cmd.parm.setup.si1 = si;
1044 cmd.parm.setup.si2 = m->mdmreg[REG_SI2];
1045 cmd.command = ISDN_CMD_DIAL;
1046 info->dialing = 1;
1047 info->emu.carrierwait = 0;
1048 strcpy(dev->num[i], n);
1049 isdn_info_update();
1050 isdn_command(&cmd);
1051 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
1055 /* isdn_tty_hangup() disassociates a tty from the real
1056 * ISDN-line (hangup). The usage-status is cleared
1057 * and some cleanup is done also.
1059 void
1060 isdn_tty_modem_hup(modem_info * info, int local)
1062 isdn_ctrl cmd;
1064 if (!info)
1065 return;
1066 #ifdef ISDN_DEBUG_MODEM_HUP
1067 printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
1068 #endif
1069 info->rcvsched = 0;
1070 isdn_tty_flush_buffer(info->tty);
1071 if (info->online) {
1072 info->last_lhup = local;
1073 info->online = 0;
1074 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
1076 #ifdef CONFIG_ISDN_AUDIO
1077 info->vonline = 0;
1078 #ifdef CONFIG_ISDN_TTY_FAX
1079 info->faxonline = 0;
1080 info->fax->phase = ISDN_FAX_PHASE_IDLE;
1081 #endif
1082 info->emu.vpar[4] = 0;
1083 info->emu.vpar[5] = 8;
1084 if (info->dtmf_state) {
1085 kfree(info->dtmf_state);
1086 info->dtmf_state = NULL;
1088 if (info->silence_state) {
1089 kfree(info->silence_state);
1090 info->silence_state = NULL;
1092 if (info->adpcms) {
1093 kfree(info->adpcms);
1094 info->adpcms = NULL;
1096 if (info->adpcmr) {
1097 kfree(info->adpcmr);
1098 info->adpcmr = NULL;
1100 #endif
1101 if ((info->msr & UART_MSR_RI) &&
1102 (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
1103 isdn_tty_modem_result(RESULT_RUNG, info);
1104 info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
1105 info->lsr |= UART_LSR_TEMT;
1106 if (info->isdn_driver >= 0) {
1107 if (local) {
1108 cmd.driver = info->isdn_driver;
1109 cmd.command = ISDN_CMD_HANGUP;
1110 cmd.arg = info->isdn_channel;
1111 isdn_command(&cmd);
1113 isdn_all_eaz(info->isdn_driver, info->isdn_channel);
1114 info->emu.mdmreg[REG_RINGCNT] = 0;
1115 isdn_free_channel(info->isdn_driver, info->isdn_channel, 0);
1117 info->isdn_driver = -1;
1118 info->isdn_channel = -1;
1119 if (info->drv_index >= 0) {
1120 dev->m_idx[info->drv_index] = -1;
1121 info->drv_index = -1;
1126 * Begin of a CAPI like interface, currently used only for
1127 * supplementary service (CAPI 2.0 part III)
1129 #include "avmb1/capicmd.h" /* this should be moved in a common place */
1132 isdn_tty_capi_facility(capi_msg *cm) {
1133 return(-1); /* dummy */
1136 /* isdn_tty_suspend() tries to suspend the current tty connection
1138 static void
1139 isdn_tty_suspend(char *id, modem_info * info, atemu * m)
1141 isdn_ctrl cmd;
1143 int l;
1145 if (!info)
1146 return;
1148 #ifdef ISDN_DEBUG_MODEM_SERVICES
1149 printk(KERN_DEBUG "Msusp ttyI%d\n", info->line);
1150 #endif
1151 l = strlen(id);
1152 if ((info->isdn_driver >= 0)) {
1153 cmd.parm.cmsg.Length = l+18;
1154 cmd.parm.cmsg.Command = CAPI_FACILITY;
1155 cmd.parm.cmsg.Subcommand = CAPI_REQ;
1156 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
1157 cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
1158 cmd.parm.cmsg.para[1] = 0;
1159 cmd.parm.cmsg.para[2] = l + 3;
1160 cmd.parm.cmsg.para[3] = 4; /* 16 bit 0x0004 Suspend */
1161 cmd.parm.cmsg.para[4] = 0;
1162 cmd.parm.cmsg.para[5] = l;
1163 strncpy(&cmd.parm.cmsg.para[6], id, l);
1164 cmd.command = CAPI_PUT_MESSAGE;
1165 cmd.driver = info->isdn_driver;
1166 cmd.arg = info->isdn_channel;
1167 isdn_command(&cmd);
1171 /* isdn_tty_resume() tries to resume a suspended call
1172 * setup of the lower levels before that. unfortunatly here is no
1173 * checking for compatibility of used protocols implemented by Q931
1174 * It does the same things like isdn_tty_dial, the last command
1175 * is different, may be we can merge it.
1178 static void
1179 isdn_tty_resume(char *id, modem_info * info, atemu * m)
1181 int usg = ISDN_USAGE_MODEM;
1182 int si = 7;
1183 int l2 = m->mdmreg[REG_L2PROT];
1184 isdn_ctrl cmd;
1185 ulong flags;
1186 int i;
1187 int j;
1188 int l;
1190 l = strlen(id);
1191 for (j = 7; j >= 0; j--)
1192 if (m->mdmreg[REG_SI1] & (1 << j)) {
1193 si = bit2si[j];
1194 break;
1196 usg = isdn_calc_usage(si, l2);
1197 #ifdef CONFIG_ISDN_AUDIO
1198 if ((si == 1) &&
1199 (l2 != ISDN_PROTO_L2_MODEM)
1200 #ifdef CONFIG_ISDN_TTY_FAX
1201 && (l2 != ISDN_PROTO_L2_FAX)
1202 #endif
1204 l2 = ISDN_PROTO_L2_TRANS;
1205 usg = ISDN_USAGE_VOICE;
1207 #endif
1208 m->mdmreg[REG_SI1I] = si2bit[si];
1209 save_flags(flags);
1210 cli();
1211 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
1212 if (i < 0) {
1213 restore_flags(flags);
1214 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
1215 } else {
1216 info->isdn_driver = dev->drvmap[i];
1217 info->isdn_channel = dev->chanmap[i];
1218 info->drv_index = i;
1219 dev->m_idx[i] = info->line;
1220 dev->usage[i] |= ISDN_USAGE_OUTGOING;
1221 info->last_dir = 1;
1222 // strcpy(info->last_num, n);
1223 isdn_info_update();
1224 restore_flags(flags);
1225 cmd.driver = info->isdn_driver;
1226 cmd.arg = info->isdn_channel;
1227 cmd.command = ISDN_CMD_CLREAZ;
1228 isdn_command(&cmd);
1229 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
1230 cmd.driver = info->isdn_driver;
1231 cmd.command = ISDN_CMD_SETEAZ;
1232 isdn_command(&cmd);
1233 cmd.driver = info->isdn_driver;
1234 cmd.command = ISDN_CMD_SETL2;
1235 info->last_l2 = l2;
1236 cmd.arg = info->isdn_channel + (l2 << 8);
1237 isdn_command(&cmd);
1238 cmd.driver = info->isdn_driver;
1239 cmd.command = ISDN_CMD_SETL3;
1240 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
1241 isdn_command(&cmd);
1242 cmd.driver = info->isdn_driver;
1243 cmd.arg = info->isdn_channel;
1244 cmd.parm.cmsg.Length = l+18;
1245 cmd.parm.cmsg.Command = CAPI_FACILITY;
1246 cmd.parm.cmsg.Subcommand = CAPI_REQ;
1247 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
1248 cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
1249 cmd.parm.cmsg.para[1] = 0;
1250 cmd.parm.cmsg.para[2] = l+3;
1251 cmd.parm.cmsg.para[3] = 5; /* 16 bit 0x0005 Resume */
1252 cmd.parm.cmsg.para[4] = 0;
1253 cmd.parm.cmsg.para[5] = l;
1254 strncpy(&cmd.parm.cmsg.para[6], id, l);
1255 cmd.command =CAPI_PUT_MESSAGE;
1256 info->dialing = 1;
1257 // strcpy(dev->num[i], n);
1258 isdn_info_update();
1259 isdn_command(&cmd);
1260 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
1264 /* isdn_tty_send_msg() sends a message to a HL driver
1265 * This is used for hybrid modem cards to send AT commands to it
1268 static void
1269 isdn_tty_send_msg(modem_info * info, atemu * m, char *msg)
1271 int usg = ISDN_USAGE_MODEM;
1272 int si = 7;
1273 int l2 = m->mdmreg[REG_L2PROT];
1274 isdn_ctrl cmd;
1275 ulong flags;
1276 int i;
1277 int j;
1278 int l;
1280 l = strlen(msg);
1281 if (!l) {
1282 isdn_tty_modem_result(RESULT_ERROR, info);
1283 return;
1285 for (j = 7; j >= 0; j--)
1286 if (m->mdmreg[REG_SI1] & (1 << j)) {
1287 si = bit2si[j];
1288 break;
1290 usg = isdn_calc_usage(si, l2);
1291 #ifdef CONFIG_ISDN_AUDIO
1292 if ((si == 1) &&
1293 (l2 != ISDN_PROTO_L2_MODEM)
1294 #ifdef CONFIG_ISDN_TTY_FAX
1295 && (l2 != ISDN_PROTO_L2_FAX)
1296 #endif
1298 l2 = ISDN_PROTO_L2_TRANS;
1299 usg = ISDN_USAGE_VOICE;
1301 #endif
1302 m->mdmreg[REG_SI1I] = si2bit[si];
1303 save_flags(flags);
1304 cli();
1305 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
1306 if (i < 0) {
1307 restore_flags(flags);
1308 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
1309 } else {
1310 info->isdn_driver = dev->drvmap[i];
1311 info->isdn_channel = dev->chanmap[i];
1312 info->drv_index = i;
1313 dev->m_idx[i] = info->line;
1314 dev->usage[i] |= ISDN_USAGE_OUTGOING;
1315 info->last_dir = 1;
1316 isdn_info_update();
1317 restore_flags(flags);
1318 cmd.driver = info->isdn_driver;
1319 cmd.arg = info->isdn_channel;
1320 cmd.command = ISDN_CMD_CLREAZ;
1321 isdn_command(&cmd);
1322 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
1323 cmd.driver = info->isdn_driver;
1324 cmd.command = ISDN_CMD_SETEAZ;
1325 isdn_command(&cmd);
1326 cmd.driver = info->isdn_driver;
1327 cmd.command = ISDN_CMD_SETL2;
1328 info->last_l2 = l2;
1329 cmd.arg = info->isdn_channel + (l2 << 8);
1330 isdn_command(&cmd);
1331 cmd.driver = info->isdn_driver;
1332 cmd.command = ISDN_CMD_SETL3;
1333 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
1334 isdn_command(&cmd);
1335 cmd.driver = info->isdn_driver;
1336 cmd.arg = info->isdn_channel;
1337 cmd.parm.cmsg.Length = l+14;
1338 cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
1339 cmd.parm.cmsg.Subcommand = CAPI_REQ;
1340 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
1341 cmd.parm.cmsg.para[0] = l+1;
1342 strncpy(&cmd.parm.cmsg.para[1], msg, l);
1343 cmd.parm.cmsg.para[l+1] = 0xd;
1344 cmd.command =CAPI_PUT_MESSAGE;
1345 /* info->dialing = 1;
1346 strcpy(dev->num[i], n);
1347 isdn_info_update();
1349 isdn_command(&cmd);
1353 static inline int
1354 isdn_tty_paranoia_check(modem_info * info, kdev_t device, const char *routine)
1356 #ifdef MODEM_PARANOIA_CHECK
1357 if (!info) {
1358 printk(KERN_WARNING "isdn_tty: null info_struct for (%d, %d) in %s\n",
1359 MAJOR(device), MINOR(device), routine);
1360 return 1;
1362 if (info->magic != ISDN_ASYNC_MAGIC) {
1363 printk(KERN_WARNING "isdn_tty: bad magic for modem struct (%d, %d) in %s\n",
1364 MAJOR(device), MINOR(device), routine);
1365 return 1;
1367 #endif
1368 return 0;
1372 * This routine is called to set the UART divisor registers to match
1373 * the specified baud rate for a serial port.
1375 static void
1376 isdn_tty_change_speed(modem_info * info)
1378 uint cflag,
1379 cval,
1380 fcr,
1381 quot;
1382 int i;
1384 if (!info->tty || !info->tty->termios)
1385 return;
1386 cflag = info->tty->termios->c_cflag;
1388 quot = i = cflag & CBAUD;
1389 if (i & CBAUDEX) {
1390 i &= ~CBAUDEX;
1391 if (i < 1 || i > 2)
1392 info->tty->termios->c_cflag &= ~CBAUDEX;
1393 else
1394 i += 15;
1396 if (quot) {
1397 info->mcr |= UART_MCR_DTR;
1398 isdn_tty_modem_ncarrier(info);
1399 } else {
1400 info->mcr &= ~UART_MCR_DTR;
1401 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1402 #ifdef ISDN_DEBUG_MODEM_HUP
1403 printk(KERN_DEBUG "Mhup in changespeed\n");
1404 #endif
1405 if (info->online)
1406 info->ncarrier = 1;
1407 isdn_tty_modem_reset_regs(info, 0);
1408 isdn_tty_modem_hup(info, 1);
1410 return;
1412 /* byte size and parity */
1413 cval = cflag & (CSIZE | CSTOPB);
1414 cval >>= 4;
1415 if (cflag & PARENB)
1416 cval |= UART_LCR_PARITY;
1417 if (!(cflag & PARODD))
1418 cval |= UART_LCR_EPAR;
1419 fcr = 0;
1421 /* CTS flow control flag and modem status interrupts */
1422 if (cflag & CRTSCTS) {
1423 info->flags |= ISDN_ASYNC_CTS_FLOW;
1424 } else
1425 info->flags &= ~ISDN_ASYNC_CTS_FLOW;
1426 if (cflag & CLOCAL)
1427 info->flags &= ~ISDN_ASYNC_CHECK_CD;
1428 else {
1429 info->flags |= ISDN_ASYNC_CHECK_CD;
1433 static int
1434 isdn_tty_startup(modem_info * info)
1436 ulong flags;
1438 if (info->flags & ISDN_ASYNC_INITIALIZED)
1439 return 0;
1440 save_flags(flags);
1441 cli();
1442 isdn_MOD_INC_USE_COUNT();
1443 #ifdef ISDN_DEBUG_MODEM_OPEN
1444 printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
1445 #endif
1447 * Now, initialize the UART
1449 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
1450 if (info->tty)
1451 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1453 * and set the speed of the serial port
1455 isdn_tty_change_speed(info);
1457 info->flags |= ISDN_ASYNC_INITIALIZED;
1458 info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
1459 info->send_outstanding = 0;
1460 restore_flags(flags);
1461 return 0;
1465 * This routine will shutdown a serial port; interrupts are disabled, and
1466 * DTR is dropped if the hangup on close termio flag is on.
1468 static void
1469 isdn_tty_shutdown(modem_info * info)
1471 ulong flags;
1473 if (!(info->flags & ISDN_ASYNC_INITIALIZED))
1474 return;
1475 #ifdef ISDN_DEBUG_MODEM_OPEN
1476 printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
1477 #endif
1478 save_flags(flags);
1479 cli(); /* Disable interrupts */
1480 isdn_MOD_DEC_USE_COUNT();
1481 info->msr &= ~UART_MSR_RI;
1482 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1483 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
1484 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1485 isdn_tty_modem_reset_regs(info, 0);
1486 #ifdef ISDN_DEBUG_MODEM_HUP
1487 printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
1488 #endif
1489 isdn_tty_modem_hup(info, 1);
1492 if (info->tty)
1493 set_bit(TTY_IO_ERROR, &info->tty->flags);
1495 info->flags &= ~ISDN_ASYNC_INITIALIZED;
1496 restore_flags(flags);
1499 /* isdn_tty_write() is the main send-routine. It is called from the upper
1500 * levels within the kernel to perform sending data. Depending on the
1501 * online-flag it either directs output to the at-command-interpreter or
1502 * to the lower level. Additional tasks done here:
1503 * - If online, check for escape-sequence (+++)
1504 * - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
1505 * - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
1506 * - If dialing, abort dial.
1508 static int
1509 isdn_tty_write(struct tty_struct *tty, int from_user, const u_char * buf, int count)
1511 int c;
1512 int total = 0;
1513 modem_info *info = (modem_info *) tty->driver_data;
1515 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_write"))
1516 return 0;
1517 if (!tty)
1518 return 0;
1519 if (from_user)
1520 down(&info->write_sem);
1521 /* See isdn_tty_senddown() */
1522 atomic_inc(&info->xmit_lock);
1523 while (1) {
1524 c = MIN(count, info->xmit_size - info->xmit_count);
1525 if (info->isdn_driver >= 0)
1526 c = MIN(c, dev->drv[info->isdn_driver]->maxbufsize);
1527 if (c <= 0)
1528 break;
1529 if ((info->online > 1)
1530 #ifdef CONFIG_ISDN_AUDIO
1531 || (info->vonline & 3)
1532 #endif
1534 atemu *m = &info->emu;
1536 #ifdef CONFIG_ISDN_AUDIO
1537 if (!info->vonline)
1538 #endif
1539 isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
1540 &(m->pluscount),
1541 &(m->lastplus),
1542 from_user);
1543 if (from_user)
1544 copy_from_user(&(info->xmit_buf[info->xmit_count]), buf, c);
1545 else
1546 memcpy(&(info->xmit_buf[info->xmit_count]), buf, c);
1547 #ifdef CONFIG_ISDN_AUDIO
1548 if (info->vonline) {
1549 int cc = isdn_tty_handleDLEdown(info, m, c);
1550 if (info->vonline & 2) {
1551 if (!cc) {
1552 /* If DLE decoding results in zero-transmit, but
1553 * c originally was non-zero, do a wakeup.
1555 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1556 tty->ldisc.write_wakeup)
1557 (tty->ldisc.write_wakeup) (tty);
1558 wake_up_interruptible(&tty->write_wait);
1559 info->msr |= UART_MSR_CTS;
1560 info->lsr |= UART_LSR_TEMT;
1562 info->xmit_count += cc;
1564 if ((info->vonline & 3) == 1) {
1565 /* Do NOT handle Ctrl-Q or Ctrl-S
1566 * when in full-duplex audio mode.
1568 if (isdn_tty_end_vrx(buf, c, from_user)) {
1569 info->vonline &= ~1;
1570 #ifdef ISDN_DEBUG_MODEM_VOICE
1571 printk(KERN_DEBUG
1572 "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
1573 info->line);
1574 #endif
1575 isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
1578 } else
1579 if (TTY_IS_FCLASS1(info)) {
1580 int cc = isdn_tty_handleDLEdown(info, m, c);
1582 if (info->vonline & 4) { /* ETX seen */
1583 isdn_ctrl c;
1585 c.command = ISDN_CMD_FAXCMD;
1586 c.driver = info->isdn_driver;
1587 c.arg = info->isdn_channel;
1588 c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
1589 c.parm.aux.subcmd = ETX;
1590 isdn_command(&c);
1592 info->vonline = 0;
1593 printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc,c);
1594 info->xmit_count += cc;
1595 } else
1596 #endif
1597 info->xmit_count += c;
1598 } else {
1599 info->msr |= UART_MSR_CTS;
1600 info->lsr |= UART_LSR_TEMT;
1601 if (info->dialing) {
1602 info->dialing = 0;
1603 #ifdef ISDN_DEBUG_MODEM_HUP
1604 printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
1605 #endif
1606 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
1607 isdn_tty_modem_hup(info, 1);
1608 } else
1609 c = isdn_tty_edit_at(buf, c, info, from_user);
1611 buf += c;
1612 count -= c;
1613 total += c;
1615 if ((info->xmit_count) || (skb_queue_len(&info->xmit_queue)))
1616 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1617 atomic_dec(&info->xmit_lock);
1618 if (from_user)
1619 up(&info->write_sem);
1620 return total;
1623 static int
1624 isdn_tty_write_room(struct tty_struct *tty)
1626 modem_info *info = (modem_info *) tty->driver_data;
1627 int ret;
1629 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_write_room"))
1630 return 0;
1631 if (!info->online)
1632 return info->xmit_size;
1633 ret = info->xmit_size - info->xmit_count;
1634 return (ret < 0) ? 0 : ret;
1637 static int
1638 isdn_tty_chars_in_buffer(struct tty_struct *tty)
1640 modem_info *info = (modem_info *) tty->driver_data;
1642 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_chars_in_buffer"))
1643 return 0;
1644 if (!info->online)
1645 return 0;
1646 return (info->xmit_count);
1649 static void
1650 isdn_tty_flush_buffer(struct tty_struct *tty)
1652 modem_info *info;
1653 unsigned long flags;
1655 save_flags(flags);
1656 cli();
1657 if (!tty) {
1658 restore_flags(flags);
1659 return;
1661 info = (modem_info *) tty->driver_data;
1662 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_flush_buffer")) {
1663 restore_flags(flags);
1664 return;
1666 isdn_tty_cleanup_xmit(info);
1667 info->xmit_count = 0;
1668 restore_flags(flags);
1669 wake_up_interruptible(&tty->write_wait);
1670 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1671 tty->ldisc.write_wakeup)
1672 (tty->ldisc.write_wakeup) (tty);
1675 static void
1676 isdn_tty_flush_chars(struct tty_struct *tty)
1678 modem_info *info = (modem_info *) tty->driver_data;
1680 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_flush_chars"))
1681 return;
1682 if ((info->xmit_count) || (skb_queue_len(&info->xmit_queue)))
1683 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1687 * ------------------------------------------------------------
1688 * isdn_tty_throttle()
1690 * This routine is called by the upper-layer tty layer to signal that
1691 * incoming characters should be throttled.
1692 * ------------------------------------------------------------
1694 static void
1695 isdn_tty_throttle(struct tty_struct *tty)
1697 modem_info *info = (modem_info *) tty->driver_data;
1699 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_throttle"))
1700 return;
1701 if (I_IXOFF(tty))
1702 info->x_char = STOP_CHAR(tty);
1703 info->mcr &= ~UART_MCR_RTS;
1706 static void
1707 isdn_tty_unthrottle(struct tty_struct *tty)
1709 modem_info *info = (modem_info *) tty->driver_data;
1711 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_unthrottle"))
1712 return;
1713 if (I_IXOFF(tty)) {
1714 if (info->x_char)
1715 info->x_char = 0;
1716 else
1717 info->x_char = START_CHAR(tty);
1719 info->mcr |= UART_MCR_RTS;
1723 * ------------------------------------------------------------
1724 * isdn_tty_ioctl() and friends
1725 * ------------------------------------------------------------
1729 * isdn_tty_get_lsr_info - get line status register info
1731 * Purpose: Let user call ioctl() to get info when the UART physically
1732 * is emptied. On bus types like RS485, the transmitter must
1733 * release the bus after transmitting. This must be done when
1734 * the transmit shift register is empty, not be done when the
1735 * transmit holding register is empty. This functionality
1736 * allows RS485 driver to be written in user space.
1738 static int
1739 isdn_tty_get_lsr_info(modem_info * info, uint * value)
1741 u_char status;
1742 uint result;
1743 ulong flags;
1745 save_flags(flags);
1746 cli();
1747 status = info->lsr;
1748 restore_flags(flags);
1749 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1750 put_user(result, (uint *) value);
1751 return 0;
1755 static int
1756 isdn_tty_get_modem_info(modem_info * info, uint * value)
1758 u_char control,
1759 status;
1760 uint result;
1761 ulong flags;
1763 control = info->mcr;
1764 save_flags(flags);
1765 cli();
1766 status = info->msr;
1767 restore_flags(flags);
1768 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1769 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1770 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1771 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1772 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1773 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1774 put_user(result, (uint *) value);
1775 return 0;
1778 static int
1779 isdn_tty_set_modem_info(modem_info * info, uint cmd, uint * value)
1781 uint arg;
1782 int pre_dtr;
1784 get_user(arg, (uint *) value);
1785 switch (cmd) {
1786 case TIOCMBIS:
1787 #ifdef ISDN_DEBUG_MODEM_IOCTL
1788 printk(KERN_DEBUG "ttyI%d ioctl TIOCMBIS\n", info->line);
1789 #endif
1790 if (arg & TIOCM_RTS) {
1791 info->mcr |= UART_MCR_RTS;
1793 if (arg & TIOCM_DTR) {
1794 info->mcr |= UART_MCR_DTR;
1795 isdn_tty_modem_ncarrier(info);
1797 break;
1798 case TIOCMBIC:
1799 #ifdef ISDN_DEBUG_MODEM_IOCTL
1800 printk(KERN_DEBUG "ttyI%d ioctl TIOCMBIC\n", info->line);
1801 #endif
1802 if (arg & TIOCM_RTS) {
1803 info->mcr &= ~UART_MCR_RTS;
1805 if (arg & TIOCM_DTR) {
1806 info->mcr &= ~UART_MCR_DTR;
1807 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1808 isdn_tty_modem_reset_regs(info, 0);
1809 #ifdef ISDN_DEBUG_MODEM_HUP
1810 printk(KERN_DEBUG "Mhup in TIOCMBIC\n");
1811 #endif
1812 if (info->online)
1813 info->ncarrier = 1;
1814 isdn_tty_modem_hup(info, 1);
1817 break;
1818 case TIOCMSET:
1819 #ifdef ISDN_DEBUG_MODEM_IOCTL
1820 printk(KERN_DEBUG "ttyI%d ioctl TIOCMSET\n", info->line);
1821 #endif
1822 pre_dtr = (info->mcr & UART_MCR_DTR);
1823 info->mcr = ((info->mcr & ~(UART_MCR_RTS | UART_MCR_DTR))
1824 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1825 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1826 if (pre_dtr |= (info->mcr & UART_MCR_DTR)) {
1827 if (!(info->mcr & UART_MCR_DTR)) {
1828 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1829 isdn_tty_modem_reset_regs(info, 0);
1830 #ifdef ISDN_DEBUG_MODEM_HUP
1831 printk(KERN_DEBUG "Mhup in TIOCMSET\n");
1832 #endif
1833 if (info->online)
1834 info->ncarrier = 1;
1835 isdn_tty_modem_hup(info, 1);
1837 } else
1838 isdn_tty_modem_ncarrier(info);
1840 break;
1841 default:
1842 return -EINVAL;
1844 return 0;
1847 static int
1848 isdn_tty_ioctl(struct tty_struct *tty, struct file *file,
1849 uint cmd, ulong arg)
1851 modem_info *info = (modem_info *) tty->driver_data;
1852 int error;
1853 int retval;
1855 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_ioctl"))
1856 return -ENODEV;
1857 if (tty->flags & (1 << TTY_IO_ERROR))
1858 return -EIO;
1859 switch (cmd) {
1860 case TCSBRK: /* SVID version: non-zero arg --> no break */
1861 #ifdef ISDN_DEBUG_MODEM_IOCTL
1862 printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
1863 #endif
1864 retval = tty_check_change(tty);
1865 if (retval)
1866 return retval;
1867 tty_wait_until_sent(tty, 0);
1868 return 0;
1869 case TCSBRKP: /* support for POSIX tcsendbreak() */
1870 #ifdef ISDN_DEBUG_MODEM_IOCTL
1871 printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
1872 #endif
1873 retval = tty_check_change(tty);
1874 if (retval)
1875 return retval;
1876 tty_wait_until_sent(tty, 0);
1877 return 0;
1878 case TIOCGSOFTCAR:
1879 #ifdef ISDN_DEBUG_MODEM_IOCTL
1880 printk(KERN_DEBUG "ttyI%d ioctl TIOCGSOFTCAR\n", info->line);
1881 #endif
1882 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long));
1883 if (error)
1884 return error;
1885 put_user(C_CLOCAL(tty) ? 1 : 0, (ulong *) arg);
1886 return 0;
1887 case TIOCSSOFTCAR:
1888 #ifdef ISDN_DEBUG_MODEM_IOCTL
1889 printk(KERN_DEBUG "ttyI%d ioctl TIOCSSOFTCAR\n", info->line);
1890 #endif
1891 error = verify_area(VERIFY_READ, (void *) arg, sizeof(long));
1892 if (error)
1893 return error;
1894 get_user(arg, (ulong *) arg);
1895 tty->termios->c_cflag =
1896 ((tty->termios->c_cflag & ~CLOCAL) |
1897 (arg ? CLOCAL : 0));
1898 return 0;
1899 case TIOCMGET:
1900 #ifdef ISDN_DEBUG_MODEM_IOCTL
1901 printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
1902 #endif
1903 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(uint));
1904 if (error)
1905 return error;
1906 return isdn_tty_get_modem_info(info, (uint *) arg);
1907 case TIOCMBIS:
1908 case TIOCMBIC:
1909 case TIOCMSET:
1910 error = verify_area(VERIFY_READ, (void *) arg, sizeof(uint));
1911 if (error)
1912 return error;
1913 return isdn_tty_set_modem_info(info, cmd, (uint *) arg);
1914 case TIOCSERGETLSR: /* Get line status register */
1915 #ifdef ISDN_DEBUG_MODEM_IOCTL
1916 printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
1917 #endif
1918 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(uint));
1919 if (error)
1920 return error;
1921 else
1922 return isdn_tty_get_lsr_info(info, (uint *) arg);
1923 default:
1924 #ifdef ISDN_DEBUG_MODEM_IOCTL
1925 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
1926 #endif
1927 return -ENOIOCTLCMD;
1929 return 0;
1932 static void
1933 isdn_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
1935 modem_info *info = (modem_info *) tty->driver_data;
1937 if (!old_termios)
1938 isdn_tty_change_speed(info);
1939 else {
1940 if (tty->termios->c_cflag == old_termios->c_cflag)
1941 return;
1942 isdn_tty_change_speed(info);
1943 if ((old_termios->c_cflag & CRTSCTS) &&
1944 !(tty->termios->c_cflag & CRTSCTS)) {
1945 tty->hw_stopped = 0;
1951 * ------------------------------------------------------------
1952 * isdn_tty_open() and friends
1953 * ------------------------------------------------------------
1955 static int
1956 isdn_tty_block_til_ready(struct tty_struct *tty, struct file *filp, modem_info * info)
1958 DECLARE_WAITQUEUE(wait, NULL);
1959 int do_clocal = 0;
1960 unsigned long flags;
1961 int retval;
1964 * If the device is in the middle of being closed, then block
1965 * until it's done, and then try again.
1967 if (tty_hung_up_p(filp) ||
1968 (info->flags & ISDN_ASYNC_CLOSING)) {
1969 if (info->flags & ISDN_ASYNC_CLOSING)
1970 interruptible_sleep_on(&info->close_wait);
1971 #ifdef MODEM_DO_RESTART
1972 if (info->flags & ISDN_ASYNC_HUP_NOTIFY)
1973 return -EAGAIN;
1974 else
1975 return -ERESTARTSYS;
1976 #else
1977 return -EAGAIN;
1978 #endif
1981 * If this is a callout device, then just make sure the normal
1982 * device isn't being used.
1984 if (tty->driver.subtype == ISDN_SERIAL_TYPE_CALLOUT) {
1985 if (info->flags & ISDN_ASYNC_NORMAL_ACTIVE)
1986 return -EBUSY;
1987 if ((info->flags & ISDN_ASYNC_CALLOUT_ACTIVE) &&
1988 (info->flags & ISDN_ASYNC_SESSION_LOCKOUT) &&
1989 (info->session != current->session))
1990 return -EBUSY;
1991 if ((info->flags & ISDN_ASYNC_CALLOUT_ACTIVE) &&
1992 (info->flags & ISDN_ASYNC_PGRP_LOCKOUT) &&
1993 (info->pgrp != current->pgrp))
1994 return -EBUSY;
1995 info->flags |= ISDN_ASYNC_CALLOUT_ACTIVE;
1996 return 0;
1999 * If non-blocking mode is set, then make the check up front
2000 * and then exit.
2002 if ((filp->f_flags & O_NONBLOCK) ||
2003 (tty->flags & (1 << TTY_IO_ERROR))) {
2004 if (info->flags & ISDN_ASYNC_CALLOUT_ACTIVE)
2005 return -EBUSY;
2006 info->flags |= ISDN_ASYNC_NORMAL_ACTIVE;
2007 return 0;
2009 if (info->flags & ISDN_ASYNC_CALLOUT_ACTIVE) {
2010 if (info->normal_termios.c_cflag & CLOCAL)
2011 do_clocal = 1;
2012 } else {
2013 if (tty->termios->c_cflag & CLOCAL)
2014 do_clocal = 1;
2017 * Block waiting for the carrier detect and the line to become
2018 * free (i.e., not in use by the callout). While we are in
2019 * this loop, info->count is dropped by one, so that
2020 * isdn_tty_close() knows when to free things. We restore it upon
2021 * exit, either normal or abnormal.
2023 retval = 0;
2024 add_wait_queue(&info->open_wait, &wait);
2025 #ifdef ISDN_DEBUG_MODEM_OPEN
2026 printk(KERN_DEBUG "isdn_tty_block_til_ready before block: ttyi%d, count = %d\n",
2027 info->line, info->count);
2028 #endif
2029 save_flags(flags);
2030 cli();
2031 if (!(tty_hung_up_p(filp)))
2032 info->count--;
2033 restore_flags(flags);
2034 info->blocked_open++;
2035 while (1) {
2036 set_current_state(TASK_INTERRUPTIBLE);
2037 if (tty_hung_up_p(filp) ||
2038 !(info->flags & ISDN_ASYNC_INITIALIZED)) {
2039 #ifdef MODEM_DO_RESTART
2040 if (info->flags & ISDN_ASYNC_HUP_NOTIFY)
2041 retval = -EAGAIN;
2042 else
2043 retval = -ERESTARTSYS;
2044 #else
2045 retval = -EAGAIN;
2046 #endif
2047 break;
2049 if (!(info->flags & ISDN_ASYNC_CALLOUT_ACTIVE) &&
2050 !(info->flags & ISDN_ASYNC_CLOSING) &&
2051 (do_clocal || (info->msr & UART_MSR_DCD))) {
2052 break;
2054 if (signal_pending(current)) {
2055 retval = -ERESTARTSYS;
2056 break;
2058 #ifdef ISDN_DEBUG_MODEM_OPEN
2059 printk(KERN_DEBUG "isdn_tty_block_til_ready blocking: ttyi%d, count = %d\n",
2060 info->line, info->count);
2061 #endif
2062 schedule();
2064 current->state = TASK_RUNNING;
2065 remove_wait_queue(&info->open_wait, &wait);
2066 if (!tty_hung_up_p(filp))
2067 info->count++;
2068 info->blocked_open--;
2069 #ifdef ISDN_DEBUG_MODEM_OPEN
2070 printk(KERN_DEBUG "isdn_tty_block_til_ready after blocking: ttyi%d, count = %d\n",
2071 info->line, info->count);
2072 #endif
2073 if (retval)
2074 return retval;
2075 info->flags |= ISDN_ASYNC_NORMAL_ACTIVE;
2076 return 0;
2080 * This routine is called whenever a serial port is opened. It
2081 * enables interrupts for a serial port, linking in its async structure into
2082 * the IRQ chain. It also performs the serial-specific
2083 * initialization for the tty structure.
2085 static int
2086 isdn_tty_open(struct tty_struct *tty, struct file *filp)
2088 modem_info *info;
2089 int retval,
2090 line;
2092 line = MINOR(tty->device) - tty->driver.minor_start;
2093 if (line < 0 || line > ISDN_MAX_CHANNELS)
2094 return -ENODEV;
2095 info = &dev->mdm.info[line];
2096 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_open"))
2097 return -ENODEV;
2098 #ifdef ISDN_DEBUG_MODEM_OPEN
2099 printk(KERN_DEBUG "isdn_tty_open %s%d, count = %d\n", tty->driver.name,
2100 info->line, info->count);
2101 #endif
2102 info->count++;
2103 tty->driver_data = info;
2104 info->tty = tty;
2106 * Start up serial port
2108 retval = isdn_tty_startup(info);
2109 if (retval) {
2110 #ifdef ISDN_DEBUG_MODEM_OPEN
2111 printk(KERN_DEBUG "isdn_tty_open return after startup\n");
2112 #endif
2113 return retval;
2115 retval = isdn_tty_block_til_ready(tty, filp, info);
2116 if (retval) {
2117 #ifdef ISDN_DEBUG_MODEM_OPEN
2118 printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
2119 #endif
2120 return retval;
2122 if ((info->count == 1) && (info->flags & ISDN_ASYNC_SPLIT_TERMIOS)) {
2123 if (tty->driver.subtype == ISDN_SERIAL_TYPE_NORMAL)
2124 *tty->termios = info->normal_termios;
2125 else
2126 *tty->termios = info->callout_termios;
2127 isdn_tty_change_speed(info);
2129 info->session = current->session;
2130 info->pgrp = current->pgrp;
2131 #ifdef ISDN_DEBUG_MODEM_OPEN
2132 printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
2133 #endif
2134 dev->modempoll++;
2135 #ifdef ISDN_DEBUG_MODEM_OPEN
2136 printk(KERN_DEBUG "isdn_tty_open normal exit\n");
2137 #endif
2138 return 0;
2141 static void
2142 isdn_tty_close(struct tty_struct *tty, struct file *filp)
2144 modem_info *info = (modem_info *) tty->driver_data;
2145 ulong flags;
2146 ulong timeout;
2148 if (!info || isdn_tty_paranoia_check(info, tty->device, "isdn_tty_close"))
2149 return;
2150 save_flags(flags);
2151 cli();
2152 if (tty_hung_up_p(filp)) {
2153 restore_flags(flags);
2154 #ifdef ISDN_DEBUG_MODEM_OPEN
2155 printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
2156 #endif
2157 return;
2159 if ((tty->count == 1) && (info->count != 1)) {
2161 * Uh, oh. tty->count is 1, which means that the tty
2162 * structure will be freed. Info->count should always
2163 * be one in these conditions. If it's greater than
2164 * one, we've got real problems, since it means the
2165 * serial port won't be shutdown.
2167 printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
2168 "info->count is %d\n", info->count);
2169 info->count = 1;
2171 if (--info->count < 0) {
2172 printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
2173 info->line, info->count);
2174 info->count = 0;
2176 if (info->count) {
2177 restore_flags(flags);
2178 #ifdef ISDN_DEBUG_MODEM_OPEN
2179 printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
2180 #endif
2181 return;
2183 info->flags |= ISDN_ASYNC_CLOSING;
2185 * Save the termios structure, since this port may have
2186 * separate termios for callout and dialin.
2188 if (info->flags & ISDN_ASYNC_NORMAL_ACTIVE)
2189 info->normal_termios = *tty->termios;
2190 if (info->flags & ISDN_ASYNC_CALLOUT_ACTIVE)
2191 info->callout_termios = *tty->termios;
2193 tty->closing = 1;
2195 * At this point we stop accepting input. To do this, we
2196 * disable the receive line status interrupts, and tell the
2197 * interrupt driver to stop checking the data ready bit in the
2198 * line status register.
2200 if (info->flags & ISDN_ASYNC_INITIALIZED) {
2201 tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
2203 * Before we drop DTR, make sure the UART transmitter
2204 * has completely drained; this is especially
2205 * important if there is a transmit FIFO!
2207 timeout = jiffies + HZ;
2208 while (!(info->lsr & UART_LSR_TEMT)) {
2209 set_current_state(TASK_INTERRUPTIBLE);
2210 schedule_timeout(20);
2211 if (time_after(jiffies,timeout))
2212 break;
2215 dev->modempoll--;
2216 isdn_tty_shutdown(info);
2217 if (tty->driver.flush_buffer)
2218 tty->driver.flush_buffer(tty);
2219 if (tty->ldisc.flush_buffer)
2220 tty->ldisc.flush_buffer(tty);
2221 info->tty = 0;
2222 info->ncarrier = 0;
2223 tty->closing = 0;
2224 if (info->blocked_open) {
2225 set_current_state(TASK_INTERRUPTIBLE);
2226 schedule_timeout(50);
2227 wake_up_interruptible(&info->open_wait);
2229 info->flags &= ~(ISDN_ASYNC_NORMAL_ACTIVE | ISDN_ASYNC_CALLOUT_ACTIVE |
2230 ISDN_ASYNC_CLOSING);
2231 wake_up_interruptible(&info->close_wait);
2232 restore_flags(flags);
2233 #ifdef ISDN_DEBUG_MODEM_OPEN
2234 printk(KERN_DEBUG "isdn_tty_close normal exit\n");
2235 #endif
2239 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
2241 static void
2242 isdn_tty_hangup(struct tty_struct *tty)
2244 modem_info *info = (modem_info *) tty->driver_data;
2246 if (isdn_tty_paranoia_check(info, tty->device, "isdn_tty_hangup"))
2247 return;
2248 isdn_tty_shutdown(info);
2249 info->count = 0;
2250 info->flags &= ~(ISDN_ASYNC_NORMAL_ACTIVE | ISDN_ASYNC_CALLOUT_ACTIVE);
2251 info->tty = 0;
2252 wake_up_interruptible(&info->open_wait);
2255 /* This routine initializes all emulator-data.
2257 static void
2258 isdn_tty_reset_profile(atemu * m)
2260 m->profile[0] = 0;
2261 m->profile[1] = 0;
2262 m->profile[2] = 43;
2263 m->profile[3] = 13;
2264 m->profile[4] = 10;
2265 m->profile[5] = 8;
2266 m->profile[6] = 3;
2267 m->profile[7] = 60;
2268 m->profile[8] = 2;
2269 m->profile[9] = 6;
2270 m->profile[10] = 7;
2271 m->profile[11] = 70;
2272 m->profile[12] = 0x45;
2273 m->profile[13] = 4;
2274 m->profile[14] = ISDN_PROTO_L2_X75I;
2275 m->profile[15] = ISDN_PROTO_L3_TRANS;
2276 m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
2277 m->profile[17] = ISDN_MODEM_WINSIZE;
2278 m->profile[18] = 4;
2279 m->profile[19] = 0;
2280 m->profile[20] = 0;
2281 m->profile[23] = 0;
2282 m->pmsn[0] = '\0';
2283 m->plmsn[0] = '\0';
2286 #ifdef CONFIG_ISDN_AUDIO
2287 static void
2288 isdn_tty_modem_reset_vpar(atemu * m)
2290 m->vpar[0] = 2; /* Voice-device (2 = phone line) */
2291 m->vpar[1] = 0; /* Silence detection level (0 = none ) */
2292 m->vpar[2] = 70; /* Silence interval (7 sec. ) */
2293 m->vpar[3] = 2; /* Compression type (1 = ADPCM-2 ) */
2294 m->vpar[4] = 0; /* DTMF detection level (0 = softcode ) */
2295 m->vpar[5] = 8; /* DTMF interval (8 * 5 ms. ) */
2297 #endif
2299 #ifdef CONFIG_ISDN_TTY_FAX
2300 static void
2301 isdn_tty_modem_reset_faxpar(modem_info * info)
2303 T30_s *f = info->fax;
2305 f->code = 0;
2306 f->phase = ISDN_FAX_PHASE_IDLE;
2307 f->direction = 0;
2308 f->resolution = 1; /* fine */
2309 f->rate = 5; /* 14400 bit/s */
2310 f->width = 0;
2311 f->length = 0;
2312 f->compression = 0;
2313 f->ecm = 0;
2314 f->binary = 0;
2315 f->scantime = 0;
2316 memset(&f->id[0], 32, FAXIDLEN - 1);
2317 f->id[FAXIDLEN - 1] = 0;
2318 f->badlin = 0;
2319 f->badmul = 0;
2320 f->bor = 0;
2321 f->nbc = 0;
2322 f->cq = 0;
2323 f->cr = 0;
2324 f->ctcrty = 0;
2325 f->minsp = 0;
2326 f->phcto = 30;
2327 f->rel = 0;
2328 memset(&f->pollid[0], 32, FAXIDLEN - 1);
2329 f->pollid[FAXIDLEN - 1] = 0;
2331 #endif
2333 static void
2334 isdn_tty_modem_reset_regs(modem_info * info, int force)
2336 atemu *m = &info->emu;
2337 if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
2338 memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
2339 memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
2340 memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
2341 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2343 #ifdef CONFIG_ISDN_AUDIO
2344 isdn_tty_modem_reset_vpar(m);
2345 #endif
2346 #ifdef CONFIG_ISDN_TTY_FAX
2347 isdn_tty_modem_reset_faxpar(info);
2348 #endif
2349 m->mdmcmdl = 0;
2352 static void
2353 modem_write_profile(atemu * m)
2355 memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
2356 memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
2357 memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
2358 if (dev->profd)
2359 send_sig(SIGIO, dev->profd, 1);
2363 isdn_tty_modem_init(void)
2365 modem *m;
2366 int i;
2367 modem_info *info;
2369 m = &dev->mdm;
2370 memset(&m->tty_modem, 0, sizeof(struct tty_driver));
2371 m->tty_modem.magic = TTY_DRIVER_MAGIC;
2372 m->tty_modem.name = isdn_ttyname_ttyI;
2373 m->tty_modem.major = ISDN_TTY_MAJOR;
2374 m->tty_modem.minor_start = 0;
2375 m->tty_modem.num = ISDN_MAX_CHANNELS;
2376 m->tty_modem.type = TTY_DRIVER_TYPE_SERIAL;
2377 m->tty_modem.subtype = ISDN_SERIAL_TYPE_NORMAL;
2378 m->tty_modem.init_termios = tty_std_termios;
2379 m->tty_modem.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2380 m->tty_modem.flags = TTY_DRIVER_REAL_RAW;
2381 m->tty_modem.refcount = &m->refcount;
2382 m->tty_modem.table = m->modem_table;
2383 m->tty_modem.termios = m->modem_termios;
2384 m->tty_modem.termios_locked = m->modem_termios_locked;
2385 m->tty_modem.open = isdn_tty_open;
2386 m->tty_modem.close = isdn_tty_close;
2387 m->tty_modem.write = isdn_tty_write;
2388 m->tty_modem.put_char = NULL;
2389 m->tty_modem.flush_chars = isdn_tty_flush_chars;
2390 m->tty_modem.write_room = isdn_tty_write_room;
2391 m->tty_modem.chars_in_buffer = isdn_tty_chars_in_buffer;
2392 m->tty_modem.flush_buffer = isdn_tty_flush_buffer;
2393 m->tty_modem.ioctl = isdn_tty_ioctl;
2394 m->tty_modem.throttle = isdn_tty_throttle;
2395 m->tty_modem.unthrottle = isdn_tty_unthrottle;
2396 m->tty_modem.set_termios = isdn_tty_set_termios;
2397 m->tty_modem.stop = NULL;
2398 m->tty_modem.start = NULL;
2399 m->tty_modem.hangup = isdn_tty_hangup;
2400 m->tty_modem.driver_name = "isdn_tty";
2402 * The callout device is just like normal device except for
2403 * major number and the subtype code.
2405 m->cua_modem = m->tty_modem;
2406 m->cua_modem.name = isdn_ttyname_cui;
2407 m->cua_modem.major = ISDN_TTYAUX_MAJOR;
2408 m->tty_modem.minor_start = 0;
2409 m->cua_modem.subtype = ISDN_SERIAL_TYPE_CALLOUT;
2411 if (tty_register_driver(&m->tty_modem)) {
2412 printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
2413 return -1;
2415 if (tty_register_driver(&m->cua_modem)) {
2416 printk(KERN_WARNING "isdn_tty: Couldn't register modem-callout-device\n");
2417 return -2;
2419 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2420 info = &m->info[i];
2421 #ifdef CONFIG_ISDN_TTY_FAX
2422 if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
2423 printk(KERN_ERR "Could not allocate fax t30-buffer\n");
2424 return -3;
2426 #endif
2427 init_MUTEX(&info->write_sem);
2428 sprintf(info->last_cause, "0000");
2429 sprintf(info->last_num, "none");
2430 info->last_dir = 0;
2431 info->last_lhup = 1;
2432 info->last_l2 = -1;
2433 info->last_si = 0;
2434 isdn_tty_reset_profile(&info->emu);
2435 isdn_tty_modem_reset_regs(info, 1);
2436 info->magic = ISDN_ASYNC_MAGIC;
2437 info->line = i;
2438 info->tty = 0;
2439 info->x_char = 0;
2440 info->count = 0;
2441 info->blocked_open = 0;
2442 info->callout_termios = m->cua_modem.init_termios;
2443 info->normal_termios = m->tty_modem.init_termios;
2444 init_waitqueue_head(&info->open_wait);
2445 init_waitqueue_head(&info->close_wait);
2446 info->isdn_driver = -1;
2447 info->isdn_channel = -1;
2448 info->drv_index = -1;
2449 info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
2450 skb_queue_head_init(&info->xmit_queue);
2451 #ifdef CONFIG_ISDN_AUDIO
2452 skb_queue_head_init(&info->dtmf_queue);
2453 #endif
2454 if (!(info->xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5, GFP_KERNEL))) {
2455 printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
2456 return -3;
2458 /* Make room for T.70 header */
2459 info->xmit_buf += 4;
2461 return 0;
2466 * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
2467 * match the MSN against the MSNs (glob patterns) defined for tty_emulator,
2468 * and return 0 for match, 1 for no match, 2 if MSN could match if longer.
2471 static int
2472 isdn_tty_match_icall(char *cid, atemu *emu, int di)
2474 #ifdef ISDN_DEBUG_MODEM_ICALL
2475 printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
2476 emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
2477 emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
2478 #endif
2479 if (strlen(emu->lmsn)) {
2480 char *p = emu->lmsn;
2481 char *q;
2482 int tmp;
2483 int ret = 0;
2485 while (1) {
2486 if ((q = strchr(p, ';')))
2487 *q = '\0';
2488 if ((tmp = isdn_wildmat(cid, isdn_map_eaz2msn(p, di))) > ret)
2489 ret = tmp;
2490 #ifdef ISDN_DEBUG_MODEM_ICALL
2491 printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
2492 p, isdn_map_eaz2msn(emu->msn, di), tmp);
2493 #endif
2494 if (q) {
2495 *q = ';';
2496 p = q;
2497 p++;
2499 if (!tmp)
2500 return 0;
2501 if (!q)
2502 break;
2504 return ret;
2505 } else {
2506 int tmp;
2507 tmp = isdn_wildmat(cid, isdn_map_eaz2msn(emu->msn, di));
2508 #ifdef ISDN_DEBUG_MODEM_ICALL
2509 printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
2510 isdn_map_eaz2msn(emu->msn, di), tmp);
2511 #endif
2512 return tmp;
2517 * An incoming call-request has arrived.
2518 * Search the tty-devices for an appropriate device and bind
2519 * it to the ISDN-Channel.
2520 * Return:
2522 * 0 = No matching device found.
2523 * 1 = A matching device found.
2524 * 3 = No match found, but eventually would match, if
2525 * CID is longer.
2528 isdn_tty_find_icall(int di, int ch, setup_parm setup)
2530 char *eaz;
2531 int i;
2532 int wret;
2533 int idx;
2534 int si1;
2535 int si2;
2536 char *nr;
2537 ulong flags;
2539 if (!setup.phone[0]) {
2540 nr = "0";
2541 printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
2542 } else
2543 nr = setup.phone;
2544 si1 = (int) setup.si1;
2545 si2 = (int) setup.si2;
2546 if (!setup.eazmsn[0]) {
2547 printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
2548 eaz = "0";
2549 } else
2550 eaz = setup.eazmsn;
2551 #ifdef ISDN_DEBUG_MODEM_ICALL
2552 printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
2553 #endif
2554 wret = 0;
2555 save_flags(flags);
2556 cli();
2557 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2558 modem_info *info = &dev->mdm.info[i];
2560 if (info->count == 0)
2561 continue;
2562 if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) && /* SI1 is matching */
2563 (info->emu.mdmreg[REG_SI2] == si2)) { /* SI2 is matching */
2564 idx = isdn_dc2minor(di, ch);
2565 #ifdef ISDN_DEBUG_MODEM_ICALL
2566 printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
2567 printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
2568 info->flags, info->isdn_driver, info->isdn_channel,
2569 dev->usage[idx]);
2570 #endif
2571 if (
2572 #ifndef FIX_FILE_TRANSFER
2573 (info->flags & ISDN_ASYNC_NORMAL_ACTIVE) &&
2574 #endif
2575 (info->isdn_driver == -1) &&
2576 (info->isdn_channel == -1) &&
2577 (USG_NONE(dev->usage[idx]))) {
2578 int matchret;
2580 if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
2581 wret = matchret;
2582 if (!matchret) { /* EAZ is matching */
2583 info->isdn_driver = di;
2584 info->isdn_channel = ch;
2585 info->drv_index = idx;
2586 dev->m_idx[idx] = info->line;
2587 dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
2588 dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
2589 strcpy(dev->num[idx], nr);
2590 strcpy(info->emu.cpn, eaz);
2591 info->emu.mdmreg[REG_SI1I] = si2bit[si1];
2592 info->emu.mdmreg[REG_PLAN] = setup.plan;
2593 info->emu.mdmreg[REG_SCREEN] = setup.screen;
2594 isdn_info_update();
2595 restore_flags(flags);
2596 printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2597 info->line);
2598 info->msr |= UART_MSR_RI;
2599 isdn_tty_modem_result(RESULT_RING, info);
2600 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2601 return 1;
2606 restore_flags(flags);
2607 printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
2608 ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2))? "rejected" : "ignored");
2609 return (wret == 2)?3:0;
2612 #define TTY_IS_ACTIVE(info) \
2613 (info->flags & (ISDN_ASYNC_NORMAL_ACTIVE | ISDN_ASYNC_CALLOUT_ACTIVE))
2616 isdn_tty_stat_callback(int i, isdn_ctrl *c)
2618 int mi;
2619 modem_info *info;
2620 char *e;
2622 if (i < 0)
2623 return 0;
2624 if ((mi = dev->m_idx[i]) >= 0) {
2625 info = &dev->mdm.info[mi];
2626 switch (c->command) {
2627 case ISDN_STAT_CINF:
2628 printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2629 info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2630 if (e == (char *)c->parm.num)
2631 info->emu.charge = 0;
2633 break;
2634 case ISDN_STAT_BSENT:
2635 #ifdef ISDN_TTY_STAT_DEBUG
2636 printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
2637 #endif
2638 if ((info->isdn_driver == c->driver) &&
2639 (info->isdn_channel == c->arg)) {
2640 info->msr |= UART_MSR_CTS;
2641 if (info->send_outstanding)
2642 if (!(--info->send_outstanding))
2643 info->lsr |= UART_LSR_TEMT;
2644 isdn_tty_tint(info);
2645 return 1;
2647 break;
2648 case ISDN_STAT_CAUSE:
2649 #ifdef ISDN_TTY_STAT_DEBUG
2650 printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2651 #endif
2652 /* Signal cause to tty-device */
2653 strncpy(info->last_cause, c->parm.num, 5);
2654 return 1;
2655 case ISDN_STAT_DISPLAY:
2656 #ifdef ISDN_TTY_STAT_DEBUG
2657 printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2658 #endif
2659 /* Signal display to tty-device */
2660 if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2661 !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2662 isdn_tty_at_cout("\r\n", info);
2663 isdn_tty_at_cout("DISPLAY: ", info);
2664 isdn_tty_at_cout(c->parm.display, info);
2665 isdn_tty_at_cout("\r\n", info);
2667 return 1;
2668 case ISDN_STAT_DCONN:
2669 #ifdef ISDN_TTY_STAT_DEBUG
2670 printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2671 #endif
2672 if (TTY_IS_ACTIVE(info)) {
2673 if (info->dialing == 1) {
2674 info->dialing = 2;
2675 return 1;
2678 break;
2679 case ISDN_STAT_DHUP:
2680 #ifdef ISDN_TTY_STAT_DEBUG
2681 printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2682 #endif
2683 if (TTY_IS_ACTIVE(info)) {
2684 if (info->dialing == 1)
2685 isdn_tty_modem_result(RESULT_BUSY, info);
2686 if (info->dialing > 1)
2687 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2688 info->dialing = 0;
2689 #ifdef ISDN_DEBUG_MODEM_HUP
2690 printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2691 #endif
2692 isdn_tty_modem_hup(info, 0);
2693 return 1;
2695 break;
2696 case ISDN_STAT_BCONN:
2697 #ifdef ISDN_TTY_STAT_DEBUG
2698 printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2699 #endif
2700 /* Schedule CONNECT-Message to any tty
2701 * waiting for it and
2702 * set DCD-bit of its modem-status.
2704 if (TTY_IS_ACTIVE(info)) {
2705 info->msr |= UART_MSR_DCD;
2706 info->emu.charge = 0;
2707 if (info->dialing & 0xf)
2708 info->last_dir = 1;
2709 else
2710 info->last_dir = 0;
2711 info->dialing = 0;
2712 info->rcvsched = 1;
2713 if (USG_MODEM(dev->usage[i])) {
2714 if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2715 strcpy(info->emu.connmsg, c->parm.num);
2716 isdn_tty_modem_result(RESULT_CONNECT, info);
2717 } else
2718 isdn_tty_modem_result(RESULT_CONNECT64000, info);
2720 if (USG_VOICE(dev->usage[i]))
2721 isdn_tty_modem_result(RESULT_VCON, info);
2722 return 1;
2724 break;
2725 case ISDN_STAT_BHUP:
2726 #ifdef ISDN_TTY_STAT_DEBUG
2727 printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2728 #endif
2729 if (TTY_IS_ACTIVE(info)) {
2730 #ifdef ISDN_DEBUG_MODEM_HUP
2731 printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2732 #endif
2733 isdn_tty_modem_hup(info, 0);
2734 return 1;
2736 break;
2737 case ISDN_STAT_NODCH:
2738 #ifdef ISDN_TTY_STAT_DEBUG
2739 printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2740 #endif
2741 if (TTY_IS_ACTIVE(info)) {
2742 if (info->dialing) {
2743 info->dialing = 0;
2744 info->last_l2 = -1;
2745 info->last_si = 0;
2746 sprintf(info->last_cause, "0000");
2747 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2749 isdn_tty_modem_hup(info, 0);
2750 return 1;
2752 break;
2753 case ISDN_STAT_UNLOAD:
2754 #ifdef ISDN_TTY_STAT_DEBUG
2755 printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2756 #endif
2757 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2758 info = &dev->mdm.info[i];
2759 if (info->isdn_driver == c->driver) {
2760 if (info->online)
2761 isdn_tty_modem_hup(info, 1);
2764 return 1;
2765 #ifdef CONFIG_ISDN_TTY_FAX
2766 case ISDN_STAT_FAXIND:
2767 if (TTY_IS_ACTIVE(info)) {
2768 isdn_tty_fax_command(info, c);
2770 break;
2771 #endif
2772 #ifdef CONFIG_ISDN_AUDIO
2773 case ISDN_STAT_AUDIO:
2774 if (TTY_IS_ACTIVE(info)) {
2775 switch(c->parm.num[0]) {
2776 case ISDN_AUDIO_DTMF:
2777 if (info->vonline) {
2778 isdn_audio_put_dle_code(info,
2779 c->parm.num[1]);
2781 break;
2784 break;
2785 #endif
2788 return 0;
2791 /*********************************************************************
2792 Modem-Emulator-Routines
2793 *********************************************************************/
2795 #define cmdchar(c) ((c>=' ')&&(c<=0x7f))
2798 * Put a message from the AT-emulator into receive-buffer of tty,
2799 * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2801 void
2802 isdn_tty_at_cout(char *msg, modem_info * info)
2804 struct tty_struct *tty;
2805 atemu *m = &info->emu;
2806 char *p;
2807 char c;
2808 ulong flags;
2809 struct sk_buff *skb = 0;
2810 char *sp = 0;
2812 if (!msg) {
2813 printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2814 return;
2816 save_flags(flags);
2817 cli();
2818 tty = info->tty;
2819 if ((info->flags & ISDN_ASYNC_CLOSING) || (!tty)) {
2820 restore_flags(flags);
2821 return;
2824 /* use queue instead of direct flip, if online and */
2825 /* data is in queue or flip buffer is full */
2826 if ((info->online) && (((tty->flip.count + strlen(msg)) >= TTY_FLIPBUF_SIZE) ||
2827 (!skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel])))) {
2828 skb = alloc_skb(strlen(msg)
2829 #ifdef CONFIG_ISDN_AUDIO
2830 + sizeof(isdn_audio_skb)
2831 #endif
2832 , GFP_ATOMIC);
2833 if (!skb) {
2834 restore_flags(flags);
2835 return;
2837 #ifdef CONFIG_ISDN_AUDIO
2838 skb_reserve(skb, sizeof(isdn_audio_skb));
2839 #endif
2840 sp = skb_put(skb, strlen(msg));
2841 #ifdef CONFIG_ISDN_AUDIO
2842 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2843 ISDN_AUDIO_SKB_LOCK(skb) = 0;
2844 #endif
2847 for (p = msg; *p; p++) {
2848 switch (*p) {
2849 case '\r':
2850 c = m->mdmreg[REG_CR];
2851 break;
2852 case '\n':
2853 c = m->mdmreg[REG_LF];
2854 break;
2855 case '\b':
2856 c = m->mdmreg[REG_BS];
2857 break;
2858 default:
2859 c = *p;
2861 if (skb) {
2862 *sp++ = c;
2863 } else {
2864 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
2865 break;
2866 tty_insert_flip_char(tty, c, 0);
2869 if (skb) {
2870 __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2871 dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2872 restore_flags(flags);
2873 /* Schedule dequeuing */
2874 if ((dev->modempoll) && (info->rcvsched))
2875 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2877 } else {
2878 restore_flags(flags);
2879 queue_task(&tty->flip.tqueue, &tq_timer);
2884 * Perform ATH Hangup
2886 static void
2887 isdn_tty_on_hook(modem_info * info)
2889 if (info->isdn_channel >= 0) {
2890 #ifdef ISDN_DEBUG_MODEM_HUP
2891 printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2892 #endif
2893 isdn_tty_modem_hup(info, 1);
2897 static void
2898 isdn_tty_off_hook(void)
2900 printk(KERN_DEBUG "isdn_tty_off_hook\n");
2903 #define PLUSWAIT1 (HZ/2) /* 0.5 sec. */
2904 #define PLUSWAIT2 (HZ*3/2) /* 1.5 sec */
2907 * Check Buffer for Modem-escape-sequence, activate timer-callback to
2908 * isdn_tty_modem_escape() if sequence found.
2910 * Parameters:
2911 * p pointer to databuffer
2912 * plus escape-character
2913 * count length of buffer
2914 * pluscount count of valid escape-characters so far
2915 * lastplus timestamp of last character
2917 static void
2918 isdn_tty_check_esc(const u_char * p, u_char plus, int count, int *pluscount,
2919 int *lastplus, int from_user)
2921 char cbuf[3];
2923 if (plus > 127)
2924 return;
2925 if (count > 3) {
2926 p += count - 3;
2927 count = 3;
2928 *pluscount = 0;
2930 if (from_user) {
2931 copy_from_user(cbuf, p, count);
2932 p = cbuf;
2934 while (count > 0) {
2935 if (*(p++) == plus) {
2936 if ((*pluscount)++) {
2937 /* Time since last '+' > 0.5 sec. ? */
2938 if ((jiffies - *lastplus) > PLUSWAIT1)
2939 *pluscount = 1;
2940 } else {
2941 /* Time since last non-'+' < 1.5 sec. ? */
2942 if ((jiffies - *lastplus) < PLUSWAIT2)
2943 *pluscount = 0;
2945 if ((*pluscount == 3) && (count = 1))
2946 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2947 if (*pluscount > 3)
2948 *pluscount = 1;
2949 } else
2950 *pluscount = 0;
2951 *lastplus = jiffies;
2952 count--;
2957 * Return result of AT-emulator to tty-receive-buffer, depending on
2958 * modem-register 12, bit 0 and 1.
2959 * For CONNECT-messages also switch to online-mode.
2960 * For RING-message handle auto-ATA if register 0 != 0
2963 static void
2964 isdn_tty_modem_result(int code, modem_info * info)
2966 atemu *m = &info->emu;
2967 static char *msg[] =
2968 {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2969 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2970 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2971 ulong flags;
2972 char s[ISDN_MSNLEN+10];
2974 switch (code) {
2975 case RESULT_RING:
2976 m->mdmreg[REG_RINGCNT]++;
2977 if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2978 /* Automatically accept incoming call */
2979 isdn_tty_cmd_ATA(info);
2980 break;
2981 case RESULT_NO_CARRIER:
2982 #ifdef ISDN_DEBUG_MODEM_HUP
2983 printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
2984 (info->flags & ISDN_ASYNC_CLOSING),
2985 (!info->tty));
2986 #endif
2987 save_flags(flags);
2988 cli();
2989 m->mdmreg[REG_RINGCNT] = 0;
2990 del_timer(&info->nc_timer);
2991 info->ncarrier = 0;
2992 if ((info->flags & ISDN_ASYNC_CLOSING) || (!info->tty)) {
2993 restore_flags(flags);
2994 return;
2996 restore_flags(flags);
2997 #ifdef CONFIG_ISDN_AUDIO
2998 if (info->vonline & 1) {
2999 #ifdef ISDN_DEBUG_MODEM_VOICE
3000 printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
3001 info->line);
3002 #endif
3003 /* voice-recording, add DLE-ETX */
3004 isdn_tty_at_cout("\020\003", info);
3006 if (info->vonline & 2) {
3007 #ifdef ISDN_DEBUG_MODEM_VOICE
3008 printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
3009 info->line);
3010 #endif
3011 /* voice-playing, add DLE-DC4 */
3012 isdn_tty_at_cout("\020\024", info);
3014 #endif
3015 break;
3016 case RESULT_CONNECT:
3017 case RESULT_CONNECT64000:
3018 sprintf(info->last_cause, "0000");
3019 if (!info->online)
3020 info->online = 2;
3021 break;
3022 case RESULT_VCON:
3023 #ifdef ISDN_DEBUG_MODEM_VOICE
3024 printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
3025 info->line);
3026 #endif
3027 sprintf(info->last_cause, "0000");
3028 if (!info->online)
3029 info->online = 1;
3030 break;
3031 } /* switch(code) */
3033 if (m->mdmreg[REG_RESP] & BIT_RESP) {
3034 /* Show results */
3035 if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
3036 /* Show numeric results only */
3037 sprintf(s, "\r\n%d\r\n", code);
3038 isdn_tty_at_cout(s, info);
3039 } else {
3040 if (code == RESULT_RING) {
3041 /* return if "show RUNG" and ringcounter>1 */
3042 if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
3043 (m->mdmreg[REG_RINGCNT] > 1))
3044 return;
3045 /* print CID, _before_ _every_ ring */
3046 if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
3047 isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
3048 isdn_tty_at_cout(dev->num[info->drv_index], info);
3051 isdn_tty_at_cout("\r\n", info);
3052 isdn_tty_at_cout(msg[code], info);
3053 switch (code) {
3054 case RESULT_CONNECT:
3055 switch (m->mdmreg[REG_L2PROT]) {
3056 case ISDN_PROTO_L2_MODEM:
3057 isdn_tty_at_cout(" ", info);
3058 isdn_tty_at_cout(m->connmsg, info);
3059 break;
3061 break;
3062 case RESULT_RING:
3063 /* Append CPN, if enabled */
3064 if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
3065 sprintf(s, "/%s", m->cpn);
3066 isdn_tty_at_cout(s, info);
3068 /* Print CID only once, _after_ 1st RING */
3069 if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
3070 (m->mdmreg[REG_RINGCNT] == 1)) {
3071 isdn_tty_at_cout("\r\n", info);
3072 isdn_tty_at_cout("CALLER NUMBER: ", info);
3073 isdn_tty_at_cout(dev->num[info->drv_index], info);
3075 break;
3076 case RESULT_NO_CARRIER:
3077 case RESULT_NO_DIALTONE:
3078 case RESULT_BUSY:
3079 case RESULT_NO_ANSWER:
3080 m->mdmreg[REG_RINGCNT] = 0;
3081 /* Append Cause-Message if enabled */
3082 if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
3083 sprintf(s, "/%s", info->last_cause);
3084 isdn_tty_at_cout(s, info);
3086 break;
3087 case RESULT_CONNECT64000:
3088 /* Append Protocol to CONNECT message */
3089 switch (m->mdmreg[REG_L2PROT]) {
3090 case ISDN_PROTO_L2_X75I:
3091 case ISDN_PROTO_L2_X75UI:
3092 case ISDN_PROTO_L2_X75BUI:
3093 isdn_tty_at_cout("/X.75", info);
3094 break;
3095 case ISDN_PROTO_L2_HDLC:
3096 isdn_tty_at_cout("/HDLC", info);
3097 break;
3098 case ISDN_PROTO_L2_V11096:
3099 isdn_tty_at_cout("/V110/9600", info);
3100 break;
3101 case ISDN_PROTO_L2_V11019:
3102 isdn_tty_at_cout("/V110/19200", info);
3103 break;
3104 case ISDN_PROTO_L2_V11038:
3105 isdn_tty_at_cout("/V110/38400", info);
3106 break;
3108 if (m->mdmreg[REG_T70] & BIT_T70) {
3109 isdn_tty_at_cout("/T.70", info);
3110 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
3111 isdn_tty_at_cout("+", info);
3113 break;
3115 isdn_tty_at_cout("\r\n", info);
3118 if (code == RESULT_NO_CARRIER) {
3119 save_flags(flags);
3120 cli();
3121 if ((info->flags & ISDN_ASYNC_CLOSING) || (!info->tty)) {
3122 restore_flags(flags);
3123 return;
3125 if (info->tty->ldisc.flush_buffer)
3126 info->tty->ldisc.flush_buffer(info->tty);
3127 if ((info->flags & ISDN_ASYNC_CHECK_CD) &&
3128 (!((info->flags & ISDN_ASYNC_CALLOUT_ACTIVE) &&
3129 (info->flags & ISDN_ASYNC_CALLOUT_NOHUP)))) {
3130 tty_hangup(info->tty);
3132 restore_flags(flags);
3138 * Display a modem-register-value.
3140 static void
3141 isdn_tty_show_profile(int ridx, modem_info * info)
3143 char v[6];
3145 sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
3146 isdn_tty_at_cout(v, info);
3150 * Get MSN-string from char-pointer, set pointer to end of number
3152 static void
3153 isdn_tty_get_msnstr(char *n, char **p)
3155 int limit = ISDN_MSNLEN - 1;
3157 while (((*p[0] >= '0' && *p[0] <= '9') ||
3158 /* Why a comma ??? */
3159 (*p[0] == ',')) &&
3160 (limit--))
3161 *n++ = *p[0]++;
3162 *n = '\0';
3166 * Get phone-number from modem-commandbuffer
3168 static void
3169 isdn_tty_getdial(char *p, char *q,int cnt)
3171 int first = 1;
3172 int limit = ISDN_MSNLEN - 1; /* MUST match the size of interface var to avoid
3173 buffer overflow */
3175 while (strchr(" 0123456789,#.*WPTS-", *p) && *p && --cnt>0) {
3176 if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
3177 (*p == '*') || (*p == '#')) {
3178 *q++ = *p;
3179 limit--;
3181 if(!limit)
3182 break;
3183 p++;
3184 first = 0;
3186 *q = 0;
3189 #define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
3190 #define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
3192 static void
3193 isdn_tty_report(modem_info * info)
3195 atemu *m = &info->emu;
3196 char s[80];
3198 isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
3199 sprintf(s, " Remote Number: %s\r\n", info->last_num);
3200 isdn_tty_at_cout(s, info);
3201 sprintf(s, " Direction: %s\r\n", info->last_dir ? "outgoing" : "incoming");
3202 isdn_tty_at_cout(s, info);
3203 isdn_tty_at_cout(" Layer-2 Protocol: ", info);
3204 switch (info->last_l2) {
3205 case ISDN_PROTO_L2_X75I:
3206 isdn_tty_at_cout("X.75i", info);
3207 break;
3208 case ISDN_PROTO_L2_X75UI:
3209 isdn_tty_at_cout("X.75ui", info);
3210 break;
3211 case ISDN_PROTO_L2_X75BUI:
3212 isdn_tty_at_cout("X.75bui", info);
3213 break;
3214 case ISDN_PROTO_L2_HDLC:
3215 isdn_tty_at_cout("HDLC", info);
3216 break;
3217 case ISDN_PROTO_L2_V11096:
3218 isdn_tty_at_cout("V.110 9600 Baud", info);
3219 break;
3220 case ISDN_PROTO_L2_V11019:
3221 isdn_tty_at_cout("V.110 19200 Baud", info);
3222 break;
3223 case ISDN_PROTO_L2_V11038:
3224 isdn_tty_at_cout("V.110 38400 Baud", info);
3225 break;
3226 case ISDN_PROTO_L2_TRANS:
3227 isdn_tty_at_cout("transparent", info);
3228 break;
3229 case ISDN_PROTO_L2_MODEM:
3230 isdn_tty_at_cout("modem", info);
3231 break;
3232 case ISDN_PROTO_L2_FAX:
3233 isdn_tty_at_cout("fax", info);
3234 break;
3235 default:
3236 isdn_tty_at_cout("unknown", info);
3237 break;
3239 if (m->mdmreg[REG_T70] & BIT_T70) {
3240 isdn_tty_at_cout("/T.70", info);
3241 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
3242 isdn_tty_at_cout("+", info);
3244 isdn_tty_at_cout("\r\n", info);
3245 isdn_tty_at_cout(" Service: ", info);
3246 switch (info->last_si) {
3247 case 1:
3248 isdn_tty_at_cout("audio\r\n", info);
3249 break;
3250 case 5:
3251 isdn_tty_at_cout("btx\r\n", info);
3252 break;
3253 case 7:
3254 isdn_tty_at_cout("data\r\n", info);
3255 break;
3256 default:
3257 sprintf(s, "%d\r\n", info->last_si);
3258 isdn_tty_at_cout(s, info);
3259 break;
3261 sprintf(s, " Hangup location: %s\r\n", info->last_lhup ? "local" : "remote");
3262 isdn_tty_at_cout(s, info);
3263 sprintf(s, " Last cause: %s\r\n", info->last_cause);
3264 isdn_tty_at_cout(s, info);
3268 * Parse AT&.. commands.
3270 static int
3271 isdn_tty_cmd_ATand(char **p, modem_info * info)
3273 atemu *m = &info->emu;
3274 int i;
3275 char rb[100];
3277 #define MAXRB (sizeof(rb) - 1)
3279 switch (*p[0]) {
3280 case 'B':
3281 /* &B - Set Buffersize */
3282 p[0]++;
3283 i = isdn_getnum(p);
3284 if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
3285 PARSE_ERROR1;
3286 #ifdef CONFIG_ISDN_AUDIO
3287 if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
3288 PARSE_ERROR1;
3289 #endif
3290 m->mdmreg[REG_PSIZE] = i / 16;
3291 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
3292 switch (m->mdmreg[REG_L2PROT]) {
3293 case ISDN_PROTO_L2_V11096:
3294 case ISDN_PROTO_L2_V11019:
3295 case ISDN_PROTO_L2_V11038:
3296 info->xmit_size /= 10;
3298 break;
3299 case 'C':
3300 /* &C - DCD Status */
3301 p[0]++;
3302 switch (isdn_getnum(p)) {
3303 case 0:
3304 m->mdmreg[REG_DCD] &= ~BIT_DCD;
3305 break;
3306 case 1:
3307 m->mdmreg[REG_DCD] |= BIT_DCD;
3308 break;
3309 default:
3310 PARSE_ERROR1
3312 break;
3313 case 'D':
3314 /* &D - Set DTR-Low-behavior */
3315 p[0]++;
3316 switch (isdn_getnum(p)) {
3317 case 0:
3318 m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
3319 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
3320 break;
3321 case 2:
3322 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
3323 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
3324 break;
3325 case 3:
3326 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
3327 m->mdmreg[REG_DTRR] |= BIT_DTRR;
3328 break;
3329 default:
3330 PARSE_ERROR1
3332 break;
3333 case 'E':
3334 /* &E -Set EAZ/MSN */
3335 p[0]++;
3336 isdn_tty_get_msnstr(m->msn, p);
3337 break;
3338 case 'F':
3339 /* &F -Set Factory-Defaults */
3340 p[0]++;
3341 if (info->msr & UART_MSR_DCD)
3342 PARSE_ERROR1;
3343 isdn_tty_reset_profile(m);
3344 isdn_tty_modem_reset_regs(info, 1);
3345 break;
3346 #ifdef DUMMY_HAYES_AT
3347 case 'K':
3348 /* only for be compilant with common scripts */
3349 /* &K Flowcontrol - no function */
3350 p[0]++;
3351 isdn_getnum(p);
3352 break;
3353 #endif
3354 case 'L':
3355 /* &L -Set Numbers to listen on */
3356 p[0]++;
3357 i = 0;
3358 while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
3359 (i < ISDN_LMSNLEN))
3360 m->lmsn[i++] = *p[0]++;
3361 m->lmsn[i] = '\0';
3362 break;
3363 case 'R':
3364 /* &R - Set V.110 bitrate adaption */
3365 p[0]++;
3366 i = isdn_getnum(p);
3367 switch (i) {
3368 case 0:
3369 /* Switch off V.110, back to X.75 */
3370 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3371 m->mdmreg[REG_SI2] = 0;
3372 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
3373 break;
3374 case 9600:
3375 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
3376 m->mdmreg[REG_SI2] = 197;
3377 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
3378 break;
3379 case 19200:
3380 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
3381 m->mdmreg[REG_SI2] = 199;
3382 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
3383 break;
3384 case 38400:
3385 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
3386 m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
3387 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
3388 break;
3389 default:
3390 PARSE_ERROR1;
3392 /* Switch off T.70 */
3393 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
3394 /* Set Service 7 */
3395 m->mdmreg[REG_SI1] |= 4;
3396 break;
3397 case 'S':
3398 /* &S - Set Windowsize */
3399 p[0]++;
3400 i = isdn_getnum(p);
3401 if ((i > 0) && (i < 9))
3402 m->mdmreg[REG_WSIZE] = i;
3403 else
3404 PARSE_ERROR1;
3405 break;
3406 case 'V':
3407 /* &V - Show registers */
3408 p[0]++;
3409 isdn_tty_at_cout("\r\n", info);
3410 for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
3411 sprintf(rb, "S%02d=%03d%s", i,
3412 m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
3413 isdn_tty_at_cout(rb, info);
3415 sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
3416 strlen(m->msn) ? m->msn : "None");
3417 isdn_tty_at_cout(rb, info);
3418 if (strlen(m->lmsn)) {
3419 isdn_tty_at_cout("\r\nListen: ", info);
3420 isdn_tty_at_cout(m->lmsn, info);
3421 isdn_tty_at_cout("\r\n", info);
3423 break;
3424 case 'W':
3425 /* &W - Write Profile */
3426 p[0]++;
3427 switch (*p[0]) {
3428 case '0':
3429 p[0]++;
3430 modem_write_profile(m);
3431 break;
3432 default:
3433 PARSE_ERROR1;
3435 break;
3436 case 'X':
3437 /* &X - Switch to BTX-Mode and T.70 */
3438 p[0]++;
3439 switch (isdn_getnum(p)) {
3440 case 0:
3441 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
3442 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
3443 break;
3444 case 1:
3445 m->mdmreg[REG_T70] |= BIT_T70;
3446 m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
3447 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3448 info->xmit_size = 112;
3449 m->mdmreg[REG_SI1] = 4;
3450 m->mdmreg[REG_SI2] = 0;
3451 break;
3452 case 2:
3453 m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
3454 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3455 info->xmit_size = 112;
3456 m->mdmreg[REG_SI1] = 4;
3457 m->mdmreg[REG_SI2] = 0;
3458 break;
3459 default:
3460 PARSE_ERROR1;
3462 break;
3463 default:
3464 PARSE_ERROR1;
3466 return 0;
3469 static int
3470 isdn_tty_check_ats(int mreg, int mval, modem_info * info, atemu * m)
3472 /* Some plausibility checks */
3473 switch (mreg) {
3474 case REG_L2PROT:
3475 if (mval > ISDN_PROTO_L2_MAX)
3476 return 1;
3477 break;
3478 case REG_PSIZE:
3479 if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
3480 return 1;
3481 #ifdef CONFIG_ISDN_AUDIO
3482 if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
3483 return 1;
3484 #endif
3485 info->xmit_size = mval * 16;
3486 switch (m->mdmreg[REG_L2PROT]) {
3487 case ISDN_PROTO_L2_V11096:
3488 case ISDN_PROTO_L2_V11019:
3489 case ISDN_PROTO_L2_V11038:
3490 info->xmit_size /= 10;
3492 break;
3493 case REG_SI1I:
3494 case REG_PLAN:
3495 case REG_SCREEN:
3496 /* readonly registers */
3497 return 1;
3499 return 0;
3503 * Perform ATS command
3505 static int
3506 isdn_tty_cmd_ATS(char **p, modem_info * info)
3508 atemu *m = &info->emu;
3509 int bitpos;
3510 int mreg;
3511 int mval;
3512 int bval;
3514 mreg = isdn_getnum(p);
3515 if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
3516 PARSE_ERROR1;
3517 switch (*p[0]) {
3518 case '=':
3519 p[0]++;
3520 mval = isdn_getnum(p);
3521 if (mval < 0 || mval > 255)
3522 PARSE_ERROR1;
3523 if (isdn_tty_check_ats(mreg, mval, info, m))
3524 PARSE_ERROR1;
3525 m->mdmreg[mreg] = mval;
3526 break;
3527 case '.':
3528 /* Set/Clear a single bit */
3529 p[0]++;
3530 bitpos = isdn_getnum(p);
3531 if ((bitpos < 0) || (bitpos > 7))
3532 PARSE_ERROR1;
3533 switch (*p[0]) {
3534 case '=':
3535 p[0]++;
3536 bval = isdn_getnum(p);
3537 if (bval < 0 || bval > 1)
3538 PARSE_ERROR1;
3539 if (bval)
3540 mval = m->mdmreg[mreg] | (1 << bitpos);
3541 else
3542 mval = m->mdmreg[mreg] & ~(1 << bitpos);
3543 if (isdn_tty_check_ats(mreg, mval, info, m))
3544 PARSE_ERROR1;
3545 m->mdmreg[mreg] = mval;
3546 break;
3547 case '?':
3548 p[0]++;
3549 isdn_tty_at_cout("\r\n", info);
3550 isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
3551 info);
3552 break;
3553 default:
3554 PARSE_ERROR1;
3556 break;
3557 case '?':
3558 p[0]++;
3559 isdn_tty_show_profile(mreg, info);
3560 break;
3561 default:
3562 PARSE_ERROR1;
3563 break;
3565 return 0;
3569 * Perform ATA command
3571 static void
3572 isdn_tty_cmd_ATA(modem_info * info)
3574 atemu *m = &info->emu;
3575 isdn_ctrl cmd;
3576 int l2;
3578 if (info->msr & UART_MSR_RI) {
3579 /* Accept incoming call */
3580 info->last_dir = 0;
3581 strcpy(info->last_num, dev->num[info->drv_index]);
3582 m->mdmreg[REG_RINGCNT] = 0;
3583 info->msr &= ~UART_MSR_RI;
3584 l2 = m->mdmreg[REG_L2PROT];
3585 #ifdef CONFIG_ISDN_AUDIO
3586 /* If more than one bit set in reg18, autoselect Layer2 */
3587 if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
3588 if (m->mdmreg[REG_SI1I] == 1) {
3589 if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
3590 l2 = ISDN_PROTO_L2_TRANS;
3591 } else
3592 l2 = ISDN_PROTO_L2_X75I;
3594 #endif
3595 cmd.driver = info->isdn_driver;
3596 cmd.command = ISDN_CMD_SETL2;
3597 cmd.arg = info->isdn_channel + (l2 << 8);
3598 info->last_l2 = l2;
3599 isdn_command(&cmd);
3600 cmd.driver = info->isdn_driver;
3601 cmd.command = ISDN_CMD_SETL3;
3602 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
3603 #ifdef CONFIG_ISDN_TTY_FAX
3604 if (l2 == ISDN_PROTO_L2_FAX) {
3605 cmd.parm.fax = info->fax;
3606 info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3608 #endif
3609 isdn_command(&cmd);
3610 cmd.driver = info->isdn_driver;
3611 cmd.arg = info->isdn_channel;
3612 cmd.command = ISDN_CMD_ACCEPTD;
3613 info->dialing = 16;
3614 info->emu.carrierwait = 0;
3615 isdn_command(&cmd);
3616 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3617 } else
3618 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3621 #ifdef CONFIG_ISDN_AUDIO
3623 * Parse AT+F.. commands
3625 static int
3626 isdn_tty_cmd_PLUSF(char **p, modem_info * info)
3628 atemu *m = &info->emu;
3629 char rs[20];
3631 if (!strncmp(p[0], "CLASS", 5)) {
3632 p[0] += 5;
3633 switch (*p[0]) {
3634 case '?':
3635 p[0]++;
3636 sprintf(rs, "\r\n%d",
3637 (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3638 #ifdef CONFIG_ISDN_TTY_FAX
3639 if (TTY_IS_FCLASS2(info))
3640 sprintf(rs, "\r\n2");
3641 else if (TTY_IS_FCLASS1(info))
3642 sprintf(rs, "\r\n1");
3643 #endif
3644 isdn_tty_at_cout(rs, info);
3645 break;
3646 case '=':
3647 p[0]++;
3648 switch (*p[0]) {
3649 case '0':
3650 p[0]++;
3651 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3652 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3653 m->mdmreg[REG_SI1] = 4;
3654 info->xmit_size =
3655 m->mdmreg[REG_PSIZE] * 16;
3656 break;
3657 #ifdef CONFIG_ISDN_TTY_FAX
3658 case '1':
3659 p[0]++;
3660 if (!(dev->global_features &
3661 ISDN_FEATURE_L3_FCLASS1))
3662 PARSE_ERROR1;
3663 m->mdmreg[REG_SI1] = 1;
3664 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3665 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3666 info->xmit_size =
3667 m->mdmreg[REG_PSIZE] * 16;
3668 break;
3669 case '2':
3670 p[0]++;
3671 if (!(dev->global_features &
3672 ISDN_FEATURE_L3_FCLASS2))
3673 PARSE_ERROR1;
3674 m->mdmreg[REG_SI1] = 1;
3675 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3676 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3677 info->xmit_size =
3678 m->mdmreg[REG_PSIZE] * 16;
3679 break;
3680 #endif
3681 case '8':
3682 p[0]++;
3683 /* L2 will change on dialout with si=1 */
3684 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3685 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3686 m->mdmreg[REG_SI1] = 5;
3687 info->xmit_size = VBUF;
3688 break;
3689 case '?':
3690 p[0]++;
3691 strcpy(rs, "\r\n0,");
3692 #ifdef CONFIG_ISDN_TTY_FAX
3693 if (dev->global_features &
3694 ISDN_FEATURE_L3_FCLASS1)
3695 strcat(rs, "1,");
3696 if (dev->global_features &
3697 ISDN_FEATURE_L3_FCLASS2)
3698 strcat(rs, "2,");
3699 #endif
3700 strcat(rs, "8");
3701 isdn_tty_at_cout(rs, info);
3702 break;
3703 default:
3704 PARSE_ERROR1;
3706 break;
3707 default:
3708 PARSE_ERROR1;
3710 return 0;
3712 #ifdef CONFIG_ISDN_TTY_FAX
3713 return (isdn_tty_cmd_PLUSF_FAX(p, info));
3714 #else
3715 PARSE_ERROR1;
3716 #endif
3720 * Parse AT+V.. commands
3722 static int
3723 isdn_tty_cmd_PLUSV(char **p, modem_info * info)
3725 atemu *m = &info->emu;
3726 isdn_ctrl cmd;
3727 static char *vcmd[] =
3728 {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
3729 int i;
3730 int par1;
3731 int par2;
3732 char rs[20];
3734 i = 0;
3735 while (vcmd[i]) {
3736 if (!strncmp(vcmd[i], p[0], 2)) {
3737 p[0] += 2;
3738 break;
3740 i++;
3742 switch (i) {
3743 case 0:
3744 /* AT+VNH - Auto hangup feature */
3745 switch (*p[0]) {
3746 case '?':
3747 p[0]++;
3748 isdn_tty_at_cout("\r\n1", info);
3749 break;
3750 case '=':
3751 p[0]++;
3752 switch (*p[0]) {
3753 case '1':
3754 p[0]++;
3755 break;
3756 case '?':
3757 p[0]++;
3758 isdn_tty_at_cout("\r\n1", info);
3759 break;
3760 default:
3761 PARSE_ERROR1;
3763 break;
3764 default:
3765 PARSE_ERROR1;
3767 break;
3768 case 1:
3769 /* AT+VIP - Reset all voice parameters */
3770 isdn_tty_modem_reset_vpar(m);
3771 break;
3772 case 2:
3773 /* AT+VLS - Select device, accept incoming call */
3774 switch (*p[0]) {
3775 case '?':
3776 p[0]++;
3777 sprintf(rs, "\r\n%d", m->vpar[0]);
3778 isdn_tty_at_cout(rs, info);
3779 break;
3780 case '=':
3781 p[0]++;
3782 switch (*p[0]) {
3783 case '0':
3784 p[0]++;
3785 m->vpar[0] = 0;
3786 break;
3787 case '2':
3788 p[0]++;
3789 m->vpar[0] = 2;
3790 break;
3791 case '?':
3792 p[0]++;
3793 isdn_tty_at_cout("\r\n0,2", info);
3794 break;
3795 default:
3796 PARSE_ERROR1;
3798 break;
3799 default:
3800 PARSE_ERROR1;
3802 break;
3803 case 3:
3804 /* AT+VRX - Start recording */
3805 if (!m->vpar[0])
3806 PARSE_ERROR1;
3807 if (info->online != 1) {
3808 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3809 return 1;
3811 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3812 if (!info->dtmf_state) {
3813 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3814 PARSE_ERROR1;
3816 info->silence_state = isdn_audio_silence_init(info->silence_state);
3817 if (!info->silence_state) {
3818 printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3819 PARSE_ERROR1;
3821 if (m->vpar[3] < 5) {
3822 info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3823 if (!info->adpcmr) {
3824 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3825 PARSE_ERROR1;
3828 #ifdef ISDN_DEBUG_AT
3829 printk(KERN_DEBUG "AT: +VRX\n");
3830 #endif
3831 info->vonline |= 1;
3832 isdn_tty_modem_result(RESULT_CONNECT, info);
3833 return 0;
3834 break;
3835 case 4:
3836 /* AT+VSD - Silence detection */
3837 switch (*p[0]) {
3838 case '?':
3839 p[0]++;
3840 sprintf(rs, "\r\n<%d>,<%d>",
3841 m->vpar[1],
3842 m->vpar[2]);
3843 isdn_tty_at_cout(rs, info);
3844 break;
3845 case '=':
3846 p[0]++;
3847 if ((*p[0]>='0') && (*p[0]<='9')) {
3848 par1 = isdn_getnum(p);
3849 if ((par1 < 0) || (par1 > 31))
3850 PARSE_ERROR1;
3851 if (*p[0] != ',')
3852 PARSE_ERROR1;
3853 p[0]++;
3854 par2 = isdn_getnum(p);
3855 if ((par2 < 0) || (par2 > 255))
3856 PARSE_ERROR1;
3857 m->vpar[1] = par1;
3858 m->vpar[2] = par2;
3859 break;
3860 } else
3861 if (*p[0] == '?') {
3862 p[0]++;
3863 isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3864 info);
3865 break;
3866 } else
3867 PARSE_ERROR1;
3868 break;
3869 default:
3870 PARSE_ERROR1;
3872 break;
3873 case 5:
3874 /* AT+VSM - Select compression */
3875 switch (*p[0]) {
3876 case '?':
3877 p[0]++;
3878 sprintf(rs, "\r\n<%d>,<%d><8000>",
3879 m->vpar[3],
3880 m->vpar[1]);
3881 isdn_tty_at_cout(rs, info);
3882 break;
3883 case '=':
3884 p[0]++;
3885 switch (*p[0]) {
3886 case '2':
3887 case '3':
3888 case '4':
3889 case '5':
3890 case '6':
3891 par1 = isdn_getnum(p);
3892 if ((par1 < 2) || (par1 > 6))
3893 PARSE_ERROR1;
3894 m->vpar[3] = par1;
3895 break;
3896 case '?':
3897 p[0]++;
3898 isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3899 info);
3900 isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3901 info);
3902 isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3903 info);
3904 isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3905 info);
3906 isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3907 info);
3908 break;
3909 default:
3910 PARSE_ERROR1;
3912 break;
3913 default:
3914 PARSE_ERROR1;
3916 break;
3917 case 6:
3918 /* AT+VTX - Start sending */
3919 if (!m->vpar[0])
3920 PARSE_ERROR1;
3921 if (info->online != 1) {
3922 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3923 return 1;
3925 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3926 if (!info->dtmf_state) {
3927 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3928 PARSE_ERROR1;
3930 if (m->vpar[3] < 5) {
3931 info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3932 if (!info->adpcms) {
3933 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3934 PARSE_ERROR1;
3937 #ifdef ISDN_DEBUG_AT
3938 printk(KERN_DEBUG "AT: +VTX\n");
3939 #endif
3940 m->lastDLE = 0;
3941 info->vonline |= 2;
3942 isdn_tty_modem_result(RESULT_CONNECT, info);
3943 return 0;
3944 break;
3945 case 7:
3946 /* AT+VDD - DTMF detection */
3947 switch (*p[0]) {
3948 case '?':
3949 p[0]++;
3950 sprintf(rs, "\r\n<%d>,<%d>",
3951 m->vpar[4],
3952 m->vpar[5]);
3953 isdn_tty_at_cout(rs, info);
3954 break;
3955 case '=':
3956 p[0]++;
3957 if ((*p[0]>='0') && (*p[0]<='9')) {
3958 if (info->online != 1)
3959 PARSE_ERROR1;
3960 par1 = isdn_getnum(p);
3961 if ((par1 < 0) || (par1 > 15))
3962 PARSE_ERROR1;
3963 if (*p[0] != ',')
3964 PARSE_ERROR1;
3965 p[0]++;
3966 par2 = isdn_getnum(p);
3967 if ((par2 < 0) || (par2 > 255))
3968 PARSE_ERROR1;
3969 m->vpar[4] = par1;
3970 m->vpar[5] = par2;
3971 cmd.driver = info->isdn_driver;
3972 cmd.command = ISDN_CMD_AUDIO;
3973 cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3974 cmd.parm.num[0] = par1;
3975 cmd.parm.num[1] = par2;
3976 isdn_command(&cmd);
3977 break;
3978 } else
3979 if (*p[0] == '?') {
3980 p[0]++;
3981 isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3982 info);
3983 break;
3984 } else
3985 PARSE_ERROR1;
3986 break;
3987 default:
3988 PARSE_ERROR1;
3990 break;
3991 default:
3992 PARSE_ERROR1;
3994 return 0;
3996 #endif /* CONFIG_ISDN_AUDIO */
3999 * Parse and perform an AT-command-line.
4001 static void
4002 isdn_tty_parse_at(modem_info * info)
4004 atemu *m = &info->emu;
4005 char *p;
4006 char ds[40];
4008 #ifdef ISDN_DEBUG_AT
4009 printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
4010 #endif
4011 for (p = &m->mdmcmd[2]; *p;) {
4012 switch (*p) {
4013 case ' ':
4014 p++;
4015 break;
4016 case 'A':
4017 /* A - Accept incoming call */
4018 p++;
4019 isdn_tty_cmd_ATA(info);
4020 return;
4021 break;
4022 case 'D':
4023 /* D - Dial */
4024 if (info->msr & UART_MSR_DCD)
4025 PARSE_ERROR;
4026 if (info->msr & UART_MSR_RI) {
4027 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
4028 return;
4030 isdn_tty_getdial(++p, ds, sizeof ds);
4031 p += strlen(p);
4032 if (!strlen(m->msn))
4033 isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
4034 else if (strlen(ds))
4035 isdn_tty_dial(ds, info, m);
4036 else
4037 PARSE_ERROR;
4038 return;
4039 case 'E':
4040 /* E - Turn Echo on/off */
4041 p++;
4042 switch (isdn_getnum(&p)) {
4043 case 0:
4044 m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
4045 break;
4046 case 1:
4047 m->mdmreg[REG_ECHO] |= BIT_ECHO;
4048 break;
4049 default:
4050 PARSE_ERROR;
4052 break;
4053 case 'H':
4054 /* H - On/Off-hook */
4055 p++;
4056 switch (*p) {
4057 case '0':
4058 p++;
4059 isdn_tty_on_hook(info);
4060 break;
4061 case '1':
4062 p++;
4063 isdn_tty_off_hook();
4064 break;
4065 default:
4066 isdn_tty_on_hook(info);
4067 break;
4069 break;
4070 case 'I':
4071 /* I - Information */
4072 p++;
4073 isdn_tty_at_cout("\r\nLinux ISDN", info);
4074 switch (*p) {
4075 case '0':
4076 case '1':
4077 p++;
4078 break;
4079 case '2':
4080 p++;
4081 isdn_tty_report(info);
4082 break;
4083 case '3':
4084 p++;
4085 sprintf(ds, "\r\n%d", info->emu.charge);
4086 isdn_tty_at_cout(ds, info);
4087 break;
4088 default:
4090 break;
4091 #ifdef DUMMY_HAYES_AT
4092 case 'L':
4093 case 'M':
4094 /* only for be compilant with common scripts */
4095 /* no function */
4096 p++;
4097 isdn_getnum(&p);
4098 break;
4099 #endif
4100 case 'O':
4101 /* O - Go online */
4102 p++;
4103 if (info->msr & UART_MSR_DCD)
4104 /* if B-Channel is up */
4105 isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT:RESULT_CONNECT64000, info);
4106 else
4107 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
4108 return;
4109 case 'Q':
4110 /* Q - Turn Emulator messages on/off */
4111 p++;
4112 switch (isdn_getnum(&p)) {
4113 case 0:
4114 m->mdmreg[REG_RESP] |= BIT_RESP;
4115 break;
4116 case 1:
4117 m->mdmreg[REG_RESP] &= ~BIT_RESP;
4118 break;
4119 default:
4120 PARSE_ERROR;
4122 break;
4123 case 'S':
4124 /* S - Set/Get Register */
4125 p++;
4126 if (isdn_tty_cmd_ATS(&p, info))
4127 return;
4128 break;
4129 case 'V':
4130 /* V - Numeric or ASCII Emulator-messages */
4131 p++;
4132 switch (isdn_getnum(&p)) {
4133 case 0:
4134 m->mdmreg[REG_RESP] |= BIT_RESPNUM;
4135 break;
4136 case 1:
4137 m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
4138 break;
4139 default:
4140 PARSE_ERROR;
4142 break;
4143 case 'Z':
4144 /* Z - Load Registers from Profile */
4145 p++;
4146 if (info->msr & UART_MSR_DCD) {
4147 info->online = 0;
4148 isdn_tty_on_hook(info);
4150 isdn_tty_modem_reset_regs(info, 1);
4151 break;
4152 case '+':
4153 p++;
4154 switch (*p) {
4155 #ifdef CONFIG_ISDN_AUDIO
4156 case 'F':
4157 p++;
4158 if (isdn_tty_cmd_PLUSF(&p, info))
4159 return;
4160 break;
4161 case 'V':
4162 if ((!(m->mdmreg[REG_SI1] & 1)) ||
4163 (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
4164 PARSE_ERROR;
4165 p++;
4166 if (isdn_tty_cmd_PLUSV(&p, info))
4167 return;
4168 break;
4169 #endif /* CONFIG_ISDN_AUDIO */
4170 case 'S': /* SUSPEND */
4171 p++;
4172 isdn_tty_get_msnstr(ds, &p);
4173 isdn_tty_suspend(ds, info, m);
4174 break;
4175 case 'R': /* RESUME */
4176 p++;
4177 isdn_tty_get_msnstr(ds, &p);
4178 isdn_tty_resume(ds, info, m);
4179 break;
4180 case 'M': /* MESSAGE */
4181 p++;
4182 isdn_tty_send_msg(info, m, p);
4183 break;
4184 default:
4185 PARSE_ERROR;
4187 break;
4188 case '&':
4189 p++;
4190 if (isdn_tty_cmd_ATand(&p, info))
4191 return;
4192 break;
4193 default:
4194 PARSE_ERROR;
4197 #ifdef CONFIG_ISDN_AUDIO
4198 if (!info->vonline)
4199 #endif
4200 isdn_tty_modem_result(RESULT_OK, info);
4203 /* Need own toupper() because standard-toupper is not available
4204 * within modules.
4206 #define my_toupper(c) (((c>='a')&&(c<='z'))?(c&0xdf):c)
4209 * Perform line-editing of AT-commands
4211 * Parameters:
4212 * p inputbuffer
4213 * count length of buffer
4214 * channel index to line (minor-device)
4215 * user flag: buffer is in userspace
4217 static int
4218 isdn_tty_edit_at(const char *p, int count, modem_info * info, int user)
4220 atemu *m = &info->emu;
4221 int total = 0;
4222 u_char c;
4223 char eb[2];
4224 int cnt;
4226 for (cnt = count; cnt > 0; p++, cnt--) {
4227 if (user)
4228 get_user(c, p);
4229 else
4230 c = *p;
4231 total++;
4232 if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
4233 /* Separator (CR or LF) */
4234 m->mdmcmd[m->mdmcmdl] = 0;
4235 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
4236 eb[0] = c;
4237 eb[1] = 0;
4238 isdn_tty_at_cout(eb, info);
4240 if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
4241 isdn_tty_parse_at(info);
4242 m->mdmcmdl = 0;
4243 continue;
4245 if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
4246 /* Backspace-Function */
4247 if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
4248 if (m->mdmcmdl)
4249 m->mdmcmdl--;
4250 if (m->mdmreg[REG_ECHO] & BIT_ECHO)
4251 isdn_tty_at_cout("\b", info);
4253 continue;
4255 if (cmdchar(c)) {
4256 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
4257 eb[0] = c;
4258 eb[1] = 0;
4259 isdn_tty_at_cout(eb, info);
4261 if (m->mdmcmdl < 255) {
4262 c = my_toupper(c);
4263 switch (m->mdmcmdl) {
4264 case 1:
4265 if (c == 'T') {
4266 m->mdmcmd[m->mdmcmdl] = c;
4267 m->mdmcmd[++m->mdmcmdl] = 0;
4268 break;
4269 } else
4270 m->mdmcmdl = 0;
4271 /* Fall through, check for 'A' */
4272 case 0:
4273 if (c == 'A') {
4274 m->mdmcmd[m->mdmcmdl] = c;
4275 m->mdmcmd[++m->mdmcmdl] = 0;
4277 break;
4278 default:
4279 m->mdmcmd[m->mdmcmdl] = c;
4280 m->mdmcmd[++m->mdmcmdl] = 0;
4285 return total;
4289 * Switch all modem-channels who are online and got a valid
4290 * escape-sequence 1.5 seconds ago, to command-mode.
4291 * This function is called every second via timer-interrupt from within
4292 * timer-dispatcher isdn_timer_function()
4294 void
4295 isdn_tty_modem_escape(void)
4297 int ton = 0;
4298 int i;
4299 int midx;
4301 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
4302 if (USG_MODEM(dev->usage[i]))
4303 if ((midx = dev->m_idx[i]) >= 0) {
4304 modem_info *info = &dev->mdm.info[midx];
4305 if (info->online) {
4306 ton = 1;
4307 if ((info->emu.pluscount == 3) &&
4308 ((jiffies - info->emu.lastplus) > PLUSWAIT2)) {
4309 info->emu.pluscount = 0;
4310 info->online = 0;
4311 isdn_tty_modem_result(RESULT_OK, info);
4315 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
4319 * Put a RING-message to all modem-channels who have the RI-bit set.
4320 * This function is called every second via timer-interrupt from within
4321 * timer-dispatcher isdn_timer_function()
4323 void
4324 isdn_tty_modem_ring(void)
4326 int ton = 0;
4327 int i;
4329 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
4330 modem_info *info = &dev->mdm.info[i];
4331 if (info->msr & UART_MSR_RI) {
4332 ton = 1;
4333 isdn_tty_modem_result(RESULT_RING, info);
4336 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
4340 * For all online tty's, try sending data to
4341 * the lower levels.
4343 void
4344 isdn_tty_modem_xmit(void)
4346 int ton = 1;
4347 int i;
4349 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
4350 modem_info *info = &dev->mdm.info[i];
4351 if (info->online) {
4352 ton = 1;
4353 isdn_tty_senddown(info);
4354 isdn_tty_tint(info);
4357 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
4361 * Check all channels if we have a 'no carrier' timeout.
4362 * Timeout value is set by Register S7.
4364 void
4365 isdn_tty_carrier_timeout(void)
4367 int ton = 0;
4368 int i;
4370 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
4371 modem_info *info = &dev->mdm.info[i];
4372 if (info->dialing) {
4373 if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
4374 info->dialing = 0;
4375 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
4376 isdn_tty_modem_hup(info, 1);
4378 else
4379 ton = 1;
4382 isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);