isdn_tty.c 91.3 KB
Newer Older
Jiri Slaby's avatar
Jiri Slaby committed
1
/*
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
16
#include <linux/delay.h>
Arnd Bergmann's avatar
Arnd Bergmann committed
17
#include <linux/mutex.h>
Linus Torvalds's avatar
Linus Torvalds committed
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 */

Arnd Bergmann's avatar
Arnd Bergmann committed
31
static DEFINE_MUTEX(modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64 65
{
	int c;
	int len;
	struct tty_struct *tty;
Alan Cox's avatar
Alan Cox committed
66
	char last;
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
79
#ifdef CONFIG_ISDN_AUDIO
80
		+ ISDN_AUDIO_SKB_DLECOUNT(skb)
Linus Torvalds's avatar
Linus Torvalds committed
81
#endif
82 83 84 85 86
		;

	c = tty_buffer_request_room(tty, len);
	if (c < len)
		return 0;
Alan Cox's avatar
Alan Cox committed
87

Linus Torvalds's avatar
Linus Torvalds committed
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 {
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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

Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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
Alan Cox's avatar
Alan Cox committed
259
	/* Try to deliver directly via tty-buf if queue is empty */
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
361
#endif
362 363
				isdn_tty_at_cout("\020\003", info);
				if (!info->vonline) {
Linus Torvalds's avatar
Linus Torvalds committed
364
#ifdef ISDN_DEBUG_MODEM_VOICE
365 366 367
					printk(KERN_DEBUG
					       "DLEdown: send VCON on ttyI%d\n",
					       info->line);
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
450
		info->msr &= ~UART_MSR_CTS;
451
	info->lsr &= ~UART_LSR_TEMT;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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
Alan Cox's avatar
Alan Cox committed
544
 * into the tty's buffer.
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
581
#ifdef CONFIG_ISDN_TTY_FAX
582 583 584
		case ISDN_PROTO_L2_FAX:
			usg = ISDN_USAGE_FAX;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
585
#endif
586 587 588 589
		case ISDN_PROTO_L2_TRANS:
		default:
			usg = ISDN_USAGE_VOICE;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
590 591 592
		}
	}
#endif
593
	return (usg);
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
619
#ifdef CONFIG_ISDN_TTY_FAX
620
	    && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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;
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;
Linus Torvalds's avatar
Linus Torvalds committed
730 731
#endif
	if ((info->msr & UART_MSR_RI) &&
732
	    (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
756 757 758
 * supplementary service (CAPI 2.0 part III)
 */
#include <linux/isdn/capicmd.h>
759
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
760 761 762

int
isdn_tty_capi_facility(capi_msg *cm) {
763
	return (-1); /* dummy */
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
770 771
{
	isdn_ctrl cmd;
772

Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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
Lucas De Marchi's avatar
Lucas De Marchi committed
802
 * setup of the lower levels before that. unfortunately here is no
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
830
#ifdef CONFIG_ISDN_TTY_FAX
831
	    && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
923
#ifdef CONFIG_ISDN_TTY_FAX
924
	    && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
970
		strncpy(&cmd.parm.cmsg.para[1], msg, l);
971 972
		cmd.parm.cmsg.para[l + 1] = 0xd;
		cmd.command = CAPI_PUT_MESSAGE;
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
1005 1006
{
	uint cflag,
1007 1008
		cval,
		quot;
Linus Torvalds's avatar
Linus Torvalds committed
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) {
Jiri Slaby's avatar
Jiri Slaby committed
1049
		info->port.flags |= ASYNC_CTS_FLOW;
Linus Torvalds's avatar
Linus Torvalds committed
1050
	} else
Jiri Slaby's avatar
Jiri Slaby committed
1051
		info->port.flags &= ~ASYNC_CTS_FLOW;
Linus Torvalds's avatar
Linus Torvalds committed
1052
	if (cflag & CLOCAL)
Jiri Slaby's avatar
Jiri Slaby committed
1053
		info->port.flags &= ~ASYNC_CHECK_CD;
Linus Torvalds's avatar
Linus Torvalds committed
1054
	else {
Jiri Slaby's avatar
Jiri Slaby committed
1055
		info->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds's avatar
Linus Torvalds committed
1056 1057 1058 1059
	}
}

static int
1060
isdn_tty_startup(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
1061
{
Jiri Slaby's avatar
Jiri Slaby committed
1062
	if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds's avatar
Linus Torvalds committed
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);

Jiri Slaby's avatar
Jiri Slaby committed
1079
	info->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
1091
{
Jiri Slaby's avatar
Jiri Slaby committed
1092
	if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds's avatar
Linus Torvalds committed
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);

Jiri Slaby's avatar
Jiri Slaby committed
1112
	info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
1199
#ifdef ISDN_DEBUG_MODEM_VOICE
1200
					printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
Linus Torvalds's avatar
Linus Torvalds committed
1201
#endif
1202 1203
					info->xmit_count += cc;
				} else
Linus Torvalds's avatar
Linus Torvalds committed
1204
#endif
1205
					info->xmit_count += c;
Linus Torvalds's avatar
Linus Torvalds committed
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)) {
Linus Torvalds's avatar
Linus Torvalds committed
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))
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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__))
Linus Torvalds's avatar
Linus Torvalds committed
1359 1360 1361 1362
		return -ENODEV;
	if (tty->flags & (1 << TTY_IO_ERROR))
		return -EIO;

Arnd Bergmann's avatar
Arnd Bergmann committed
1363
	mutex_lock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
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;
Arnd Bergmann's avatar
Arnd Bergmann committed
1370
	mutex_unlock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
1377 1378 1379
}

static int
1380
isdn_tty_tiocmset(struct tty_struct *tty,
1381
		  unsigned int set, unsigned int clear)
Linus Torvalds's avatar
Linus Torvalds committed
1382 1383 1384
{
	modem_info *info = (modem_info *) tty->driver_data;

1385
	if (isdn_tty_paranoia_check(info, tty->name, __func__))
Linus Torvalds's avatar
Linus Torvalds committed
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

Arnd Bergmann's avatar
Arnd Bergmann committed
1394
	mutex_lock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
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);
		}
	}
Arnd Bergmann's avatar
Arnd Bergmann committed
1416
	mutex_unlock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1417 1418 1419 1420
	return 0;
}

static int
1421
isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
Linus Torvalds's avatar
Linus Torvalds committed
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 */
Linus Torvalds's avatar
Linus Torvalds committed
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() */
Linus Torvalds's avatar
Linus Torvalds committed
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 */
Linus Torvalds's avatar
Linus Torvalds committed
1450
#ifdef ISDN_DEBUG_MODEM_IOCTL
1451
		printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
Linus Torvalds's avatar
Linus Torvalds committed
1452
#endif
1453 1454
		return isdn_tty_get_lsr_info(info, (uint __user *) arg);
	default:
Linus Torvalds's avatar
Linus Torvalds committed
1455
#ifdef ISDN_DEBUG_MODEM_IOCTL
1456
		printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
Linus Torvalds's avatar
Linus Torvalds committed
1457
#endif
1458
		return -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
1459 1460 1461 1462 1463
	}
	return 0;
}

static void
Alan Cox's avatar
Alan Cox committed
1464
isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds's avatar
Linus Torvalds committed
1465 1466 1467 1468 1469 1470
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (!old_termios)
		isdn_tty_change_speed(info);
	else {
Alan Cox's avatar
Alan Cox committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
1474 1475 1476
			return;
		isdn_tty_change_speed(info);
		if ((old_termios->c_cflag & CRTSCTS) &&
Alan Cox's avatar
Alan Cox committed
1477
		    !(tty->termios->c_cflag & CRTSCTS))
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
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) ||
Jiri Slaby's avatar
Jiri Slaby committed
1499 1500
	    (info->port.flags & ASYNC_CLOSING)) {
		if (info->port.flags & ASYNC_CLOSING)
1501
			interruptible_sleep_on(&info->port.close_wait);
Linus Torvalds's avatar
Linus Torvalds committed
1502
#ifdef MODEM_DO_RESTART
Jiri Slaby's avatar
Jiri Slaby committed
1503
		if (info->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds's avatar
Linus Torvalds committed
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))) {
Jiri Slaby's avatar
Jiri Slaby committed
1517
		info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds's avatar
Linus Torvalds committed
1518 1519
		return 0;
	}
Jiri Slaby's avatar
Jiri Slaby committed
1520 1521
	if (tty->termios->c_cflag & CLOCAL)
		do_clocal = 1;
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
1531 1532 1533 1534 1535
#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)))
1536 1537
		info->port.count--;
	info->port.blocked_open++;
Linus Torvalds's avatar
Linus Torvalds committed
1538 1539 1540
	while (1) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (tty_hung_up_p(filp) ||
Jiri Slaby's avatar
Jiri Slaby committed
1541
		    !(info->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds's avatar
Linus Torvalds committed
1542
#ifdef MODEM_DO_RESTART
Jiri Slaby's avatar
Jiri Slaby committed
1543
			if (info->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds's avatar
Linus Torvalds committed
1544 1545 1546 1547 1548 1549 1550 1551
				retval = -EAGAIN;
			else
				retval = -ERESTARTSYS;
#else
			retval = -EAGAIN;
#endif
			break;
		}
Jiri Slaby's avatar
Jiri Slaby committed
1552
		if (!(info->port.flags & ASYNC_CLOSING) &&
Linus Torvalds's avatar
Linus Torvalds committed
1553 1554 1555 1556 1557 1558 1559 1560 1561
		    (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",
1562
		       info->line, info->port.count);
Linus Torvalds's avatar
Linus Torvalds committed
1563 1564 1565 1566
#endif
		schedule();
	}
	current->state = TASK_RUNNING;
1567
	remove_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds's avatar
Linus Torvalds committed
1568
	if (!tty_hung_up_p(filp))
1569 1570
		info->port.count++;
	info->port.blocked_open--;
Linus Torvalds's avatar
Linus Torvalds committed
1571 1572
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "isdn_tty_block_til_ready after blocking: ttyi%d, count = %d\n",
1573
	       info->line, info->port.count);
Linus Torvalds's avatar
Linus Torvalds committed
1574 1575 1576
#endif
	if (retval)
		return retval;
Jiri Slaby's avatar
Jiri Slaby committed
1577
	info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
1592

1593
	info = &dev->mdm.info[tty->index];
Linus Torvalds's avatar
Linus Torvalds committed
1594 1595 1596
	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_open"))
		return -ENODEV