isdn_tty.c 91.2 KB
Newer Older
J
Jiri Slaby 已提交
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13
 * Linux ISDN subsystem, tty functions and AT-command emulator (linklevel).
 *
 * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
 * Copyright 1995,96    by Thinking Objects Software GmbH Wuerzburg
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
 */
#undef ISDN_TTY_STAT_DEBUG

#include <linux/isdn.h>
14
#include <linux/serial.h> /* ASYNC_* flags */
15
#include <linux/slab.h>
L
Linus Torvalds 已提交
16
#include <linux/delay.h>
A
Arnd Bergmann 已提交
17
#include <linux/mutex.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
#include "isdn_common.h"
#include "isdn_tty.h"
#ifdef CONFIG_ISDN_AUDIO
#include "isdn_audio.h"
#define VBUF 0x3e0
#define VBUFX (VBUF/16)
#endif

#define FIX_FILE_TRANSFER
#define	DUMMY_HAYES_AT

/* Prototypes */

A
Arnd Bergmann 已提交
31
static DEFINE_MUTEX(modem_info_mutex);
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static int isdn_tty_edit_at(const char *, int, modem_info *);
static void isdn_tty_check_esc(const u_char *, u_char, int, int *, u_long *);
static void isdn_tty_modem_reset_regs(modem_info *, int);
static void isdn_tty_cmd_ATA(modem_info *);
static void isdn_tty_flush_buffer(struct tty_struct *);
static void isdn_tty_modem_result(int, modem_info *);
#ifdef CONFIG_ISDN_AUDIO
static int isdn_tty_countDLE(unsigned char *, int);
#endif

/* Leave this unchanged unless you know what you do! */
#define MODEM_PARANOIA_CHECK
#define MODEM_DO_RESTART

static int bit2si[8] =
{1, 5, 7, 7, 7, 7, 7, 7};
static int si2bit[8] =
{4, 1, 4, 4, 4, 4, 4, 4};

/* isdn_tty_try_read() is called from within isdn_tty_rcv_skb()
 * to stuff incoming data directly into a tty's flip-buffer. This
 * is done to speed up tty-receiving if the receive-queue is empty.
 * This routine MUST be called with interrupts off.
 * Return:
 *  1 = Success
 *  0 = Failure, data has to be buffered and later processed by
 *      isdn_tty_readmodem().
 */
static int
61
isdn_tty_try_read(modem_info *info, struct sk_buff *skb)
L
Linus Torvalds 已提交
62 63 64 65
{
	int c;
	int len;
	struct tty_struct *tty;
A
Alan Cox 已提交
66
	char last;
L
Linus Torvalds 已提交
67

68 69 70 71 72 73 74 75 76 77 78
	if (!info->online)
		return 0;

	tty = info->tty;
	if (!tty)
		return 0;

	if (!(info->mcr & UART_MCR_RTS))
		return 0;

	len = skb->len
L
Linus Torvalds 已提交
79
#ifdef CONFIG_ISDN_AUDIO
80
		+ ISDN_AUDIO_SKB_DLECOUNT(skb)
L
Linus Torvalds 已提交
81
#endif
82 83 84 85 86
		;

	c = tty_buffer_request_room(tty, len);
	if (c < len)
		return 0;
A
Alan Cox 已提交
87

L
Linus Torvalds 已提交
88
#ifdef CONFIG_ISDN_AUDIO
89 90 91 92 93 94 95 96 97 98 99 100
	if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
		int l = skb->len;
		unsigned char *dp = skb->data;
		while (--l) {
			if (*dp == DLE)
				tty_insert_flip_char(tty, DLE, 0);
			tty_insert_flip_char(tty, *dp++, 0);
		}
		if (*dp == DLE)
			tty_insert_flip_char(tty, DLE, 0);
		last = *dp;
	} else {
L
Linus Torvalds 已提交
101
#endif
102 103 104 105
		if (len > 1)
			tty_insert_flip_string(tty, skb->data, len - 1);
		last = skb->data[len - 1];
#ifdef CONFIG_ISDN_AUDIO
L
Linus Torvalds 已提交
106
	}
107 108 109 110 111 112 113 114 115
#endif
	if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
		tty_insert_flip_char(tty, last, 0xFF);
	else
		tty_insert_flip_char(tty, last, TTY_NORMAL);
	tty_flip_buffer_push(tty);
	kfree_skb(skb);

	return 1;
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
}

/* isdn_tty_readmodem() is called periodically from within timer-interrupt.
 * It tries getting received data from the receive queue an stuff it into
 * the tty's flip-buffer.
 */
void
isdn_tty_readmodem(void)
{
	int resched = 0;
	int midx;
	int i;
	int r;
	struct tty_struct *tty;
	modem_info *info;

	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
133 134 135 136 137 138 139 140 141
		midx = dev->m_idx[i];
		if (midx < 0)
			continue;

		info = &dev->mdm.info[midx];
		if (!info->online)
			continue;

		r = 0;
L
Linus Torvalds 已提交
142
#ifdef CONFIG_ISDN_AUDIO
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
		isdn_audio_eval_dtmf(info);
		if ((info->vonline & 1) && (info->emu.vpar[1]))
			isdn_audio_eval_silence(info);
#endif
		tty = info->tty;
		if (tty) {
			if (info->mcr & UART_MCR_RTS) {
				/* CISCO AsyncPPP Hack */
				if (!(info->emu.mdmreg[REG_CPPP] & BIT_CPPP))
					r = isdn_readbchan_tty(info->isdn_driver, info->isdn_channel, tty, 0);
				else
					r = isdn_readbchan_tty(info->isdn_driver, info->isdn_channel, tty, 1);
				if (r)
					tty_flip_buffer_push(tty);
			} else
				r = 1;
		} else
			r = 1;
		if (r) {
			info->rcvsched = 0;
			resched = 1;
		} else
			info->rcvsched = 1;
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	}
	if (!resched)
		isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
}

int
isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
{
	ulong flags;
	int midx;
#ifdef CONFIG_ISDN_AUDIO
	int ifmt;
#endif
	modem_info *info;

	if ((midx = dev->m_idx[i]) < 0) {
		/* if midx is invalid, packet is not for tty */
		return 0;
	}
	info = &dev->mdm.info[midx];
#ifdef CONFIG_ISDN_AUDIO
	ifmt = 1;
188

L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	if ((info->vonline) && (!info->emu.vpar[4]))
		isdn_audio_calc_dtmf(info, skb->data, skb->len, ifmt);
	if ((info->vonline & 1) && (info->emu.vpar[1]))
		isdn_audio_calc_silence(info, skb->data, skb->len, ifmt);
#endif
	if ((info->online < 2)
#ifdef CONFIG_ISDN_AUDIO
	    && (!(info->vonline & 1))
#endif
		) {
		/* If Modem not listening, drop data */
		kfree_skb(skb);
		return 1;
	}
	if (info->emu.mdmreg[REG_T70] & BIT_T70) {
		if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT) {
			/* T.70 decoding: throw away the T.70 header (2 or 4 bytes)   */
			if (skb->data[0] == 3) /* pure data packet -> 4 byte headers  */
				skb_pull(skb, 4);
			else
				if (skb->data[0] == 1) /* keepalive packet -> 2 byte hdr  */
					skb_pull(skb, 2);
		} else
			/* T.70 decoding: Simply throw away the T.70 header (4 bytes) */
			if ((skb->data[0] == 1) && ((skb->data[1] == 0) || (skb->data[1] == 1)))
				skb_pull(skb, 4);
	}
#ifdef CONFIG_ISDN_AUDIO
	ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
	ISDN_AUDIO_SKB_LOCK(skb) = 0;
	if (info->vonline & 1) {
		/* voice conversion/compression */
		switch (info->emu.vpar[3]) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		case 2:
		case 3:
		case 4:
			/* adpcm
			 * Since compressed data takes less
			 * space, we can overwrite the buffer.
			 */
			skb_trim(skb, isdn_audio_xlaw2adpcm(info->adpcmr,
							    ifmt,
							    skb->data,
							    skb->data,
							    skb->len));
			break;
		case 5:
			/* a-law */
			if (!ifmt)
				isdn_audio_ulaw2alaw(skb->data, skb->len);
			break;
		case 6:
			/* u-law */
			if (ifmt)
				isdn_audio_alaw2ulaw(skb->data, skb->len);
			break;
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258
		}
		ISDN_AUDIO_SKB_DLECOUNT(skb) =
			isdn_tty_countDLE(skb->data, skb->len);
	}
#ifdef CONFIG_ISDN_TTY_FAX
	else {
		if (info->faxonline & 2) {
			isdn_tty_fax_bitorder(info, skb);
			ISDN_AUDIO_SKB_DLECOUNT(skb) =
				isdn_tty_countDLE(skb->data, skb->len);
		}
	}
#endif
#endif
A
Alan Cox 已提交
259
	/* Try to deliver directly via tty-buf if queue is empty */
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	spin_lock_irqsave(&info->readlock, flags);
	if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
		if (isdn_tty_try_read(info, skb)) {
			spin_unlock_irqrestore(&info->readlock, flags);
			return 1;
		}
	/* Direct deliver failed or queue wasn't empty.
	 * Queue up for later dequeueing via timer-irq.
	 */
	__skb_queue_tail(&dev->drv[di]->rpqueue[channel], skb);
	dev->drv[di]->rcvcount[channel] +=
		(skb->len
#ifdef CONFIG_ISDN_AUDIO
		 + ISDN_AUDIO_SKB_DLECOUNT(skb)
#endif
			);
	spin_unlock_irqrestore(&info->readlock, flags);
	/* Schedule dequeuing */
	if ((dev->modempoll) && (info->rcvsched))
		isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
	return 1;
}

283
static void
284
isdn_tty_cleanup_xmit(modem_info *info)
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292
{
	skb_queue_purge(&info->xmit_queue);
#ifdef CONFIG_ISDN_AUDIO
	skb_queue_purge(&info->dtmf_queue);
#endif
}

static void
293
isdn_tty_tint(modem_info *info)
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
	int len, slen;

	if (!skb)
		return;
	len = skb->len;
	if ((slen = isdn_writebuf_skb_stub(info->isdn_driver,
					   info->isdn_channel, 1, skb)) == len) {
		struct tty_struct *tty = info->tty;
		info->send_outstanding++;
		info->msr &= ~UART_MSR_CTS;
		info->lsr &= ~UART_LSR_TEMT;
		tty_wakeup(tty);
		return;
	}
	if (slen < 0) {
		/* Error: no channel, already shutdown, or wrong parameter */
		dev_kfree_skb(skb);
		return;
	}
	skb_queue_head(&info->xmit_queue, skb);
}

#ifdef CONFIG_ISDN_AUDIO
static int
isdn_tty_countDLE(unsigned char *buf, int len)
{
	int count = 0;

	while (len--)
		if (*buf++ == DLE)
			count++;
	return count;
}

/* This routine is called from within isdn_tty_write() to perform
 * DLE-decoding when sending audio-data.
 */
static int
334
isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342
{
	unsigned char *p = &info->xmit_buf[info->xmit_count];
	int count = 0;

	while (len > 0) {
		if (m->lastDLE) {
			m->lastDLE = 0;
			switch (*p) {
343 344 345 346 347 348 349 350 351 352 353 354 355 356
			case DLE:
				/* Escape code */
				if (len > 1)
					memmove(p, p + 1, len - 1);
				p--;
				count++;
				break;
			case ETX:
				/* End of data */
				info->vonline |= 4;
				return count;
			case DC4:
				/* Abort RX */
				info->vonline &= ~1;
L
Linus Torvalds 已提交
357
#ifdef ISDN_DEBUG_MODEM_VOICE
358 359 360
				printk(KERN_DEBUG
				       "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
				       info->line);
L
Linus Torvalds 已提交
361
#endif
362 363
				isdn_tty_at_cout("\020\003", info);
				if (!info->vonline) {
L
Linus Torvalds 已提交
364
#ifdef ISDN_DEBUG_MODEM_VOICE
365 366 367
					printk(KERN_DEBUG
					       "DLEdown: send VCON on ttyI%d\n",
					       info->line);
L
Linus Torvalds 已提交
368
#endif
369 370 371 372 373 374 375 376 377 378
					isdn_tty_at_cout("\r\nVCON\r\n", info);
				}
				/* Fall through */
			case 'q':
			case 's':
				/* Silence */
				if (len > 1)
					memmove(p, p + 1, len - 1);
				p--;
				break;
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
			}
		} else {
			if (*p == DLE)
				m->lastDLE = 1;
			else
				count++;
		}
		p++;
		len--;
	}
	if (len < 0) {
		printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
		return 0;
	}
	return count;
}

/* This routine is called from within isdn_tty_write() when receiving
 * audio-data. It interrupts receiving, if an character other than
 * ^S or ^Q is sent.
 */
static int
isdn_tty_end_vrx(const char *buf, int c)
{
	char ch;

	while (c--) {
		ch = *buf;
		if ((ch != 0x11) && (ch != 0x13))
			return 1;
		buf++;
	}
	return 0;
}

static int voice_cf[7] =
{0, 0, 4, 3, 2, 0, 0};

#endif                          /* CONFIG_ISDN_AUDIO */

/* isdn_tty_senddown() is called either directly from within isdn_tty_write()
 * or via timer-interrupt from within isdn_tty_modem_xmit(). It pulls
 * outgoing data from the tty's xmit-buffer, handles voice-decompression or
 * T.70 if necessary, and finally queues it up for sending via isdn_tty_tint.
 */
static void
425
isdn_tty_senddown(modem_info *info)
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
{
	int buflen;
	int skb_res;
#ifdef CONFIG_ISDN_AUDIO
	int audio_len;
#endif
	struct sk_buff *skb;

#ifdef CONFIG_ISDN_AUDIO
	if (info->vonline & 4) {
		info->vonline &= ~6;
		if (!info->vonline) {
#ifdef ISDN_DEBUG_MODEM_VOICE
			printk(KERN_DEBUG
			       "senddown: send VCON on ttyI%d\n",
			       info->line);
#endif
			isdn_tty_at_cout("\r\nVCON\r\n", info);
		}
	}
#endif
	if (!(buflen = info->xmit_count))
		return;
449
	if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
L
Linus Torvalds 已提交
450
		info->msr &= ~UART_MSR_CTS;
451
	info->lsr &= ~UART_LSR_TEMT;
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	/* info->xmit_count is modified here and in isdn_tty_write().
	 * So we return here if isdn_tty_write() is in the
	 * critical section.
	 */
	atomic_inc(&info->xmit_lock);
	if (!(atomic_dec_and_test(&info->xmit_lock)))
		return;
	if (info->isdn_driver < 0) {
		info->xmit_count = 0;
		return;
	}
	skb_res = dev->drv[info->isdn_driver]->interface->hl_hdrlen + 4;
#ifdef CONFIG_ISDN_AUDIO
	if (info->vonline & 2)
		audio_len = buflen * voice_cf[info->emu.vpar[3]];
	else
		audio_len = 0;
	skb = dev_alloc_skb(skb_res + buflen + audio_len);
#else
	skb = dev_alloc_skb(skb_res + buflen);
#endif
	if (!skb) {
		printk(KERN_WARNING
		       "isdn_tty: Out of memory in ttyI%d senddown\n",
		       info->line);
		return;
	}
	skb_reserve(skb, skb_res);
	memcpy(skb_put(skb, buflen), info->xmit_buf, buflen);
	info->xmit_count = 0;
#ifdef CONFIG_ISDN_AUDIO
	if (info->vonline & 2) {
		/* For now, ifmt is fixed to 1 (alaw), since this
		 * is used with ISDN everywhere in the world, except
		 * US, Canada and Japan.
		 * Later, when US-ISDN protocols are implemented,
		 * this setting will depend on the D-channel protocol.
		 */
		int ifmt = 1;

		/* voice conversion/decompression */
		switch (info->emu.vpar[3]) {
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		case 2:
		case 3:
		case 4:
			/* adpcm, compatible to ZyXel 1496 modem
			 * with ROM revision 6.01
			 */
			audio_len = isdn_audio_adpcm2xlaw(info->adpcms,
							  ifmt,
							  skb->data,
							  skb_put(skb, audio_len),
							  buflen);
			skb_pull(skb, buflen);
			skb_trim(skb, audio_len);
			break;
		case 5:
			/* a-law */
			if (!ifmt)
				isdn_audio_alaw2ulaw(skb->data,
						     buflen);
			break;
		case 6:
			/* u-law */
			if (ifmt)
				isdn_audio_ulaw2alaw(skb->data,
						     buflen);
			break;
L
Linus Torvalds 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		}
	}
#endif                          /* CONFIG_ISDN_AUDIO */
	if (info->emu.mdmreg[REG_T70] & BIT_T70) {
		/* Add T.70 simplified header */
		if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT)
			memcpy(skb_push(skb, 2), "\1\0", 2);
		else
			memcpy(skb_push(skb, 4), "\1\0\1\0", 4);
	}
	skb_queue_tail(&info->xmit_queue, skb);
}

/************************************************************
 *
 * Modem-functions
 *
 * mostly "stolen" from original Linux-serial.c and friends.
 *
 ************************************************************/

/* The next routine is called once from within timer-interrupt
 * triggered within isdn_tty_modem_ncarrier(). It calls
 * isdn_tty_modem_result() to stuff a "NO CARRIER" Message
A
Alan Cox 已提交
544
 * into the tty's buffer.
L
Linus Torvalds 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558
 */
static void
isdn_tty_modem_do_ncarrier(unsigned long data)
{
	modem_info *info = (modem_info *) data;
	isdn_tty_modem_result(RESULT_NO_CARRIER, info);
}

/* Next routine is called, whenever the DTR-signal is raised.
 * It checks the ncarrier-flag, and triggers the above routine
 * when necessary. The ncarrier-flag is set, whenever DTR goes
 * low.
 */
static void
559
isdn_tty_modem_ncarrier(modem_info *info)
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569
{
	if (info->ncarrier) {
		info->nc_timer.expires = jiffies + HZ;
		add_timer(&info->nc_timer);
	}
}

/*
 * return the usage calculated by si and layer 2 protocol
 */
570
static int
L
Linus Torvalds 已提交
571 572 573 574 575 576
isdn_calc_usage(int si, int l2)
{
	int usg = ISDN_USAGE_MODEM;

#ifdef CONFIG_ISDN_AUDIO
	if (si == 1) {
577 578 579 580
		switch (l2) {
		case ISDN_PROTO_L2_MODEM:
			usg = ISDN_USAGE_MODEM;
			break;
L
Linus Torvalds 已提交
581
#ifdef CONFIG_ISDN_TTY_FAX
582 583 584
		case ISDN_PROTO_L2_FAX:
			usg = ISDN_USAGE_FAX;
			break;
L
Linus Torvalds 已提交
585
#endif
586 587 588 589
		case ISDN_PROTO_L2_TRANS:
		default:
			usg = ISDN_USAGE_VOICE;
			break;
L
Linus Torvalds 已提交
590 591 592
		}
	}
#endif
593
	return (usg);
L
Linus Torvalds 已提交
594 595 596 597 598 599
}

/* isdn_tty_dial() performs dialing of a tty an the necessary
 * setup of the lower levels before that.
 */
static void
600
isdn_tty_dial(char *n, modem_info *info, atemu *m)
L
Linus Torvalds 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
{
	int usg = ISDN_USAGE_MODEM;
	int si = 7;
	int l2 = m->mdmreg[REG_L2PROT];
	u_long flags;
	isdn_ctrl cmd;
	int i;
	int j;

	for (j = 7; j >= 0; j--)
		if (m->mdmreg[REG_SI1] & (1 << j)) {
			si = bit2si[j];
			break;
		}
	usg = isdn_calc_usage(si, l2);
#ifdef CONFIG_ISDN_AUDIO
617 618
	if ((si == 1) &&
	    (l2 != ISDN_PROTO_L2_MODEM)
L
Linus Torvalds 已提交
619
#ifdef CONFIG_ISDN_TTY_FAX
620
	    && (l2 != ISDN_PROTO_L2_FAX)
L
Linus Torvalds 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
#endif
		) {
		l2 = ISDN_PROTO_L2_TRANS;
		usg = ISDN_USAGE_VOICE;
	}
#endif
	m->mdmreg[REG_SI1I] = si2bit[si];
	spin_lock_irqsave(&dev->lock, flags);
	i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
	if (i < 0) {
		spin_unlock_irqrestore(&dev->lock, flags);
		isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
	} else {
		info->isdn_driver = dev->drvmap[i];
		info->isdn_channel = dev->chanmap[i];
		info->drv_index = i;
		dev->m_idx[i] = info->line;
		dev->usage[i] |= ISDN_USAGE_OUTGOING;
		info->last_dir = 1;
		strcpy(info->last_num, n);
		isdn_info_update();
		spin_unlock_irqrestore(&dev->lock, flags);
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
		cmd.command = ISDN_CMD_CLREAZ;
		isdn_command(&cmd);
		strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETEAZ;
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL2;
		info->last_l2 = l2;
		cmd.arg = info->isdn_channel + (l2 << 8);
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL3;
		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
#ifdef CONFIG_ISDN_TTY_FAX
		if (l2 == ISDN_PROTO_L2_FAX) {
			cmd.parm.fax = info->fax;
			info->fax->direction = ISDN_TTY_FAX_CONN_OUT;
		}
#endif
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
		sprintf(cmd.parm.setup.phone, "%s", n);
		sprintf(cmd.parm.setup.eazmsn, "%s",
			isdn_map_eaz2msn(m->msn, info->isdn_driver));
		cmd.parm.setup.si1 = si;
		cmd.parm.setup.si2 = m->mdmreg[REG_SI2];
		cmd.command = ISDN_CMD_DIAL;
		info->dialing = 1;
		info->emu.carrierwait = 0;
		strcpy(dev->num[i], n);
		isdn_info_update();
		isdn_command(&cmd);
		isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
	}
}

/* isdn_tty_hangup() disassociates a tty from the real
 * ISDN-line (hangup). The usage-status is cleared
 * and some cleanup is done also.
 */
void
688
isdn_tty_modem_hup(modem_info *info, int local)
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
{
	isdn_ctrl cmd;
	int di, ch;

	if (!info)
		return;

	di = info->isdn_driver;
	ch = info->isdn_channel;
	if (di < 0 || ch < 0)
		return;

	info->isdn_driver = -1;
	info->isdn_channel = -1;

#ifdef ISDN_DEBUG_MODEM_HUP
	printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
#endif
	info->rcvsched = 0;
	isdn_tty_flush_buffer(info->tty);
	if (info->online) {
		info->last_lhup = local;
		info->online = 0;
		isdn_tty_modem_result(RESULT_NO_CARRIER, info);
	}
#ifdef CONFIG_ISDN_AUDIO
	info->vonline = 0;
#ifdef CONFIG_ISDN_TTY_FAX
	info->faxonline = 0;
	info->fax->phase = ISDN_FAX_PHASE_IDLE;
#endif
	info->emu.vpar[4] = 0;
	info->emu.vpar[5] = 8;
J
Jesper Juhl 已提交
722 723 724 725 726 727 728 729
	kfree(info->dtmf_state);
	info->dtmf_state = NULL;
	kfree(info->silence_state);
	info->silence_state = NULL;
	kfree(info->adpcms);
	info->adpcms = NULL;
	kfree(info->adpcmr);
	info->adpcmr = NULL;
L
Linus Torvalds 已提交
730 731
#endif
	if ((info->msr & UART_MSR_RI) &&
732
	    (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
		isdn_tty_modem_result(RESULT_RUNG, info);
	info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
	info->lsr |= UART_LSR_TEMT;

	if (local) {
		cmd.driver = di;
		cmd.command = ISDN_CMD_HANGUP;
		cmd.arg = ch;
		isdn_command(&cmd);
	}

	isdn_all_eaz(di, ch);
	info->emu.mdmreg[REG_RINGCNT] = 0;
	isdn_free_channel(di, ch, 0);

	if (info->drv_index >= 0) {
		dev->m_idx[info->drv_index] = -1;
		info->drv_index = -1;
	}
}

/*
755
 * Begin of a CAPI like interface, currently used only for
L
Linus Torvalds 已提交
756 757 758
 * supplementary service (CAPI 2.0 part III)
 */
#include <linux/isdn/capicmd.h>
759
#include <linux/module.h>
L
Linus Torvalds 已提交
760 761 762

int
isdn_tty_capi_facility(capi_msg *cm) {
763
	return (-1); /* dummy */
L
Linus Torvalds 已提交
764 765 766 767 768
}

/* isdn_tty_suspend() tries to suspend the current tty connection
 */
static void
769
isdn_tty_suspend(char *id, modem_info *info, atemu *m)
L
Linus Torvalds 已提交
770 771
{
	isdn_ctrl cmd;
772

L
Linus Torvalds 已提交
773 774 775 776 777 778 779 780 781 782
	int l;

	if (!info)
		return;

#ifdef ISDN_DEBUG_MODEM_SERVICES
	printk(KERN_DEBUG "Msusp ttyI%d\n", info->line);
#endif
	l = strlen(id);
	if ((info->isdn_driver >= 0)) {
783
		cmd.parm.cmsg.Length = l + 18;
L
Linus Torvalds 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
		cmd.parm.cmsg.Command = CAPI_FACILITY;
		cmd.parm.cmsg.Subcommand = CAPI_REQ;
		cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
		cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
		cmd.parm.cmsg.para[1] = 0;
		cmd.parm.cmsg.para[2] = l + 3;
		cmd.parm.cmsg.para[3] = 4; /* 16 bit 0x0004 Suspend */
		cmd.parm.cmsg.para[4] = 0;
		cmd.parm.cmsg.para[5] = l;
		strncpy(&cmd.parm.cmsg.para[6], id, l);
		cmd.command = CAPI_PUT_MESSAGE;
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
		isdn_command(&cmd);
	}
}

/* isdn_tty_resume() tries to resume a suspended call
L
Lucas De Marchi 已提交
802
 * setup of the lower levels before that. unfortunately here is no
L
Linus Torvalds 已提交
803 804 805 806 807 808
 * checking for compatibility of used protocols implemented by Q931
 * It does the same things like isdn_tty_dial, the last command
 * is different, may be we can merge it.
 */

static void
809
isdn_tty_resume(char *id, modem_info *info, atemu *m)
L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
{
	int usg = ISDN_USAGE_MODEM;
	int si = 7;
	int l2 = m->mdmreg[REG_L2PROT];
	isdn_ctrl cmd;
	ulong flags;
	int i;
	int j;
	int l;

	l = strlen(id);
	for (j = 7; j >= 0; j--)
		if (m->mdmreg[REG_SI1] & (1 << j)) {
			si = bit2si[j];
			break;
		}
	usg = isdn_calc_usage(si, l2);
#ifdef CONFIG_ISDN_AUDIO
828 829
	if ((si == 1) &&
	    (l2 != ISDN_PROTO_L2_MODEM)
L
Linus Torvalds 已提交
830
#ifdef CONFIG_ISDN_TTY_FAX
831
	    && (l2 != ISDN_PROTO_L2_FAX)
L
Linus Torvalds 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
#endif
		) {
		l2 = ISDN_PROTO_L2_TRANS;
		usg = ISDN_USAGE_VOICE;
	}
#endif
	m->mdmreg[REG_SI1I] = si2bit[si];
	spin_lock_irqsave(&dev->lock, flags);
	i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
	if (i < 0) {
		spin_unlock_irqrestore(&dev->lock, flags);
		isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
	} else {
		info->isdn_driver = dev->drvmap[i];
		info->isdn_channel = dev->chanmap[i];
		info->drv_index = i;
		dev->m_idx[i] = info->line;
		dev->usage[i] |= ISDN_USAGE_OUTGOING;
		info->last_dir = 1;
//		strcpy(info->last_num, n);
		isdn_info_update();
		spin_unlock_irqrestore(&dev->lock, flags);
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
		cmd.command = ISDN_CMD_CLREAZ;
		isdn_command(&cmd);
		strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETEAZ;
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL2;
		info->last_l2 = l2;
		cmd.arg = info->isdn_channel + (l2 << 8);
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL3;
		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
873
		cmd.parm.cmsg.Length = l + 18;
L
Linus Torvalds 已提交
874 875 876 877 878
		cmd.parm.cmsg.Command = CAPI_FACILITY;
		cmd.parm.cmsg.Subcommand = CAPI_REQ;
		cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
		cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
		cmd.parm.cmsg.para[1] = 0;
879
		cmd.parm.cmsg.para[2] = l + 3;
L
Linus Torvalds 已提交
880 881 882 883
		cmd.parm.cmsg.para[3] = 5; /* 16 bit 0x0005 Resume */
		cmd.parm.cmsg.para[4] = 0;
		cmd.parm.cmsg.para[5] = l;
		strncpy(&cmd.parm.cmsg.para[6], id, l);
884
		cmd.command = CAPI_PUT_MESSAGE;
L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897
		info->dialing = 1;
//		strcpy(dev->num[i], n);
		isdn_info_update();
		isdn_command(&cmd);
		isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
	}
}

/* isdn_tty_send_msg() sends a message to a HL driver
 * This is used for hybrid modem cards to send AT commands to it
 */

static void
898
isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
L
Linus Torvalds 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
{
	int usg = ISDN_USAGE_MODEM;
	int si = 7;
	int l2 = m->mdmreg[REG_L2PROT];
	isdn_ctrl cmd;
	ulong flags;
	int i;
	int j;
	int l;

	l = strlen(msg);
	if (!l) {
		isdn_tty_modem_result(RESULT_ERROR, info);
		return;
	}
	for (j = 7; j >= 0; j--)
		if (m->mdmreg[REG_SI1] & (1 << j)) {
			si = bit2si[j];
			break;
		}
	usg = isdn_calc_usage(si, l2);
#ifdef CONFIG_ISDN_AUDIO
921 922
	if ((si == 1) &&
	    (l2 != ISDN_PROTO_L2_MODEM)
L
Linus Torvalds 已提交
923
#ifdef CONFIG_ISDN_TTY_FAX
924
	    && (l2 != ISDN_PROTO_L2_FAX)
L
Linus Torvalds 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
#endif
		) {
		l2 = ISDN_PROTO_L2_TRANS;
		usg = ISDN_USAGE_VOICE;
	}
#endif
	m->mdmreg[REG_SI1I] = si2bit[si];
	spin_lock_irqsave(&dev->lock, flags);
	i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
	if (i < 0) {
		spin_unlock_irqrestore(&dev->lock, flags);
		isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
	} else {
		info->isdn_driver = dev->drvmap[i];
		info->isdn_channel = dev->chanmap[i];
		info->drv_index = i;
		dev->m_idx[i] = info->line;
		dev->usage[i] |= ISDN_USAGE_OUTGOING;
		info->last_dir = 1;
		isdn_info_update();
		spin_unlock_irqrestore(&dev->lock, flags);
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
		cmd.command = ISDN_CMD_CLREAZ;
		isdn_command(&cmd);
		strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETEAZ;
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL2;
		info->last_l2 = l2;
		cmd.arg = info->isdn_channel + (l2 << 8);
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL3;
		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
965
		cmd.parm.cmsg.Length = l + 14;
L
Linus Torvalds 已提交
966 967 968
		cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
		cmd.parm.cmsg.Subcommand = CAPI_REQ;
		cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
969
		cmd.parm.cmsg.para[0] = l + 1;
L
Linus Torvalds 已提交
970
		strncpy(&cmd.parm.cmsg.para[1], msg, l);
971 972
		cmd.parm.cmsg.para[l + 1] = 0xd;
		cmd.command = CAPI_PUT_MESSAGE;
L
Linus Torvalds 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986
/*		info->dialing = 1;
		strcpy(dev->num[i], n);
		isdn_info_update();
*/
		isdn_command(&cmd);
	}
}

static inline int
isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
{
#ifdef MODEM_PARANOIA_CHECK
	if (!info) {
		printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
987
		       name, routine);
L
Linus Torvalds 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
		return 1;
	}
	if (info->magic != ISDN_ASYNC_MAGIC) {
		printk(KERN_WARNING "isdn_tty: bad magic for modem struct %s in %s\n",
		       name, routine);
		return 1;
	}
#endif
	return 0;
}

/*
 * This routine is called to set the UART divisor registers to match
 * the specified baud rate for a serial port.
 */
static void
1004
isdn_tty_change_speed(modem_info *info)
L
Linus Torvalds 已提交
1005 1006
{
	uint cflag,
1007 1008
		cval,
		quot;
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	int i;

	if (!info->tty || !info->tty->termios)
		return;
	cflag = info->tty->termios->c_cflag;

	quot = i = cflag & CBAUD;
	if (i & CBAUDEX) {
		i &= ~CBAUDEX;
		if (i < 1 || i > 2)
			info->tty->termios->c_cflag &= ~CBAUDEX;
		else
			i += 15;
	}
	if (quot) {
		info->mcr |= UART_MCR_DTR;
		isdn_tty_modem_ncarrier(info);
	} else {
		info->mcr &= ~UART_MCR_DTR;
		if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
#ifdef ISDN_DEBUG_MODEM_HUP
			printk(KERN_DEBUG "Mhup in changespeed\n");
#endif
			if (info->online)
				info->ncarrier = 1;
			isdn_tty_modem_reset_regs(info, 0);
			isdn_tty_modem_hup(info, 1);
		}
		return;
	}
	/* byte size and parity */
	cval = cflag & (CSIZE | CSTOPB);
	cval >>= 4;
	if (cflag & PARENB)
		cval |= UART_LCR_PARITY;
	if (!(cflag & PARODD))
		cval |= UART_LCR_EPAR;

	/* CTS flow control flag and modem status interrupts */
	if (cflag & CRTSCTS) {
J
Jiri Slaby 已提交
1049
		info->port.flags |= ASYNC_CTS_FLOW;
L
Linus Torvalds 已提交
1050
	} else
J
Jiri Slaby 已提交
1051
		info->port.flags &= ~ASYNC_CTS_FLOW;
L
Linus Torvalds 已提交
1052
	if (cflag & CLOCAL)
J
Jiri Slaby 已提交
1053
		info->port.flags &= ~ASYNC_CHECK_CD;
L
Linus Torvalds 已提交
1054
	else {
J
Jiri Slaby 已提交
1055
		info->port.flags |= ASYNC_CHECK_CD;
L
Linus Torvalds 已提交
1056 1057 1058 1059
	}
}

static int
1060
isdn_tty_startup(modem_info *info)
L
Linus Torvalds 已提交
1061
{
J
Jiri Slaby 已提交
1062
	if (info->port.flags & ASYNC_INITIALIZED)
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
		return 0;
	isdn_lock_drivers();
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
#endif
	/*
	 * Now, initialize the UART
	 */
	info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
	if (info->tty)
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
	/*
	 * and set the speed of the serial port
	 */
	isdn_tty_change_speed(info);

J
Jiri Slaby 已提交
1079
	info->port.flags |= ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
	info->send_outstanding = 0;
	return 0;
}

/*
 * This routine will shutdown a serial port; interrupts are disabled, and
 * DTR is dropped if the hangup on close termio flag is on.
 */
static void
1090
isdn_tty_shutdown(modem_info *info)
L
Linus Torvalds 已提交
1091
{
J
Jiri Slaby 已提交
1092
	if (!(info->port.flags & ASYNC_INITIALIZED))
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
		return;
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
#endif
	isdn_unlock_drivers();
	info->msr &= ~UART_MSR_RI;
	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
		info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
		if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
			isdn_tty_modem_reset_regs(info, 0);
#ifdef ISDN_DEBUG_MODEM_HUP
			printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
#endif
			isdn_tty_modem_hup(info, 1);
		}
	}
	if (info->tty)
		set_bit(TTY_IO_ERROR, &info->tty->flags);

J
Jiri Slaby 已提交
1112
	info->port.flags &= ~ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
}

/* isdn_tty_write() is the main send-routine. It is called from the upper
 * levels within the kernel to perform sending data. Depending on the
 * online-flag it either directs output to the at-command-interpreter or
 * to the lower level. Additional tasks done here:
 *  - If online, check for escape-sequence (+++)
 *  - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
 *  - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
 *  - If dialing, abort dial.
 */
static int
1125
isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
L
Linus Torvalds 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
{
	int c;
	int total = 0;
	modem_info *info = (modem_info *) tty->driver_data;
	atemu *m = &info->emu;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write"))
		return 0;
	/* See isdn_tty_senddown() */
	atomic_inc(&info->xmit_lock);
	while (1) {
		c = count;
		if (c > info->xmit_size - info->xmit_count)
			c = info->xmit_size - info->xmit_count;
		if (info->isdn_driver >= 0 && c > dev->drv[info->isdn_driver]->maxbufsize)
			c = dev->drv[info->isdn_driver]->maxbufsize;
		if (c <= 0)
			break;
		if ((info->online > 1)
#ifdef CONFIG_ISDN_AUDIO
		    || (info->vonline & 3)
#endif
			) {
#ifdef CONFIG_ISDN_AUDIO
			if (!info->vonline)
#endif
				isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
						   &(m->pluscount),
						   &(m->lastplus));
			memcpy(&(info->xmit_buf[info->xmit_count]), buf, c);
#ifdef CONFIG_ISDN_AUDIO
			if (info->vonline) {
				int cc = isdn_tty_handleDLEdown(info, m, c);
				if (info->vonline & 2) {
					if (!cc) {
						/* If DLE decoding results in zero-transmit, but
						 * c originally was non-zero, do a wakeup.
						 */
						tty_wakeup(tty);
						info->msr |= UART_MSR_CTS;
						info->lsr |= UART_LSR_TEMT;
					}
					info->xmit_count += cc;
				}
				if ((info->vonline & 3) == 1) {
					/* Do NOT handle Ctrl-Q or Ctrl-S
					 * when in full-duplex audio mode.
					 */
					if (isdn_tty_end_vrx(buf, c)) {
						info->vonline &= ~1;
#ifdef ISDN_DEBUG_MODEM_VOICE
						printk(KERN_DEBUG
						       "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
						       info->line);
#endif
						isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
					}
				}
			} else
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
				if (TTY_IS_FCLASS1(info)) {
					int cc = isdn_tty_handleDLEdown(info, m, c);

					if (info->vonline & 4) { /* ETX seen */
						isdn_ctrl c;

						c.command = ISDN_CMD_FAXCMD;
						c.driver = info->isdn_driver;
						c.arg = info->isdn_channel;
						c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
						c.parm.aux.subcmd = ETX;
						isdn_command(&c);
					}
					info->vonline = 0;
L
Linus Torvalds 已提交
1199
#ifdef ISDN_DEBUG_MODEM_VOICE
1200
					printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
L
Linus Torvalds 已提交
1201
#endif
1202 1203
					info->xmit_count += cc;
				} else
L
Linus Torvalds 已提交
1204
#endif
1205
					info->xmit_count += c;
L
Linus Torvalds 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
		} else {
			info->msr |= UART_MSR_CTS;
			info->lsr |= UART_LSR_TEMT;
			if (info->dialing) {
				info->dialing = 0;
#ifdef ISDN_DEBUG_MODEM_HUP
				printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
#endif
				isdn_tty_modem_result(RESULT_NO_CARRIER, info);
				isdn_tty_modem_hup(info, 1);
			} else
				c = isdn_tty_edit_at(buf, c, info);
		}
		buf += c;
		count -= c;
		total += c;
	}
	atomic_dec(&info->xmit_lock);
1224
	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
		if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
			isdn_tty_senddown(info);
			isdn_tty_tint(info);
		}
		isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
	}
	return total;
}

static int
isdn_tty_write_room(struct tty_struct *tty)
{
	modem_info *info = (modem_info *) tty->driver_data;
	int ret;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write_room"))
		return 0;
	if (!info->online)
		return info->xmit_size;
	ret = info->xmit_size - info->xmit_count;
	return (ret < 0) ? 0 : ret;
}

static int
isdn_tty_chars_in_buffer(struct tty_struct *tty)
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_chars_in_buffer"))
		return 0;
	if (!info->online)
		return 0;
	return (info->xmit_count);
}

static void
isdn_tty_flush_buffer(struct tty_struct *tty)
{
	modem_info *info;

	if (!tty) {
		return;
	}
	info = (modem_info *) tty->driver_data;
	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
		return;
	}
	isdn_tty_cleanup_xmit(info);
	info->xmit_count = 0;
	tty_wakeup(tty);
}

static void
isdn_tty_flush_chars(struct tty_struct *tty)
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
		return;
1284
	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
L
Linus Torvalds 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
		isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
}

/*
 * ------------------------------------------------------------
 * isdn_tty_throttle()
 *
 * This routine is called by the upper-layer tty layer to signal that
 * incoming characters should be throttled.
 * ------------------------------------------------------------
 */
static void
isdn_tty_throttle(struct tty_struct *tty)
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_throttle"))
		return;
	if (I_IXOFF(tty))
		info->x_char = STOP_CHAR(tty);
	info->mcr &= ~UART_MCR_RTS;
}

static void
isdn_tty_unthrottle(struct tty_struct *tty)
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_unthrottle"))
		return;
	if (I_IXOFF(tty)) {
		if (info->x_char)
			info->x_char = 0;
		else
			info->x_char = START_CHAR(tty);
	}
	info->mcr |= UART_MCR_RTS;
}

/*
 * ------------------------------------------------------------
 * isdn_tty_ioctl() and friends
 * ------------------------------------------------------------
 */

/*
 * isdn_tty_get_lsr_info - get line status register info
 *
 * Purpose: Let user call ioctl() to get info when the UART physically
 *          is emptied.  On bus types like RS485, the transmitter must
 *          release the bus after transmitting. This must be done when
 *          the transmit shift register is empty, not be done when the
 *          transmit holding register is empty.  This functionality
 *          allows RS485 driver to be written in user space.
 */
static int
1341
isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
L
Linus Torvalds 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
{
	u_char status;
	uint result;

	status = info->lsr;
	result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
	return put_user(result, value);
}


static int
1353
isdn_tty_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
1354 1355 1356 1357
{
	modem_info *info = (modem_info *) tty->driver_data;
	u_char control, status;

1358
	if (isdn_tty_paranoia_check(info, tty->name, __func__))
L
Linus Torvalds 已提交
1359 1360 1361 1362
		return -ENODEV;
	if (tty->flags & (1 << TTY_IO_ERROR))
		return -EIO;

A
Arnd Bergmann 已提交
1363
	mutex_lock(&modem_info_mutex);
L
Linus Torvalds 已提交
1364 1365 1366 1367 1368 1369
#ifdef ISDN_DEBUG_MODEM_IOCTL
	printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
#endif

	control = info->mcr;
	status = info->msr;
A
Arnd Bergmann 已提交
1370
	mutex_unlock(&modem_info_mutex);
L
Linus Torvalds 已提交
1371
	return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1372 1373 1374 1375 1376
		| ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
		| ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
		| ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
		| ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
		| ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
L
Linus Torvalds 已提交
1377 1378 1379
}

static int
1380
isdn_tty_tiocmset(struct tty_struct *tty,
1381
		  unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
1382 1383 1384
{
	modem_info *info = (modem_info *) tty->driver_data;

1385
	if (isdn_tty_paranoia_check(info, tty->name, __func__))
L
Linus Torvalds 已提交
1386 1387 1388 1389 1390 1391 1392 1393
		return -ENODEV;
	if (tty->flags & (1 << TTY_IO_ERROR))
		return -EIO;

#ifdef ISDN_DEBUG_MODEM_IOCTL
	printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
#endif

A
Arnd Bergmann 已提交
1394
	mutex_lock(&modem_info_mutex);
L
Linus Torvalds 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
	if (set & TIOCM_RTS)
		info->mcr |= UART_MCR_RTS;
	if (set & TIOCM_DTR) {
		info->mcr |= UART_MCR_DTR;
		isdn_tty_modem_ncarrier(info);
	}

	if (clear & TIOCM_RTS)
		info->mcr &= ~UART_MCR_RTS;
	if (clear & TIOCM_DTR) {
		info->mcr &= ~UART_MCR_DTR;
		if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
			isdn_tty_modem_reset_regs(info, 0);
#ifdef ISDN_DEBUG_MODEM_HUP
			printk(KERN_DEBUG "Mhup in TIOCMSET\n");
#endif
			if (info->online)
				info->ncarrier = 1;
			isdn_tty_modem_hup(info, 1);
		}
	}
A
Arnd Bergmann 已提交
1416
	mutex_unlock(&modem_info_mutex);
L
Linus Torvalds 已提交
1417 1418 1419 1420
	return 0;
}

static int
1421
isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
L
Linus Torvalds 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430
{
	modem_info *info = (modem_info *) tty->driver_data;
	int retval;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
		return -ENODEV;
	if (tty->flags & (1 << TTY_IO_ERROR))
		return -EIO;
	switch (cmd) {
1431
	case TCSBRK:   /* SVID version: non-zero arg --> no break */
L
Linus Torvalds 已提交
1432
#ifdef ISDN_DEBUG_MODEM_IOCTL
1433 1434 1435 1436 1437 1438 1439 1440
		printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
#endif
		retval = tty_check_change(tty);
		if (retval)
			return retval;
		tty_wait_until_sent(tty, 0);
		return 0;
	case TCSBRKP:  /* support for POSIX tcsendbreak() */
L
Linus Torvalds 已提交
1441
#ifdef ISDN_DEBUG_MODEM_IOCTL
1442 1443 1444 1445 1446 1447 1448 1449
		printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
#endif
		retval = tty_check_change(tty);
		if (retval)
			return retval;
		tty_wait_until_sent(tty, 0);
		return 0;
	case TIOCSERGETLSR:	/* Get line status register */
L
Linus Torvalds 已提交
1450
#ifdef ISDN_DEBUG_MODEM_IOCTL
1451
		printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
L
Linus Torvalds 已提交
1452
#endif
1453 1454
		return isdn_tty_get_lsr_info(info, (uint __user *) arg);
	default:
L
Linus Torvalds 已提交
1455
#ifdef ISDN_DEBUG_MODEM_IOCTL
1456
		printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
L
Linus Torvalds 已提交
1457
#endif
1458
		return -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1459 1460 1461 1462 1463
	}
	return 0;
}

static void
A
Alan Cox 已提交
1464
isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (!old_termios)
		isdn_tty_change_speed(info);
	else {
A
Alan Cox 已提交
1471 1472 1473
		if (tty->termios->c_cflag == old_termios->c_cflag &&
		    tty->termios->c_ispeed == old_termios->c_ispeed &&
		    tty->termios->c_ospeed == old_termios->c_ospeed)
L
Linus Torvalds 已提交
1474 1475 1476
			return;
		isdn_tty_change_speed(info);
		if ((old_termios->c_cflag & CRTSCTS) &&
A
Alan Cox 已提交
1477
		    !(tty->termios->c_cflag & CRTSCTS))
L
Linus Torvalds 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
			tty->hw_stopped = 0;
	}
}

/*
 * ------------------------------------------------------------
 * isdn_tty_open() and friends
 * ------------------------------------------------------------
 */
static int
1488
isdn_tty_block_til_ready(struct tty_struct *tty, struct file *filp, modem_info *info)
L
Linus Torvalds 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
{
	DECLARE_WAITQUEUE(wait, NULL);
	int do_clocal = 0;
	int retval;

	/*
	 * If the device is in the middle of being closed, then block
	 * until it's done, and then try again.
	 */
	if (tty_hung_up_p(filp) ||
J
Jiri Slaby 已提交
1499 1500
	    (info->port.flags & ASYNC_CLOSING)) {
		if (info->port.flags & ASYNC_CLOSING)
1501
			interruptible_sleep_on(&info->port.close_wait);
L
Linus Torvalds 已提交
1502
#ifdef MODEM_DO_RESTART
J
Jiri Slaby 已提交
1503
		if (info->port.flags & ASYNC_HUP_NOTIFY)
L
Linus Torvalds 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
			return -EAGAIN;
		else
			return -ERESTARTSYS;
#else
		return -EAGAIN;
#endif
	}
	/*
	 * If non-blocking mode is set, then make the check up front
	 * and then exit.
	 */
	if ((filp->f_flags & O_NONBLOCK) ||
	    (tty->flags & (1 << TTY_IO_ERROR))) {
J
Jiri Slaby 已提交
1517
		info->port.flags |= ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
1518 1519
		return 0;
	}
J
Jiri Slaby 已提交
1520 1521
	if (tty->termios->c_cflag & CLOCAL)
		do_clocal = 1;
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526 1527 1528 1529
	/*
	 * Block waiting for the carrier detect and the line to become
	 * free (i.e., not in use by the callout).  While we are in
	 * this loop, info->count is dropped by one, so that
	 * isdn_tty_close() knows when to free things.  We restore it upon
	 * exit, either normal or abnormal.
	 */
	retval = 0;
1530
	add_wait_queue(&info->port.open_wait, &wait);
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "isdn_tty_block_til_ready before block: ttyi%d, count = %d\n",
	       info->line, info->count);
#endif
	if (!(tty_hung_up_p(filp)))
		info->count--;
	info->blocked_open++;
	while (1) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (tty_hung_up_p(filp) ||
J
Jiri Slaby 已提交
1541
		    !(info->port.flags & ASYNC_INITIALIZED)) {
L
Linus Torvalds 已提交
1542
#ifdef MODEM_DO_RESTART
J
Jiri Slaby 已提交
1543
			if (info->port.flags & ASYNC_HUP_NOTIFY)
L
Linus Torvalds 已提交
1544 1545 1546 1547 1548 1549 1550 1551
				retval = -EAGAIN;
			else
				retval = -ERESTARTSYS;
#else
			retval = -EAGAIN;
#endif
			break;
		}
J
Jiri Slaby 已提交
1552
		if (!(info->port.flags & ASYNC_CLOSING) &&
L
Linus Torvalds 已提交
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
		    (do_clocal || (info->msr & UART_MSR_DCD))) {
			break;
		}
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
#ifdef ISDN_DEBUG_MODEM_OPEN
		printk(KERN_DEBUG "isdn_tty_block_til_ready blocking: ttyi%d, count = %d\n",
		       info->line, info->count);
#endif
		schedule();
	}
	current->state = TASK_RUNNING;
1567
	remove_wait_queue(&info->port.open_wait, &wait);
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576
	if (!tty_hung_up_p(filp))
		info->count++;
	info->blocked_open--;
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "isdn_tty_block_til_ready after blocking: ttyi%d, count = %d\n",
	       info->line, info->count);
#endif
	if (retval)
		return retval;
J
Jiri Slaby 已提交
1577
	info->port.flags |= ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
	return 0;
}

/*
 * This routine is called whenever a serial port is opened.  It
 * enables interrupts for a serial port, linking in its async structure into
 * the IRQ chain.   It also performs the serial-specific
 * initialization for the tty structure.
 */
static int
isdn_tty_open(struct tty_struct *tty, struct file *filp)
{
	modem_info *info;
1591
	int retval;
L
Linus Torvalds 已提交
1592

1593
	info = &dev->mdm.info[tty->index];
L
Linus Torvalds 已提交
1594 1595 1596
	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_open"))
		return -ENODEV;
#ifdef ISDN_DEBUG_MODEM_OPEN
1597
	printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602
	       info->count);
#endif
	info->count++;
	tty->driver_data = info;
	info->tty = tty;
J
Jiri Slaby 已提交
1603
	tty->port = &info->port;
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	/*
	 * Start up serial port
	 */
	retval = isdn_tty_startup(info);
	if (retval) {
#ifdef ISDN_DEBUG_MODEM_OPEN
		printk(KERN_DEBUG "isdn_tty_open return after startup\n");
#endif
		return retval;
	}
	retval = isdn_tty_block_til_ready(tty, filp, info);
	if (retval) {
#ifdef ISDN_DEBUG_MODEM_OPEN
		printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
#endif
		return retval;
	}
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
#endif
	dev->modempoll++;
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "isdn_tty_open normal exit\n");
#endif
	return 0;
}

static void
isdn_tty_close(struct tty_struct *tty, struct file *filp)
{
	modem_info *info = (modem_info *) tty->driver_data;
	ulong timeout;

	if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
		return;
	if (tty_hung_up_p(filp)) {
#ifdef ISDN_DEBUG_MODEM_OPEN
		printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
#endif
		return;
	}
	if ((tty->count == 1) && (info->count != 1)) {
		/*
		 * Uh, oh.  tty->count is 1, which means that the tty
		 * structure will be freed.  Info->count should always
		 * be one in these conditions.  If it's greater than
		 * one, we've got real problems, since it means the
		 * serial port won't be shutdown.
		 */
		printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
		       "info->count is %d\n", info->count);
		info->count = 1;
	}
	if (--info->count < 0) {
		printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
		       info->line, info->count);
		info->count = 0;
	}
	if (info->count) {
#ifdef ISDN_DEBUG_MODEM_OPEN
		printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
#endif
		return;
	}
J
Jiri Slaby 已提交
1668
	info->port.flags |= ASYNC_CLOSING;
L
Linus Torvalds 已提交
1669 1670 1671 1672 1673 1674 1675 1676

	tty->closing = 1;
	/*
	 * At this point we stop accepting input.  To do this, we
	 * disable the receive line status interrupts, and tell the
	 * interrupt driver to stop checking the data ready bit in the
	 * line status register.
	 */
J
Jiri Slaby 已提交
1677
	if (info->port.flags & ASYNC_INITIALIZED) {
1678
		tty_wait_until_sent_from_close(tty, 3000);	/* 30 seconds timeout */
L
Linus Torvalds 已提交
1679 1680 1681 1682 1683 1684 1685
		/*
		 * Before we drop DTR, make sure the UART transmitter
		 * has completely drained; this is especially
		 * important if there is a transmit FIFO!
		 */
		timeout = jiffies + HZ;
		while (!(info->lsr & UART_LSR_TEMT)) {
1686
			schedule_timeout_interruptible(20);
1687
			if (time_after(jiffies, timeout))
L
Linus Torvalds 已提交
1688 1689 1690 1691 1692
				break;
		}
	}
	dev->modempoll--;
	isdn_tty_shutdown(info);
1693
	isdn_tty_flush_buffer(tty);
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699
	tty_ldisc_flush(tty);
	info->tty = NULL;
	info->ncarrier = 0;
	tty->closing = 0;
	if (info->blocked_open) {
		msleep_interruptible(500);
1700
		wake_up_interruptible(&info->port.open_wait);
L
Linus Torvalds 已提交
1701
	}
J
Jiri Slaby 已提交
1702
	info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1703
	wake_up_interruptible(&info->port.close_wait);
L
Linus Torvalds 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "isdn_tty_close normal exit\n");
#endif
}

/*
 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
 */
static void
isdn_tty_hangup(struct tty_struct *tty)
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
		return;
	isdn_tty_shutdown(info);
	info->count = 0;
J
Jiri Slaby 已提交
1721
	info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
1722
	info->tty = NULL;
1723
	wake_up_interruptible(&info->port.open_wait);
L
Linus Torvalds 已提交
1724 1725 1726 1727 1728
}

/* This routine initializes all emulator-data.
 */
static void
1729
isdn_tty_reset_profile(atemu *m)
L
Linus Torvalds 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
{
	m->profile[0] = 0;
	m->profile[1] = 0;
	m->profile[2] = 43;
	m->profile[3] = 13;
	m->profile[4] = 10;
	m->profile[5] = 8;
	m->profile[6] = 3;
	m->profile[7] = 60;
	m->profile[8] = 2;
	m->profile[9] = 6;
	m->profile[10] = 7;
	m->profile[11] = 70;
	m->profile[12] = 0x45;
	m->profile[13] = 4;
	m->profile[14] = ISDN_PROTO_L2_X75I;
	m->profile[15] = ISDN_PROTO_L3_TRANS;
	m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
	m->profile[17] = ISDN_MODEM_WINSIZE;
	m->profile[18] = 4;
	m->profile[19] = 0;
	m->profile[20] = 0;
	m->profile[23] = 0;
	m->pmsn[0] = '\0';
	m->plmsn[0] = '\0';
}

#ifdef CONFIG_ISDN_AUDIO
static void
1759
isdn_tty_modem_reset_vpar(atemu *m)
L
Linus Torvalds 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
{
	m->vpar[0] = 2;         /* Voice-device            (2 = phone line) */
	m->vpar[1] = 0;         /* Silence detection level (0 = none      ) */
	m->vpar[2] = 70;        /* Silence interval        (7 sec.        ) */
	m->vpar[3] = 2;         /* Compression type        (1 = ADPCM-2   ) */
	m->vpar[4] = 0;         /* DTMF detection level    (0 = softcode  ) */
	m->vpar[5] = 8;         /* DTMF interval           (8 * 5 ms.     ) */
}
#endif

#ifdef CONFIG_ISDN_TTY_FAX
static void
1772
isdn_tty_modem_reset_faxpar(modem_info *info)
L
Linus Torvalds 已提交
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
{
	T30_s *f = info->fax;

	f->code = 0;
	f->phase = ISDN_FAX_PHASE_IDLE;
	f->direction = 0;
	f->resolution = 1;	/* fine */
	f->rate = 5;		/* 14400 bit/s */
	f->width = 0;
	f->length = 0;
	f->compression = 0;
	f->ecm = 0;
	f->binary = 0;
	f->scantime = 0;
	memset(&f->id[0], 32, FAXIDLEN - 1);
	f->id[FAXIDLEN - 1] = 0;
	f->badlin = 0;
	f->badmul = 0;
	f->bor = 0;
	f->nbc = 0;
	f->cq = 0;
	f->cr = 0;
	f->ctcrty = 0;
	f->minsp = 0;
	f->phcto = 30;
	f->rel = 0;
	memset(&f->pollid[0], 32, FAXIDLEN - 1);
	f->pollid[FAXIDLEN - 1] = 0;
}
#endif

static void
1805
isdn_tty_modem_reset_regs(modem_info *info, int force)
L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
{
	atemu *m = &info->emu;
	if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
		memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
		memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
		memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
		info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
	}
#ifdef CONFIG_ISDN_AUDIO
	isdn_tty_modem_reset_vpar(m);
#endif
#ifdef CONFIG_ISDN_TTY_FAX
	isdn_tty_modem_reset_faxpar(info);
#endif
	m->mdmcmdl = 0;
}

static void
1824
modem_write_profile(atemu *m)
L
Linus Torvalds 已提交
1825 1826 1827 1828 1829 1830 1831 1832
{
	memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
	memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
	memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
	if (dev->profd)
		send_sig(SIGIO, dev->profd, 1);
}

J
Jeff Dike 已提交
1833
static const struct tty_operations modem_ops = {
1834
	.open = isdn_tty_open,
L
Linus Torvalds 已提交
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
	.close = isdn_tty_close,
	.write = isdn_tty_write,
	.flush_chars = isdn_tty_flush_chars,
	.write_room = isdn_tty_write_room,
	.chars_in_buffer = isdn_tty_chars_in_buffer,
	.flush_buffer = isdn_tty_flush_buffer,
	.ioctl = isdn_tty_ioctl,
	.throttle = isdn_tty_throttle,
	.unthrottle = isdn_tty_unthrottle,
	.set_termios = isdn_tty_set_termios,
	.hangup = isdn_tty_hangup,
	.tiocmget = isdn_tty_tiocmget,
	.tiocmset = isdn_tty_tiocmset,
};

int
isdn_tty_modem_init(void)
{
	isdn_modem_t	*m;
	int		i, retval;
	modem_info	*info;

	m = &dev->mdm;
	m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
	if (!m->tty_modem)
		return -ENOMEM;
	m->tty_modem->name = "ttyI";
	m->tty_modem->major = ISDN_TTY_MAJOR;
	m->tty_modem->minor_start = 0;
	m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
	m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
	m->tty_modem->init_termios = tty_std_termios;
	m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1868
	m->tty_modem->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
L
Linus Torvalds 已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
	m->tty_modem->driver_name = "isdn_tty";
	tty_set_operations(m->tty_modem, &modem_ops);
	retval = tty_register_driver(m->tty_modem);
	if (retval) {
		printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
		goto err;
	}
	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
		info = &m->info[i];
#ifdef CONFIG_ISDN_TTY_FAX
		if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
			printk(KERN_ERR "Could not allocate fax t30-buffer\n");
			retval = -ENOMEM;
			goto err_unregister;
		}
#endif
J
Jiri Slaby 已提交
1885
		tty_port_init(&info->port);
L
Linus Torvalds 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
		spin_lock_init(&info->readlock);
		sprintf(info->last_cause, "0000");
		sprintf(info->last_num, "none");
		info->last_dir = 0;
		info->last_lhup = 1;
		info->last_l2 = -1;
		info->last_si = 0;
		isdn_tty_reset_profile(&info->emu);
		isdn_tty_modem_reset_regs(info, 1);
		info->magic = ISDN_ASYNC_MAGIC;
		info->line = i;
		info->tty = NULL;
		info->x_char = 0;
		info->count = 0;
		info->blocked_open = 0;
		info->isdn_driver = -1;
		info->isdn_channel = -1;
		info->drv_index = -1;
		info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
		init_timer(&info->nc_timer);
		info->nc_timer.function = isdn_tty_modem_do_ncarrier;
		info->nc_timer.data = (unsigned long) info;
		skb_queue_head_init(&info->xmit_queue);
#ifdef CONFIG_ISDN_AUDIO
		skb_queue_head_init(&info->dtmf_queue);
#endif
		if (!(info->xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5, GFP_KERNEL))) {
			printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
			retval = -ENOMEM;
			goto err_unregister;
		}
		/* Make room for T.70 header */
		info->xmit_buf += 4;
	}
	return 0;
err_unregister:
	for (i--; i >= 0; i--) {
		info = &m->info[i];
#ifdef CONFIG_ISDN_TTY_FAX
		kfree(info->fax);
#endif
		kfree(info->xmit_buf - 4);
	}
	tty_unregister_driver(m->tty_modem);
1930
err:
L
Linus Torvalds 已提交
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
	put_tty_driver(m->tty_modem);
	m->tty_modem = NULL;
	return retval;
}

void
isdn_tty_exit(void)
{
	modem_info *info;
	int i;

	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
		info = &dev->mdm.info[i];
		isdn_tty_cleanup_xmit(info);
#ifdef CONFIG_ISDN_TTY_FAX
		kfree(info->fax);
#endif
		kfree(info->xmit_buf - 4);
	}
	tty_unregister_driver(dev->mdm.tty_modem);
	put_tty_driver(dev->mdm.tty_modem);
	dev->mdm.tty_modem = NULL;
}


/*
 * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
 *      match the MSN against the MSNs (glob patterns) defined for tty_emulator,
 *      and return 0 for match, 1 for no match, 2 if MSN could match if longer.
 */

static int
isdn_tty_match_icall(char *cid, atemu *emu, int di)
{
#ifdef ISDN_DEBUG_MODEM_ICALL
	printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
	       emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
	       emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
#endif
	if (strlen(emu->lmsn)) {
		char *p = emu->lmsn;
		char *q;
		int  tmp;
		int  ret = 0;

		while (1) {
			if ((q = strchr(p, ';')))
				*q = '\0';
			if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
				ret = tmp;
#ifdef ISDN_DEBUG_MODEM_ICALL
			printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
			       p, isdn_map_eaz2msn(emu->msn, di), tmp);
#endif
			if (q) {
				*q = ';';
				p = q;
				p++;
			}
			if (!tmp)
				return 0;
			if (!q)
				break;
		}
		return ret;
	} else {
		int tmp;
		tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
#ifdef ISDN_DEBUG_MODEM_ICALL
2000 2001
		printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
		       isdn_map_eaz2msn(emu->msn, di), tmp);
L
Linus Torvalds 已提交
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
#endif
		return tmp;
	}
}

/*
 * An incoming call-request has arrived.
 * Search the tty-devices for an appropriate device and bind
 * it to the ISDN-Channel.
 * Return:
 *
 *  0 = No matching device found.
 *  1 = A matching device found.
 *  3 = No match found, but eventually would match, if
 *      CID is longer.
 */
int
isdn_tty_find_icall(int di, int ch, setup_parm *setup)
{
	char *eaz;
	int i;
	int wret;
	int idx;
	int si1;
	int si2;
	char *nr;
	ulong flags;

	if (!setup->phone[0]) {
		nr = "0";
		printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
	} else
		nr = setup->phone;
	si1 = (int) setup->si1;
	si2 = (int) setup->si2;
	if (!setup->eazmsn[0]) {
		printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
		eaz = "0";
	} else
		eaz = setup->eazmsn;
#ifdef ISDN_DEBUG_MODEM_ICALL
	printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
#endif
	wret = 0;
	spin_lock_irqsave(&dev->lock, flags);
	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
		modem_info *info = &dev->mdm.info[i];

2050 2051
		if (info->count == 0)
			continue;
L
Linus Torvalds 已提交
2052 2053 2054 2055 2056 2057
		if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) &&  /* SI1 is matching */
		    (info->emu.mdmreg[REG_SI2] == si2))	{         /* SI2 is matching */
			idx = isdn_dc2minor(di, ch);
#ifdef ISDN_DEBUG_MODEM_ICALL
			printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
			printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
J
Jiri Slaby 已提交
2058 2059
			       info->port.flags, info->isdn_driver,
			       info->isdn_channel, dev->usage[idx]);
L
Linus Torvalds 已提交
2060 2061 2062
#endif
			if (
#ifndef FIX_FILE_TRANSFER
J
Jiri Slaby 已提交
2063
				(info->port.flags & ASYNC_NORMAL_ACTIVE) &&
L
Linus Torvalds 已提交
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
#endif
				(info->isdn_driver == -1) &&
				(info->isdn_channel == -1) &&
				(USG_NONE(dev->usage[idx]))) {
				int matchret;

				if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
					wret = matchret;
				if (!matchret) {                  /* EAZ is matching */
					info->isdn_driver = di;
					info->isdn_channel = ch;
					info->drv_index = idx;
					dev->m_idx[idx] = info->line;
					dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
2078
					dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
L
Linus Torvalds 已提交
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
					strcpy(dev->num[idx], nr);
					strcpy(info->emu.cpn, eaz);
					info->emu.mdmreg[REG_SI1I] = si2bit[si1];
					info->emu.mdmreg[REG_PLAN] = setup->plan;
					info->emu.mdmreg[REG_SCREEN] = setup->screen;
					isdn_info_update();
					spin_unlock_irqrestore(&dev->lock, flags);
					printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
					       info->line);
					info->msr |= UART_MSR_RI;
					isdn_tty_modem_result(RESULT_RING, info);
					isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
					return 1;
				}
			}
		}
	}
	spin_unlock_irqrestore(&dev->lock, flags);
	printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
2098 2099
	       ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
	return (wret == 2) ? 3 : 0;
L
Linus Torvalds 已提交
2100 2101
}

J
Jiri Slaby 已提交
2102
#define TTY_IS_ACTIVE(info)	(info->port.flags & ASYNC_NORMAL_ACTIVE)
L
Linus Torvalds 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115

int
isdn_tty_stat_callback(int i, isdn_ctrl *c)
{
	int mi;
	modem_info *info;
	char *e;

	if (i < 0)
		return 0;
	if ((mi = dev->m_idx[i]) >= 0) {
		info = &dev->mdm.info[mi];
		switch (c->command) {
2116 2117 2118 2119 2120 2121 2122 2123
		case ISDN_STAT_CINF:
			printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
			info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
			if (e == (char *)c->parm.num)
				info->emu.charge = 0;

			break;
		case ISDN_STAT_BSENT:
L
Linus Torvalds 已提交
2124
#ifdef ISDN_TTY_STAT_DEBUG
2125
			printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
L
Linus Torvalds 已提交
2126
#endif
2127 2128 2129 2130 2131 2132 2133
			if ((info->isdn_driver == c->driver) &&
			    (info->isdn_channel == c->arg)) {
				info->msr |= UART_MSR_CTS;
				if (info->send_outstanding)
					if (!(--info->send_outstanding))
						info->lsr |= UART_LSR_TEMT;
				isdn_tty_tint(info);
L
Linus Torvalds 已提交
2134
				return 1;
2135 2136 2137
			}
			break;
		case ISDN_STAT_CAUSE:
L
Linus Torvalds 已提交
2138
#ifdef ISDN_TTY_STAT_DEBUG
2139 2140 2141 2142 2143 2144
			printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
#endif
			/* Signal cause to tty-device */
			strncpy(info->last_cause, c->parm.num, 5);
			return 1;
		case ISDN_STAT_DISPLAY:
L
Linus Torvalds 已提交
2145
#ifdef ISDN_TTY_STAT_DEBUG
2146
			printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
L
Linus Torvalds 已提交
2147
#endif
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
			/* Signal display to tty-device */
			if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
			    !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
				isdn_tty_at_cout("\r\n", info);
				isdn_tty_at_cout("DISPLAY: ", info);
				isdn_tty_at_cout(c->parm.display, info);
				isdn_tty_at_cout("\r\n", info);
			}
			return 1;
		case ISDN_STAT_DCONN:
#ifdef ISDN_TTY_STAT_DEBUG
			printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
#endif
			if (TTY_IS_ACTIVE(info)) {
				if (info->dialing == 1) {
					info->dialing = 2;
					return 1;
L
Linus Torvalds 已提交
2165
				}
2166 2167 2168
			}
			break;
		case ISDN_STAT_DHUP:
L
Linus Torvalds 已提交
2169
#ifdef ISDN_TTY_STAT_DEBUG
2170
			printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
L
Linus Torvalds 已提交
2171
#endif
2172 2173 2174 2175 2176 2177
			if (TTY_IS_ACTIVE(info)) {
				if (info->dialing == 1)
					isdn_tty_modem_result(RESULT_BUSY, info);
				if (info->dialing > 1)
					isdn_tty_modem_result(RESULT_NO_CARRIER, info);
				info->dialing = 0;
L
Linus Torvalds 已提交
2178
#ifdef ISDN_DEBUG_MODEM_HUP
2179
				printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
L
Linus Torvalds 已提交
2180
#endif
2181 2182 2183 2184 2185
				isdn_tty_modem_hup(info, 0);
				return 1;
			}
			break;
		case ISDN_STAT_BCONN:
L
Linus Torvalds 已提交
2186
#ifdef ISDN_TTY_STAT_DEBUG
2187 2188 2189 2190 2191 2192 2193 2194
			printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
#endif
			/* Wake up any processes waiting
			 * for incoming call of this device when
			 * DCD follow the state of incoming carrier
			 */
			if (info->blocked_open &&
			    (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
2195
				wake_up_interruptible(&info->port.open_wait);
2196
			}
L
Linus Torvalds 已提交
2197

2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
			/* Schedule CONNECT-Message to any tty
			 * waiting for it and
			 * set DCD-bit of its modem-status.
			 */
			if (TTY_IS_ACTIVE(info) ||
			    (info->blocked_open && (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
				info->msr |= UART_MSR_DCD;
				info->emu.charge = 0;
				if (info->dialing & 0xf)
					info->last_dir = 1;
				else
					info->last_dir = 0;
				info->dialing = 0;
				info->rcvsched = 1;
				if (USG_MODEM(dev->usage[i])) {
					if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
						strcpy(info->emu.connmsg, c->parm.num);
						isdn_tty_modem_result(RESULT_CONNECT, info);
					} else
						isdn_tty_modem_result(RESULT_CONNECT64000, info);
L
Linus Torvalds 已提交
2218
				}
2219 2220 2221 2222 2223 2224
				if (USG_VOICE(dev->usage[i]))
					isdn_tty_modem_result(RESULT_VCON, info);
				return 1;
			}
			break;
		case ISDN_STAT_BHUP:
L
Linus Torvalds 已提交
2225
#ifdef ISDN_TTY_STAT_DEBUG
2226
			printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
L
Linus Torvalds 已提交
2227
#endif
2228
			if (TTY_IS_ACTIVE(info)) {
L
Linus Torvalds 已提交
2229
#ifdef ISDN_DEBUG_MODEM_HUP
2230
				printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
L
Linus Torvalds 已提交
2231
#endif
2232 2233 2234 2235 2236
				isdn_tty_modem_hup(info, 0);
				return 1;
			}
			break;
		case ISDN_STAT_NODCH:
L
Linus Torvalds 已提交
2237
#ifdef ISDN_TTY_STAT_DEBUG
2238 2239 2240 2241 2242 2243 2244 2245 2246
			printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
#endif
			if (TTY_IS_ACTIVE(info)) {
				if (info->dialing) {
					info->dialing = 0;
					info->last_l2 = -1;
					info->last_si = 0;
					sprintf(info->last_cause, "0000");
					isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
L
Linus Torvalds 已提交
2247
				}
2248 2249 2250 2251 2252
				isdn_tty_modem_hup(info, 0);
				return 1;
			}
			break;
		case ISDN_STAT_UNLOAD:
L
Linus Torvalds 已提交
2253
#ifdef ISDN_TTY_STAT_DEBUG
2254
			printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
L
Linus Torvalds 已提交
2255
#endif
2256 2257 2258 2259 2260
			for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
				info = &dev->mdm.info[i];
				if (info->isdn_driver == c->driver) {
					if (info->online)
						isdn_tty_modem_hup(info, 1);
L
Linus Torvalds 已提交
2261
				}
2262 2263
			}
			return 1;
L
Linus Torvalds 已提交
2264
#ifdef CONFIG_ISDN_TTY_FAX
2265 2266 2267 2268 2269
		case ISDN_STAT_FAXIND:
			if (TTY_IS_ACTIVE(info)) {
				isdn_tty_fax_command(info, c);
			}
			break;
L
Linus Torvalds 已提交
2270 2271
#endif
#ifdef CONFIG_ISDN_AUDIO
2272 2273 2274 2275 2276 2277
		case ISDN_STAT_AUDIO:
			if (TTY_IS_ACTIVE(info)) {
				switch (c->parm.num[0]) {
				case ISDN_AUDIO_DTMF:
					if (info->vonline) {
						isdn_audio_put_dle_code(info,
L
Linus Torvalds 已提交
2278 2279
									c->parm.num[1]);
					}
2280
					break;
L
Linus Torvalds 已提交
2281
				}
2282 2283
			}
			break;
L
Linus Torvalds 已提交
2284 2285 2286 2287 2288 2289 2290 2291
#endif
		}
	}
	return 0;
}

/*********************************************************************
 Modem-Emulator-Routines
2292
*********************************************************************/
L
Linus Torvalds 已提交
2293

2294
#define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
L
Linus Torvalds 已提交
2295 2296 2297 2298 2299 2300

/*
 * Put a message from the AT-emulator into receive-buffer of tty,
 * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
 */
void
2301
isdn_tty_at_cout(char *msg, modem_info *info)
L
Linus Torvalds 已提交
2302 2303 2304 2305 2306 2307 2308 2309
{
	struct tty_struct *tty;
	atemu *m = &info->emu;
	char *p;
	char c;
	u_long flags;
	struct sk_buff *skb = NULL;
	char *sp = NULL;
2310
	int l;
L
Linus Torvalds 已提交
2311 2312 2313 2314 2315

	if (!msg) {
		printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
		return;
	}
2316 2317 2318

	l = strlen(msg);

L
Linus Torvalds 已提交
2319 2320
	spin_lock_irqsave(&info->readlock, flags);
	tty = info->tty;
J
Jiri Slaby 已提交
2321
	if ((info->port.flags & ASYNC_CLOSING) || (!tty)) {
L
Linus Torvalds 已提交
2322 2323 2324 2325
		spin_unlock_irqrestore(&info->readlock, flags);
		return;
	}

A
Alan Cox 已提交
2326 2327
	/* use queue instead of direct, if online and */
	/* data is in queue or buffer is full */
2328
	if (info->online && ((tty_buffer_request_room(tty, l) < l) ||
2329
			     !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
A
Alan Cox 已提交
2330
		skb = alloc_skb(l, GFP_ATOMIC);
L
Linus Torvalds 已提交
2331 2332 2333 2334
		if (!skb) {
			spin_unlock_irqrestore(&info->readlock, flags);
			return;
		}
A
Alan Cox 已提交
2335
		sp = skb_put(skb, l);
L
Linus Torvalds 已提交
2336 2337 2338 2339 2340 2341 2342 2343
#ifdef CONFIG_ISDN_AUDIO
		ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
		ISDN_AUDIO_SKB_LOCK(skb) = 0;
#endif
	}

	for (p = msg; *p; p++) {
		switch (*p) {
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
		case '\r':
			c = m->mdmreg[REG_CR];
			break;
		case '\n':
			c = m->mdmreg[REG_LF];
			break;
		case '\b':
			c = m->mdmreg[REG_BS];
			break;
		default:
			c = *p;
L
Linus Torvalds 已提交
2355 2356 2357 2358
		}
		if (skb) {
			*sp++ = c;
		} else {
2359
			if (tty_insert_flip_char(tty, c, TTY_NORMAL) == 0)
L
Linus Torvalds 已提交
2360 2361 2362 2363 2364 2365 2366 2367
				break;
		}
	}
	if (skb) {
		__skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
		dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
		spin_unlock_irqrestore(&info->readlock, flags);
		/* Schedule dequeuing */
A
Alan Cox 已提交
2368
		if (dev->modempoll && info->rcvsched)
L
Linus Torvalds 已提交
2369 2370 2371 2372
			isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);

	} else {
		spin_unlock_irqrestore(&info->readlock, flags);
A
Alan Cox 已提交
2373
		tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
2374 2375 2376 2377 2378 2379 2380
	}
}

/*
 * Perform ATH Hangup
 */
static void
2381
isdn_tty_on_hook(modem_info *info)
L
Linus Torvalds 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
{
	if (info->isdn_channel >= 0) {
#ifdef ISDN_DEBUG_MODEM_HUP
		printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
#endif
		isdn_tty_modem_hup(info, 1);
	}
}

static void
isdn_tty_off_hook(void)
{
	printk(KERN_DEBUG "isdn_tty_off_hook\n");
}

2397 2398
#define PLUSWAIT1 (HZ / 2)      /* 0.5 sec. */
#define PLUSWAIT2 (HZ * 3 / 2)  /* 1.5 sec */
L
Linus Torvalds 已提交
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411

/*
 * Check Buffer for Modem-escape-sequence, activate timer-callback to
 * isdn_tty_modem_escape() if sequence found.
 *
 * Parameters:
 *   p          pointer to databuffer
 *   plus       escape-character
 *   count      length of buffer
 *   pluscount  count of valid escape-characters so far
 *   lastplus   timestamp of last character
 */
static void
2412
isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
L
Linus Torvalds 已提交
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
		   u_long *lastplus)
{
	if (plus > 127)
		return;
	if (count > 3) {
		p += count - 3;
		count = 3;
		*pluscount = 0;
	}
	while (count > 0) {
		if (*(p++) == plus) {
			if ((*pluscount)++) {
				/* Time since last '+' > 0.5 sec. ? */
				if (time_after(jiffies, *lastplus + PLUSWAIT1))
					*pluscount = 1;
			} else {
				/* Time since last non-'+' < 1.5 sec. ? */
				if (time_before(jiffies, *lastplus + PLUSWAIT2))
					*pluscount = 0;
			}
			if ((*pluscount == 3) && (count == 1))
				isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
			if (*pluscount > 3)
				*pluscount = 1;
		} else
			*pluscount = 0;
		*lastplus = jiffies;
		count--;
	}
}

/*
 * Return result of AT-emulator to tty-receive-buffer, depending on
 * modem-register 12, bit 0 and 1.
 * For CONNECT-messages also switch to online-mode.
 * For RING-message handle auto-ATA if register 0 != 0
 */

static void
2452
isdn_tty_modem_result(int code, modem_info *info)
L
Linus Torvalds 已提交
2453 2454 2455
{
	atemu *m = &info->emu;
	static char *msg[] =
2456 2457 2458 2459
		{"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
		 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
		 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
	char s[ISDN_MSNLEN + 10];
L
Linus Torvalds 已提交
2460 2461

	switch (code) {
2462 2463 2464 2465 2466 2467 2468
	case RESULT_RING:
		m->mdmreg[REG_RINGCNT]++;
		if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
			/* Automatically accept incoming call */
			isdn_tty_cmd_ATA(info);
		break;
	case RESULT_NO_CARRIER:
L
Linus Torvalds 已提交
2469
#ifdef ISDN_DEBUG_MODEM_HUP
2470
		printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
J
Jiri Slaby 已提交
2471
		       (info->port.flags & ASYNC_CLOSING),
2472 2473 2474 2475 2476
		       (!info->tty));
#endif
		m->mdmreg[REG_RINGCNT] = 0;
		del_timer(&info->nc_timer);
		info->ncarrier = 0;
J
Jiri Slaby 已提交
2477
		if ((info->port.flags & ASYNC_CLOSING) || (!info->tty))
2478
			return;
2479

L
Linus Torvalds 已提交
2480
#ifdef CONFIG_ISDN_AUDIO
2481
		if (info->vonline & 1) {
L
Linus Torvalds 已提交
2482
#ifdef ISDN_DEBUG_MODEM_VOICE
2483 2484
			printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
			       info->line);
L
Linus Torvalds 已提交
2485
#endif
2486 2487 2488 2489
			/* voice-recording, add DLE-ETX */
			isdn_tty_at_cout("\020\003", info);
		}
		if (info->vonline & 2) {
L
Linus Torvalds 已提交
2490
#ifdef ISDN_DEBUG_MODEM_VOICE
2491 2492
			printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
			       info->line);
L
Linus Torvalds 已提交
2493
#endif
2494 2495 2496
			/* voice-playing, add DLE-DC4 */
			isdn_tty_at_cout("\020\024", info);
		}
L
Linus Torvalds 已提交
2497
#endif
2498 2499 2500 2501 2502 2503 2504 2505
		break;
	case RESULT_CONNECT:
	case RESULT_CONNECT64000:
		sprintf(info->last_cause, "0000");
		if (!info->online)
			info->online = 2;
		break;
	case RESULT_VCON:
L
Linus Torvalds 已提交
2506
#ifdef ISDN_DEBUG_MODEM_VOICE
2507 2508
		printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
		       info->line);
L
Linus Torvalds 已提交
2509
#endif
2510 2511 2512 2513 2514
		sprintf(info->last_cause, "0000");
		if (!info->online)
			info->online = 1;
		break;
	} /* switch (code) */
L
Linus Torvalds 已提交
2515 2516 2517 2518 2519 2520 2521 2522 2523

	if (m->mdmreg[REG_RESP] & BIT_RESP) {
		/* Show results */
		if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
			/* Show numeric results only */
			sprintf(s, "\r\n%d\r\n", code);
			isdn_tty_at_cout(s, info);
		} else {
			if (code == RESULT_RING) {
2524 2525
				/* return if "show RUNG" and ringcounter>1 */
				if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
L
Linus Torvalds 已提交
2526
				    (m->mdmreg[REG_RINGCNT] > 1))
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
					return;
				/* print CID, _before_ _every_ ring */
				if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
					isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
					isdn_tty_at_cout(dev->num[info->drv_index], info);
					if (m->mdmreg[REG_CDN] & BIT_CDN) {
						isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
						isdn_tty_at_cout(info->emu.cpn, info);
					}
				}
L
Linus Torvalds 已提交
2537 2538 2539 2540
			}
			isdn_tty_at_cout("\r\n", info);
			isdn_tty_at_cout(msg[code], info);
			switch (code) {
2541 2542 2543 2544 2545
			case RESULT_CONNECT:
				switch (m->mdmreg[REG_L2PROT]) {
				case ISDN_PROTO_L2_MODEM:
					isdn_tty_at_cout(" ", info);
					isdn_tty_at_cout(m->connmsg, info);
L
Linus Torvalds 已提交
2546
					break;
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
				}
				break;
			case RESULT_RING:
				/* Append CPN, if enabled */
				if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
					sprintf(s, "/%s", m->cpn);
					isdn_tty_at_cout(s, info);
				}
				/* Print CID only once, _after_ 1st RING */
				if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
				    (m->mdmreg[REG_RINGCNT] == 1)) {
					isdn_tty_at_cout("\r\n", info);
					isdn_tty_at_cout("CALLER NUMBER: ", info);
					isdn_tty_at_cout(dev->num[info->drv_index], info);
					if (m->mdmreg[REG_CDN] & BIT_CDN) {
						isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
						isdn_tty_at_cout(info->emu.cpn, info);
L
Linus Torvalds 已提交
2564
					}
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584
				}
				break;
			case RESULT_NO_CARRIER:
			case RESULT_NO_DIALTONE:
			case RESULT_BUSY:
			case RESULT_NO_ANSWER:
				m->mdmreg[REG_RINGCNT] = 0;
				/* Append Cause-Message if enabled */
				if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
					sprintf(s, "/%s", info->last_cause);
					isdn_tty_at_cout(s, info);
				}
				break;
			case RESULT_CONNECT64000:
				/* Append Protocol to CONNECT message */
				switch (m->mdmreg[REG_L2PROT]) {
				case ISDN_PROTO_L2_X75I:
				case ISDN_PROTO_L2_X75UI:
				case ISDN_PROTO_L2_X75BUI:
					isdn_tty_at_cout("/X.75", info);
L
Linus Torvalds 已提交
2585
					break;
2586 2587
				case ISDN_PROTO_L2_HDLC:
					isdn_tty_at_cout("/HDLC", info);
L
Linus Torvalds 已提交
2588
					break;
2589 2590 2591 2592 2593 2594 2595 2596
				case ISDN_PROTO_L2_V11096:
					isdn_tty_at_cout("/V110/9600", info);
					break;
				case ISDN_PROTO_L2_V11019:
					isdn_tty_at_cout("/V110/19200", info);
					break;
				case ISDN_PROTO_L2_V11038:
					isdn_tty_at_cout("/V110/38400", info);
L
Linus Torvalds 已提交
2597
					break;
2598 2599 2600 2601 2602 2603 2604
				}
				if (m->mdmreg[REG_T70] & BIT_T70) {
					isdn_tty_at_cout("/T.70", info);
					if (m->mdmreg[REG_T70] & BIT_T70_EXT)
						isdn_tty_at_cout("+", info);
				}
				break;
L
Linus Torvalds 已提交
2605 2606 2607 2608 2609
			}
			isdn_tty_at_cout("\r\n", info);
		}
	}
	if (code == RESULT_NO_CARRIER) {
J
Jiri Slaby 已提交
2610
		if ((info->port.flags & ASYNC_CLOSING) || (!info->tty))
L
Linus Torvalds 已提交
2611
			return;
2612

J
Jiri Slaby 已提交
2613
		if (info->port.flags & ASYNC_CHECK_CD)
L
Linus Torvalds 已提交
2614 2615 2616 2617 2618 2619 2620 2621 2622
			tty_hangup(info->tty);
	}
}


/*
 * Display a modem-register-value.
 */
static void
2623
isdn_tty_show_profile(int ridx, modem_info *info)
L
Linus Torvalds 已提交
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
{
	char v[6];

	sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
	isdn_tty_at_cout(v, info);
}

/*
 * Get MSN-string from char-pointer, set pointer to end of number
 */
static void
isdn_tty_get_msnstr(char *n, char **p)
{
	int limit = ISDN_MSNLEN - 1;

	while (((*p[0] >= '0' && *p[0] <= '9') ||
		/* Why a comma ??? */
		(*p[0] == ',') || (*p[0] == ':')) &&
2642
	       (limit--))
L
Linus Torvalds 已提交
2643 2644 2645 2646 2647 2648 2649 2650
		*n++ = *p[0]++;
	*n = '\0';
}

/*
 * Get phone-number from modem-commandbuffer
 */
static void
2651
isdn_tty_getdial(char *p, char *q, int cnt)
L
Linus Torvalds 已提交
2652 2653 2654
{
	int first = 1;
	int limit = ISDN_MSNLEN - 1;	/* MUST match the size of interface var to avoid
2655
					   buffer overflow */
L
Linus Torvalds 已提交
2656

2657
	while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
L
Linus Torvalds 已提交
2658
		if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
K
Karsten Keil 已提交
2659
		    ((*p == 'R') && first) ||
L
Linus Torvalds 已提交
2660 2661 2662 2663
		    (*p == '*') || (*p == '#')) {
			*q++ = *p;
			limit--;
		}
2664
		if (!limit)
L
Linus Torvalds 已提交
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
			break;
		p++;
		first = 0;
	}
	*q = 0;
}

#define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
#define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }

static void
2676
isdn_tty_report(modem_info *info)
L
Linus Torvalds 已提交
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
{
	atemu *m = &info->emu;
	char s[80];

	isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
	sprintf(s, "    Remote Number:    %s\r\n", info->last_num);
	isdn_tty_at_cout(s, info);
	sprintf(s, "    Direction:        %s\r\n", info->last_dir ? "outgoing" : "incoming");
	isdn_tty_at_cout(s, info);
	isdn_tty_at_cout("    Layer-2 Protocol: ", info);
	switch (info->last_l2) {
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
	case ISDN_PROTO_L2_X75I:
		isdn_tty_at_cout("X.75i", info);
		break;
	case ISDN_PROTO_L2_X75UI:
		isdn_tty_at_cout("X.75ui", info);
		break;
	case ISDN_PROTO_L2_X75BUI:
		isdn_tty_at_cout("X.75bui", info);
		break;
	case ISDN_PROTO_L2_HDLC:
		isdn_tty_at_cout("HDLC", info);
		break;
	case ISDN_PROTO_L2_V11096:
		isdn_tty_at_cout("V.110 9600 Baud", info);
		break;
	case ISDN_PROTO_L2_V11019:
		isdn_tty_at_cout("V.110 19200 Baud", info);
		break;
	case ISDN_PROTO_L2_V11038:
		isdn_tty_at_cout("V.110 38400 Baud", info);
		break;
	case ISDN_PROTO_L2_TRANS:
		isdn_tty_at_cout("transparent", info);
		break;
	case ISDN_PROTO_L2_MODEM:
		isdn_tty_at_cout("modem", info);
		break;
	case ISDN_PROTO_L2_FAX:
		isdn_tty_at_cout("fax", info);
		break;
	default:
		isdn_tty_at_cout("unknown", info);
		break;
L
Linus Torvalds 已提交
2721 2722 2723 2724 2725 2726 2727 2728 2729
	}
	if (m->mdmreg[REG_T70] & BIT_T70) {
		isdn_tty_at_cout("/T.70", info);
		if (m->mdmreg[REG_T70] & BIT_T70_EXT)
			isdn_tty_at_cout("+", info);
	}
	isdn_tty_at_cout("\r\n", info);
	isdn_tty_at_cout("    Service:          ", info);
	switch (info->last_si) {
2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
	case 1:
		isdn_tty_at_cout("audio\r\n", info);
		break;
	case 5:
		isdn_tty_at_cout("btx\r\n", info);
		break;
	case 7:
		isdn_tty_at_cout("data\r\n", info);
		break;
	default:
		sprintf(s, "%d\r\n", info->last_si);
		isdn_tty_at_cout(s, info);
		break;
L
Linus Torvalds 已提交
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
	}
	sprintf(s, "    Hangup location:  %s\r\n", info->last_lhup ? "local" : "remote");
	isdn_tty_at_cout(s, info);
	sprintf(s, "    Last cause:       %s\r\n", info->last_cause);
	isdn_tty_at_cout(s, info);
}

/*
 * Parse AT&.. commands.
 */
static int
2754
isdn_tty_cmd_ATand(char **p, modem_info *info)
L
Linus Torvalds 已提交
2755 2756 2757 2758 2759 2760 2761 2762
{
	atemu *m = &info->emu;
	int i;
	char rb[100];

#define MAXRB (sizeof(rb) - 1)

	switch (*p[0]) {
2763 2764 2765 2766 2767 2768
	case 'B':
		/* &B - Set Buffersize */
		p[0]++;
		i = isdn_getnum(p);
		if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
			PARSE_ERROR1;
L
Linus Torvalds 已提交
2769
#ifdef CONFIG_ISDN_AUDIO
2770 2771
		if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
			PARSE_ERROR1;
L
Linus Torvalds 已提交
2772
#endif
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
		m->mdmreg[REG_PSIZE] = i / 16;
		info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
		switch (m->mdmreg[REG_L2PROT]) {
		case ISDN_PROTO_L2_V11096:
		case ISDN_PROTO_L2_V11019:
		case ISDN_PROTO_L2_V11038:
			info->xmit_size /= 10;
		}
		break;
	case 'C':
		/* &C - DCD Status */
		p[0]++;
		switch (isdn_getnum(p)) {
		case 0:
			m->mdmreg[REG_DCD] &= ~BIT_DCD;
L
Linus Torvalds 已提交
2788
			break;
2789 2790
		case 1:
			m->mdmreg[REG_DCD] |= BIT_DCD;
L
Linus Torvalds 已提交
2791
			break;
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802
		default:
			PARSE_ERROR1
				}
		break;
	case 'D':
		/* &D - Set DTR-Low-behavior */
		p[0]++;
		switch (isdn_getnum(p)) {
		case 0:
			m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
			m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
L
Linus Torvalds 已提交
2803
			break;
2804 2805 2806 2807 2808 2809 2810
		case 2:
			m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
			m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
			break;
		case 3:
			m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
			m->mdmreg[REG_DTRR] |= BIT_DTRR;
L
Linus Torvalds 已提交
2811
			break;
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
		default:
			PARSE_ERROR1
				}
		break;
	case 'E':
		/* &E -Set EAZ/MSN */
		p[0]++;
		isdn_tty_get_msnstr(m->msn, p);
		break;
	case 'F':
		/* &F -Set Factory-Defaults */
		p[0]++;
		if (info->msr & UART_MSR_DCD)
			PARSE_ERROR1;
		isdn_tty_reset_profile(m);
		isdn_tty_modem_reset_regs(info, 1);
		break;
L
Linus Torvalds 已提交
2829
#ifdef DUMMY_HAYES_AT
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
	case 'K':
		/* only for be compilant with common scripts */
		/* &K Flowcontrol - no function */
		p[0]++;
		isdn_getnum(p);
		break;
#endif
	case 'L':
		/* &L -Set Numbers to listen on */
		p[0]++;
		i = 0;
		while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
		       (i < ISDN_LMSNLEN - 1))
			m->lmsn[i++] = *p[0]++;
		m->lmsn[i] = '\0';
		break;
	case 'R':
		/* &R - Set V.110 bitrate adaption */
		p[0]++;
		i = isdn_getnum(p);
		switch (i) {
		case 0:
			/* Switch off V.110, back to X.75 */
			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
			m->mdmreg[REG_SI2] = 0;
			info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
L
Linus Torvalds 已提交
2856
			break;
2857 2858 2859 2860
		case 9600:
			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
			m->mdmreg[REG_SI2] = 197;
			info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
L
Linus Torvalds 已提交
2861
			break;
2862 2863 2864 2865
		case 19200:
			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
			m->mdmreg[REG_SI2] = 199;
			info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
L
Linus Torvalds 已提交
2866
			break;
2867 2868 2869 2870
		case 38400:
			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
			m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
			info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
L
Linus Torvalds 已提交
2871
			break;
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
		default:
			PARSE_ERROR1;
		}
		/* Switch off T.70 */
		m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
		/* Set Service 7 */
		m->mdmreg[REG_SI1] |= 4;
		break;
	case 'S':
		/* &S - Set Windowsize */
		p[0]++;
		i = isdn_getnum(p);
		if ((i > 0) && (i < 9))
			m->mdmreg[REG_WSIZE] = i;
		else
			PARSE_ERROR1;
		break;
	case 'V':
		/* &V - Show registers */
		p[0]++;
		isdn_tty_at_cout("\r\n", info);
		for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
			sprintf(rb, "S%02d=%03d%s", i,
				m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
			isdn_tty_at_cout(rb, info);
		}
		sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
			strlen(m->msn) ? m->msn : "None");
		isdn_tty_at_cout(rb, info);
		if (strlen(m->lmsn)) {
			isdn_tty_at_cout("\r\nListen: ", info);
			isdn_tty_at_cout(m->lmsn, info);
			isdn_tty_at_cout("\r\n", info);
		}
		break;
	case 'W':
		/* &W - Write Profile */
		p[0]++;
		switch (*p[0]) {
		case '0':
L
Linus Torvalds 已提交
2912
			p[0]++;
2913
			modem_write_profile(m);
L
Linus Torvalds 已提交
2914
			break;
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
		default:
			PARSE_ERROR1;
		}
		break;
	case 'X':
		/* &X - Switch to BTX-Mode and T.70 */
		p[0]++;
		switch (isdn_getnum(p)) {
		case 0:
			m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
			info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
			break;
		case 1:
			m->mdmreg[REG_T70] |= BIT_T70;
			m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
			info->xmit_size = 112;
			m->mdmreg[REG_SI1] = 4;
			m->mdmreg[REG_SI2] = 0;
			break;
		case 2:
			m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
			info->xmit_size = 112;
			m->mdmreg[REG_SI1] = 4;
			m->mdmreg[REG_SI2] = 0;
L
Linus Torvalds 已提交
2941 2942 2943
			break;
		default:
			PARSE_ERROR1;
2944 2945 2946 2947
		}
		break;
	default:
		PARSE_ERROR1;
L
Linus Torvalds 已提交
2948 2949 2950 2951 2952
	}
	return 0;
}

static int
2953
isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
L
Linus Torvalds 已提交
2954 2955 2956
{
	/* Some plausibility checks */
	switch (mreg) {
2957 2958 2959 2960 2961 2962 2963
	case REG_L2PROT:
		if (mval > ISDN_PROTO_L2_MAX)
			return 1;
		break;
	case REG_PSIZE:
		if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
			return 1;
L
Linus Torvalds 已提交
2964
#ifdef CONFIG_ISDN_AUDIO
2965
		if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
L
Linus Torvalds 已提交
2966
			return 1;
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980
#endif
		info->xmit_size = mval * 16;
		switch (m->mdmreg[REG_L2PROT]) {
		case ISDN_PROTO_L2_V11096:
		case ISDN_PROTO_L2_V11019:
		case ISDN_PROTO_L2_V11038:
			info->xmit_size /= 10;
		}
		break;
	case REG_SI1I:
	case REG_PLAN:
	case REG_SCREEN:
		/* readonly registers */
		return 1;
L
Linus Torvalds 已提交
2981 2982 2983 2984 2985 2986 2987 2988
	}
	return 0;
}

/*
 * Perform ATS command
 */
static int
2989
isdn_tty_cmd_ATS(char **p, modem_info *info)
L
Linus Torvalds 已提交
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
{
	atemu *m = &info->emu;
	int bitpos;
	int mreg;
	int mval;
	int bval;

	mreg = isdn_getnum(p);
	if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
		PARSE_ERROR1;
	switch (*p[0]) {
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
	case '=':
		p[0]++;
		mval = isdn_getnum(p);
		if (mval < 0 || mval > 255)
			PARSE_ERROR1;
		if (isdn_tty_check_ats(mreg, mval, info, m))
			PARSE_ERROR1;
		m->mdmreg[mreg] = mval;
		break;
	case '.':
		/* Set/Clear a single bit */
		p[0]++;
		bitpos = isdn_getnum(p);
		if ((bitpos < 0) || (bitpos > 7))
			PARSE_ERROR1;
		switch (*p[0]) {
L
Linus Torvalds 已提交
3017 3018
		case '=':
			p[0]++;
3019 3020
			bval = isdn_getnum(p);
			if (bval < 0 || bval > 1)
L
Linus Torvalds 已提交
3021
				PARSE_ERROR1;
3022 3023 3024 3025
			if (bval)
				mval = m->mdmreg[mreg] | (1 << bitpos);
			else
				mval = m->mdmreg[mreg] & ~(1 << bitpos);
L
Linus Torvalds 已提交
3026 3027 3028 3029 3030 3031
			if (isdn_tty_check_ats(mreg, mval, info, m))
				PARSE_ERROR1;
			m->mdmreg[mreg] = mval;
			break;
		case '?':
			p[0]++;
3032 3033 3034
			isdn_tty_at_cout("\r\n", info);
			isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
					 info);
L
Linus Torvalds 已提交
3035 3036 3037
			break;
		default:
			PARSE_ERROR1;
3038 3039 3040 3041 3042 3043 3044 3045 3046
		}
		break;
	case '?':
		p[0]++;
		isdn_tty_show_profile(mreg, info);
		break;
	default:
		PARSE_ERROR1;
		break;
L
Linus Torvalds 已提交
3047 3048 3049 3050 3051 3052 3053 3054
	}
	return 0;
}

/*
 * Perform ATA command
 */
static void
3055
isdn_tty_cmd_ATA(modem_info *info)
L
Linus Torvalds 已提交
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108
{
	atemu *m = &info->emu;
	isdn_ctrl cmd;
	int l2;

	if (info->msr & UART_MSR_RI) {
		/* Accept incoming call */
		info->last_dir = 0;
		strcpy(info->last_num, dev->num[info->drv_index]);
		m->mdmreg[REG_RINGCNT] = 0;
		info->msr &= ~UART_MSR_RI;
		l2 = m->mdmreg[REG_L2PROT];
#ifdef CONFIG_ISDN_AUDIO
		/* If more than one bit set in reg18, autoselect Layer2 */
		if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
			if (m->mdmreg[REG_SI1I] == 1) {
				if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
					l2 = ISDN_PROTO_L2_TRANS;
			} else
				l2 = ISDN_PROTO_L2_X75I;
		}
#endif
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL2;
		cmd.arg = info->isdn_channel + (l2 << 8);
		info->last_l2 = l2;
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.command = ISDN_CMD_SETL3;
		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
#ifdef CONFIG_ISDN_TTY_FAX
		if (l2 == ISDN_PROTO_L2_FAX) {
			cmd.parm.fax = info->fax;
			info->fax->direction = ISDN_TTY_FAX_CONN_IN;
		}
#endif
		isdn_command(&cmd);
		cmd.driver = info->isdn_driver;
		cmd.arg = info->isdn_channel;
		cmd.command = ISDN_CMD_ACCEPTD;
		info->dialing = 16;
		info->emu.carrierwait = 0;
		isdn_command(&cmd);
		isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
	} else
		isdn_tty_modem_result(RESULT_NO_ANSWER, info);
}

#ifdef CONFIG_ISDN_AUDIO
/*
 * Parse AT+F.. commands
 */
static int
3109
isdn_tty_cmd_PLUSF(char **p, modem_info *info)
L
Linus Torvalds 已提交
3110 3111 3112 3113 3114 3115 3116
{
	atemu *m = &info->emu;
	char rs[20];

	if (!strncmp(p[0], "CLASS", 5)) {
		p[0] += 5;
		switch (*p[0]) {
3117 3118 3119 3120
		case '?':
			p[0]++;
			sprintf(rs, "\r\n%d",
				(m->mdmreg[REG_SI1] & 1) ? 8 : 0);
L
Linus Torvalds 已提交
3121
#ifdef CONFIG_ISDN_TTY_FAX
3122 3123 3124 3125
			if (TTY_IS_FCLASS2(info))
				sprintf(rs, "\r\n2");
			else if (TTY_IS_FCLASS1(info))
				sprintf(rs, "\r\n1");
L
Linus Torvalds 已提交
3126
#endif
3127 3128 3129 3130 3131 3132
			isdn_tty_at_cout(rs, info);
			break;
		case '=':
			p[0]++;
			switch (*p[0]) {
			case '0':
L
Linus Torvalds 已提交
3133
				p[0]++;
3134 3135 3136 3137 3138 3139
				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
				m->mdmreg[REG_SI1] = 4;
				info->xmit_size =
					m->mdmreg[REG_PSIZE] * 16;
				break;
L
Linus Torvalds 已提交
3140
#ifdef CONFIG_ISDN_TTY_FAX
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
			case '1':
				p[0]++;
				if (!(dev->global_features &
				      ISDN_FEATURE_L3_FCLASS1))
					PARSE_ERROR1;
				m->mdmreg[REG_SI1] = 1;
				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
				info->xmit_size =
					m->mdmreg[REG_PSIZE] * 16;
				break;
			case '2':
				p[0]++;
				if (!(dev->global_features &
				      ISDN_FEATURE_L3_FCLASS2))
					PARSE_ERROR1;
				m->mdmreg[REG_SI1] = 1;
				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
				info->xmit_size =
					m->mdmreg[REG_PSIZE] * 16;
				break;
L
Linus Torvalds 已提交
3163
#endif
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174
			case '8':
				p[0]++;
				/* L2 will change on dialout with si=1 */
				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
				m->mdmreg[REG_SI1] = 5;
				info->xmit_size = VBUF;
				break;
			case '?':
				p[0]++;
				strcpy(rs, "\r\n0,");
L
Linus Torvalds 已提交
3175
#ifdef CONFIG_ISDN_TTY_FAX
3176 3177 3178 3179 3180 3181 3182 3183 3184
				if (dev->global_features &
				    ISDN_FEATURE_L3_FCLASS1)
					strcat(rs, "1,");
				if (dev->global_features &
				    ISDN_FEATURE_L3_FCLASS2)
					strcat(rs, "2,");
#endif
				strcat(rs, "8");
				isdn_tty_at_cout(rs, info);
L
Linus Torvalds 已提交
3185 3186 3187
				break;
			default:
				PARSE_ERROR1;
3188 3189 3190 3191
			}
			break;
		default:
			PARSE_ERROR1;
L
Linus Torvalds 已提交
3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
		}
		return 0;
	}
#ifdef CONFIG_ISDN_TTY_FAX
	return (isdn_tty_cmd_PLUSF_FAX(p, info));
#else
	PARSE_ERROR1;
#endif
}

/*
 * Parse AT+V.. commands
 */
static int
3206
isdn_tty_cmd_PLUSV(char **p, modem_info *info)
L
Linus Torvalds 已提交
3207 3208 3209 3210
{
	atemu *m = &info->emu;
	isdn_ctrl cmd;
	static char *vcmd[] =
3211
		{"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
L
Linus Torvalds 已提交
3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225
	int i;
	int par1;
	int par2;
	char rs[20];

	i = 0;
	while (vcmd[i]) {
		if (!strncmp(vcmd[i], p[0], 2)) {
			p[0] += 2;
			break;
		}
		i++;
	}
	switch (i) {
3226 3227 3228 3229 3230 3231 3232 3233 3234
	case 0:
		/* AT+VNH - Auto hangup feature */
		switch (*p[0]) {
		case '?':
			p[0]++;
			isdn_tty_at_cout("\r\n1", info);
			break;
		case '=':
			p[0]++;
L
Linus Torvalds 已提交
3235
			switch (*p[0]) {
3236 3237 3238 3239 3240 3241 3242 3243 3244
			case '1':
				p[0]++;
				break;
			case '?':
				p[0]++;
				isdn_tty_at_cout("\r\n1", info);
				break;
			default:
				PARSE_ERROR1;
L
Linus Torvalds 已提交
3245 3246
			}
			break;
3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
		default:
			PARSE_ERROR1;
		}
		break;
	case 1:
		/* AT+VIP - Reset all voice parameters */
		isdn_tty_modem_reset_vpar(m);
		break;
	case 2:
		/* AT+VLS - Select device, accept incoming call */
		switch (*p[0]) {
		case '?':
			p[0]++;
			sprintf(rs, "\r\n%d", m->vpar[0]);
			isdn_tty_at_cout(rs, info);
L
Linus Torvalds 已提交
3262
			break;
3263 3264
		case '=':
			p[0]++;
L
Linus Torvalds 已提交
3265
			switch (*p[0]) {
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278
			case '0':
				p[0]++;
				m->vpar[0] = 0;
				break;
			case '2':
				p[0]++;
				m->vpar[0] = 2;
				break;
			case '?':
				p[0]++;
				isdn_tty_at_cout("\r\n0,2", info);
				break;
			default:
L
Linus Torvalds 已提交
3279 3280
				PARSE_ERROR1;
			}
3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
			break;
		default:
			PARSE_ERROR1;
		}
		break;
	case 3:
		/* AT+VRX - Start recording */
		if (!m->vpar[0])
			PARSE_ERROR1;
		if (info->online != 1) {
			isdn_tty_modem_result(RESULT_NO_ANSWER, info);
			return 1;
		}
		info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
		if (!info->dtmf_state) {
			printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
			PARSE_ERROR1;
		}
		info->silence_state = isdn_audio_silence_init(info->silence_state);
		if (!info->silence_state) {
			printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
			PARSE_ERROR1;
		}
		if (m->vpar[3] < 5) {
			info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
			if (!info->adpcmr) {
				printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
L
Linus Torvalds 已提交
3308 3309
				PARSE_ERROR1;
			}
3310
		}
L
Linus Torvalds 已提交
3311
#ifdef ISDN_DEBUG_AT
3312
		printk(KERN_DEBUG "AT: +VRX\n");
L
Linus Torvalds 已提交
3313
#endif
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
		info->vonline |= 1;
		isdn_tty_modem_result(RESULT_CONNECT, info);
		return 0;
		break;
	case 4:
		/* AT+VSD - Silence detection */
		switch (*p[0]) {
		case '?':
			p[0]++;
			sprintf(rs, "\r\n<%d>,<%d>",
				m->vpar[1],
				m->vpar[2]);
			isdn_tty_at_cout(rs, info);
L
Linus Torvalds 已提交
3327
			break;
3328 3329 3330 3331 3332
		case '=':
			p[0]++;
			if ((*p[0] >= '0') && (*p[0] <= '9')) {
				par1 = isdn_getnum(p);
				if ((par1 < 0) || (par1 > 31))
L
Linus Torvalds 已提交
3333
					PARSE_ERROR1;
3334
				if (*p[0] != ',')
L
Linus Torvalds 已提交
3335
					PARSE_ERROR1;
3336 3337 3338 3339 3340 3341 3342 3343 3344
				p[0]++;
				par2 = isdn_getnum(p);
				if ((par2 < 0) || (par2 > 255))
					PARSE_ERROR1;
				m->vpar[1] = par1;
				m->vpar[2] = par2;
				break;
			} else
				if (*p[0] == '?') {
L
Linus Torvalds 已提交
3345
					p[0]++;
3346 3347
					isdn_tty_at_cout("\r\n<0-31>,<0-255>",
							 info);
L
Linus Torvalds 已提交
3348
					break;
3349
				} else
L
Linus Torvalds 已提交
3350 3351
					PARSE_ERROR1;
			break;
3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
		default:
			PARSE_ERROR1;
		}
		break;
	case 5:
		/* AT+VSM - Select compression */
		switch (*p[0]) {
		case '?':
			p[0]++;
			sprintf(rs, "\r\n<%d>,<%d><8000>",
				m->vpar[3],
				m->vpar[1]);
			isdn_tty_at_cout(rs, info);
			break;
		case '=':
			p[0]++;
			switch (*p[0]) {
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
				par1 = isdn_getnum(p);
				if ((par1 < 2) || (par1 > 6))
					PARSE_ERROR1;
				m->vpar[3] = par1;
				break;
			case '?':
				p[0]++;
				isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
						 info);
				isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
						 info);
				isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
						 info);
				isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
						 info);
				isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
						 info);
				break;
			default:
L
Linus Torvalds 已提交
3393 3394
				PARSE_ERROR1;
			}
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
			break;
		default:
			PARSE_ERROR1;
		}
		break;
	case 6:
		/* AT+VTX - Start sending */
		if (!m->vpar[0])
			PARSE_ERROR1;
		if (info->online != 1) {
			isdn_tty_modem_result(RESULT_NO_ANSWER, info);
			return 1;
		}
		info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
		if (!info->dtmf_state) {
			printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
			PARSE_ERROR1;
		}
		if (m->vpar[3] < 5) {
			info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
			if (!info->adpcms) {
				printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
L
Linus Torvalds 已提交
3417 3418
				PARSE_ERROR1;
			}
3419
		}
L
Linus Torvalds 已提交
3420
#ifdef ISDN_DEBUG_AT
3421
		printk(KERN_DEBUG "AT: +VTX\n");
L
Linus Torvalds 已提交
3422
#endif
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
		m->lastDLE = 0;
		info->vonline |= 2;
		isdn_tty_modem_result(RESULT_CONNECT, info);
		return 0;
		break;
	case 7:
		/* AT+VDD - DTMF detection */
		switch (*p[0]) {
		case '?':
			p[0]++;
			sprintf(rs, "\r\n<%d>,<%d>",
				m->vpar[4],
				m->vpar[5]);
			isdn_tty_at_cout(rs, info);
L
Linus Torvalds 已提交
3437
			break;
3438 3439 3440 3441 3442 3443 3444 3445 3446
		case '=':
			p[0]++;
			if ((*p[0] >= '0') && (*p[0] <= '9')) {
				if (info->online != 1)
					PARSE_ERROR1;
				par1 = isdn_getnum(p);
				if ((par1 < 0) || (par1 > 15))
					PARSE_ERROR1;
				if (*p[0] != ',')
L
Linus Torvalds 已提交
3447
					PARSE_ERROR1;
3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465
				p[0]++;
				par2 = isdn_getnum(p);
				if ((par2 < 0) || (par2 > 255))
					PARSE_ERROR1;
				m->vpar[4] = par1;
				m->vpar[5] = par2;
				cmd.driver = info->isdn_driver;
				cmd.command = ISDN_CMD_AUDIO;
				cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
				cmd.parm.num[0] = par1;
				cmd.parm.num[1] = par2;
				isdn_command(&cmd);
				break;
			} else
				if (*p[0] == '?') {
					p[0]++;
					isdn_tty_at_cout("\r\n<0-15>,<0-255>",
							 info);
L
Linus Torvalds 已提交
3466
					break;
3467
				} else
L
Linus Torvalds 已提交
3468 3469 3470 3471
					PARSE_ERROR1;
			break;
		default:
			PARSE_ERROR1;
3472 3473 3474 3475
		}
		break;
	default:
		PARSE_ERROR1;
L
Linus Torvalds 已提交
3476 3477 3478 3479 3480 3481 3482 3483 3484
	}
	return 0;
}
#endif                          /* CONFIG_ISDN_AUDIO */

/*
 * Parse and perform an AT-command-line.
 */
static void
3485
isdn_tty_parse_at(modem_info *info)
L
Linus Torvalds 已提交
3486 3487 3488
{
	atemu *m = &info->emu;
	char *p;
3489
	char ds[ISDN_MSNLEN];
L
Linus Torvalds 已提交
3490 3491 3492 3493 3494 3495

#ifdef ISDN_DEBUG_AT
	printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
#endif
	for (p = &m->mdmcmd[2]; *p;) {
		switch (*p) {
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527
		case ' ':
			p++;
			break;
		case 'A':
			/* A - Accept incoming call */
			p++;
			isdn_tty_cmd_ATA(info);
			return;
			break;
		case 'D':
			/* D - Dial */
			if (info->msr & UART_MSR_DCD)
				PARSE_ERROR;
			if (info->msr & UART_MSR_RI) {
				isdn_tty_modem_result(RESULT_NO_CARRIER, info);
				return;
			}
			isdn_tty_getdial(++p, ds, sizeof ds);
			p += strlen(p);
			if (!strlen(m->msn))
				isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
			else if (strlen(ds))
				isdn_tty_dial(ds, info, m);
			else
				PARSE_ERROR;
			return;
		case 'E':
			/* E - Turn Echo on/off */
			p++;
			switch (isdn_getnum(&p)) {
			case 0:
				m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
L
Linus Torvalds 已提交
3528
				break;
3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540
			case 1:
				m->mdmreg[REG_ECHO] |= BIT_ECHO;
				break;
			default:
				PARSE_ERROR;
			}
			break;
		case 'H':
			/* H - On/Off-hook */
			p++;
			switch (*p) {
			case '0':
L
Linus Torvalds 已提交
3541
				p++;
3542
				isdn_tty_on_hook(info);
L
Linus Torvalds 已提交
3543
				break;
3544
			case '1':
L
Linus Torvalds 已提交
3545
				p++;
3546
				isdn_tty_off_hook();
L
Linus Torvalds 已提交
3547
				break;
3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
			default:
				isdn_tty_on_hook(info);
				break;
			}
			break;
		case 'I':
			/* I - Information */
			p++;
			isdn_tty_at_cout("\r\nLinux ISDN", info);
			switch (*p) {
			case '0':
			case '1':
L
Linus Torvalds 已提交
3560 3561
				p++;
				break;
3562
			case '2':
L
Linus Torvalds 已提交
3563
				p++;
3564
				isdn_tty_report(info);
L
Linus Torvalds 已提交
3565
				break;
3566
			case '3':
L
Linus Torvalds 已提交
3567
				p++;
3568 3569
				snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
				isdn_tty_at_cout(ds, info);
L
Linus Torvalds 已提交
3570
				break;
3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
			default:;
			}
			break;
#ifdef DUMMY_HAYES_AT
		case 'L':
		case 'M':
			/* only for be compilant with common scripts */
			/* no function */
			p++;
			isdn_getnum(&p);
			break;
L
Linus Torvalds 已提交
3582
#endif
3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
		case 'O':
			/* O - Go online */
			p++;
			if (info->msr & UART_MSR_DCD)
				/* if B-Channel is up */
				isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
			else
				isdn_tty_modem_result(RESULT_NO_CARRIER, info);
			return;
		case 'Q':
			/* Q - Turn Emulator messages on/off */
			p++;
			switch (isdn_getnum(&p)) {
			case 0:
				m->mdmreg[REG_RESP] |= BIT_RESP;
				break;
			case 1:
				m->mdmreg[REG_RESP] &= ~BIT_RESP;
				break;
			default:
				PARSE_ERROR;
			}
			break;
		case 'S':
			/* S - Set/Get Register */
			p++;
			if (isdn_tty_cmd_ATS(&p, info))
L
Linus Torvalds 已提交
3610
				return;
3611 3612 3613 3614 3615 3616 3617 3618 3619 3620
			break;
		case 'V':
			/* V - Numeric or ASCII Emulator-messages */
			p++;
			switch (isdn_getnum(&p)) {
			case 0:
				m->mdmreg[REG_RESP] |= BIT_RESPNUM;
				break;
			case 1:
				m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
L
Linus Torvalds 已提交
3621
				break;
3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639
			default:
				PARSE_ERROR;
			}
			break;
		case 'Z':
			/* Z - Load Registers from Profile */
			p++;
			if (info->msr & UART_MSR_DCD) {
				info->online = 0;
				isdn_tty_on_hook(info);
			}
			isdn_tty_modem_reset_regs(info, 1);
			break;
		case '+':
			p++;
			switch (*p) {
#ifdef CONFIG_ISDN_AUDIO
			case 'F':
L
Linus Torvalds 已提交
3640
				p++;
3641
				if (isdn_tty_cmd_PLUSF(&p, info))
L
Linus Torvalds 已提交
3642 3643 3644
					return;
				break;
			case 'V':
3645 3646 3647
				if ((!(m->mdmreg[REG_SI1] & 1)) ||
				    (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
					PARSE_ERROR;
L
Linus Torvalds 已提交
3648
				p++;
3649 3650
				if (isdn_tty_cmd_PLUSV(&p, info))
					return;
L
Linus Torvalds 已提交
3651
				break;
3652 3653
#endif                          /* CONFIG_ISDN_AUDIO */
			case 'S':	/* SUSPEND */
L
Linus Torvalds 已提交
3654
				p++;
3655 3656
				isdn_tty_get_msnstr(ds, &p);
				isdn_tty_suspend(ds, info, m);
L
Linus Torvalds 已提交
3657
				break;
3658
			case 'R':	/* RESUME */
L
Linus Torvalds 已提交
3659
				p++;
3660 3661
				isdn_tty_get_msnstr(ds, &p);
				isdn_tty_resume(ds, info, m);
L
Linus Torvalds 已提交
3662
				break;
3663
			case 'M':	/* MESSAGE */
L
Linus Torvalds 已提交
3664
				p++;
3665
				isdn_tty_send_msg(info, m, p);
L
Linus Torvalds 已提交
3666 3667 3668
				break;
			default:
				PARSE_ERROR;
3669 3670 3671 3672 3673 3674 3675 3676 3677
			}
			break;
		case '&':
			p++;
			if (isdn_tty_cmd_ATand(&p, info))
				return;
			break;
		default:
			PARSE_ERROR;
L
Linus Torvalds 已提交
3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688
		}
	}
#ifdef CONFIG_ISDN_AUDIO
	if (!info->vonline)
#endif
		isdn_tty_modem_result(RESULT_OK, info);
}

/* Need own toupper() because standard-toupper is not available
 * within modules.
 */
3689
#define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
L
Linus Torvalds 已提交
3690 3691 3692 3693 3694 3695 3696 3697 3698 3699

/*
 * Perform line-editing of AT-commands
 *
 * Parameters:
 *   p        inputbuffer
 *   count    length of buffer
 *   channel  index to line (minor-device)
 */
static int
3700
isdn_tty_edit_at(const char *p, int count, modem_info *info)
L
Linus Torvalds 已提交
3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
{
	atemu *m = &info->emu;
	int total = 0;
	u_char c;
	char eb[2];
	int cnt;

	for (cnt = count; cnt > 0; p++, cnt--) {
		c = *p;
		total++;
		if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
			/* Separator (CR or LF) */
			m->mdmcmd[m->mdmcmdl] = 0;
			if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
				eb[0] = c;
				eb[1] = 0;
				isdn_tty_at_cout(eb, info);
			}
			if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
				isdn_tty_parse_at(info);
			m->mdmcmdl = 0;
			continue;
		}
		if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
			/* Backspace-Function */
			if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
				if (m->mdmcmdl)
					m->mdmcmdl--;
				if (m->mdmreg[REG_ECHO] & BIT_ECHO)
					isdn_tty_at_cout("\b", info);
			}
			continue;
		}
		if (cmdchar(c)) {
			if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
				eb[0] = c;
				eb[1] = 0;
				isdn_tty_at_cout(eb, info);
			}
			if (m->mdmcmdl < 255) {
				c = my_toupper(c);
				switch (m->mdmcmdl) {
3743 3744 3745 3746
				case 1:
					if (c == 'T') {
						m->mdmcmd[m->mdmcmdl] = c;
						m->mdmcmd[++m->mdmcmdl] = 0;
L
Linus Torvalds 已提交
3747
						break;
3748 3749 3750 3751 3752
					} else
						m->mdmcmdl = 0;
					/* Fall through, check for 'A' */
				case 0:
					if (c == 'A') {
L
Linus Torvalds 已提交
3753 3754
						m->mdmcmd[m->mdmcmdl] = c;
						m->mdmcmd[++m->mdmcmdl] = 0;
3755 3756 3757 3758 3759
					}
					break;
				default:
					m->mdmcmd[m->mdmcmdl] = c;
					m->mdmcmd[++m->mdmcmdl] = 0;
L
Linus Torvalds 已提交
3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
				}
			}
		}
	}
	return total;
}

/*
 * Switch all modem-channels who are online and got a valid
 * escape-sequence 1.5 seconds ago, to command-mode.
 * This function is called every second via timer-interrupt from within
 * timer-dispatcher isdn_timer_function()
 */
void
isdn_tty_modem_escape(void)
{
	int ton = 0;
	int i;
	int midx;

	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
3781 3782 3783 3784 3785 3786 3787 3788 3789 3790
		if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
			modem_info *info = &dev->mdm.info[midx];
			if (info->online) {
				ton = 1;
				if ((info->emu.pluscount == 3) &&
				    time_after(jiffies,
					    info->emu.lastplus + PLUSWAIT2)) {
					info->emu.pluscount = 0;
					info->online = 0;
					isdn_tty_modem_result(RESULT_OK, info);
L
Linus Torvalds 已提交
3791 3792
				}
			}
3793
		}
L
Linus Torvalds 已提交
3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850
	isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
}

/*
 * Put a RING-message to all modem-channels who have the RI-bit set.
 * This function is called every second via timer-interrupt from within
 * timer-dispatcher isdn_timer_function()
 */
void
isdn_tty_modem_ring(void)
{
	int ton = 0;
	int i;

	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
		modem_info *info = &dev->mdm.info[i];
		if (info->msr & UART_MSR_RI) {
			ton = 1;
			isdn_tty_modem_result(RESULT_RING, info);
		}
	}
	isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
}

/*
 * For all online tty's, try sending data to
 * the lower levels.
 */
void
isdn_tty_modem_xmit(void)
{
	int ton = 1;
	int i;

	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
		modem_info *info = &dev->mdm.info[i];
		if (info->online) {
			ton = 1;
			isdn_tty_senddown(info);
			isdn_tty_tint(info);
		}
	}
	isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
}

/*
 * Check all channels if we have a 'no carrier' timeout.
 * Timeout value is set by Register S7.
 */
void
isdn_tty_carrier_timeout(void)
{
	int ton = 0;
	int i;

	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
		modem_info *info = &dev->mdm.info[i];
3851 3852 3853 3854 3855 3856 3857 3858
		if (!info->dialing)
			continue;
		if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
			info->dialing = 0;
			isdn_tty_modem_result(RESULT_NO_CARRIER, info);
			isdn_tty_modem_hup(info, 1);
		} else
			ton = 1;
L
Linus Torvalds 已提交
3859 3860 3861
	}
	isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
}