All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

isdn_tty.c 88.9 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
	struct tty_port *port = &info->port;
Linus Torvalds's avatar
Linus Torvalds committed
64 65
	int c;
	int len;
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
	if (!info->online)
		return 0;

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

	len = skb->len
Linus Torvalds's avatar
Linus Torvalds committed
75
#ifdef CONFIG_ISDN_AUDIO
76
		+ ISDN_AUDIO_SKB_DLECOUNT(skb)
Linus Torvalds's avatar
Linus Torvalds committed
77
#endif
78 79
		;

80
	c = tty_buffer_request_room(port, len);
81 82
	if (c < len)
		return 0;
Alan Cox's avatar
Alan Cox committed
83

Linus Torvalds's avatar
Linus Torvalds committed
84
#ifdef CONFIG_ISDN_AUDIO
85 86 87 88 89
	if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
		int l = skb->len;
		unsigned char *dp = skb->data;
		while (--l) {
			if (*dp == DLE)
Jiri Slaby's avatar
Jiri Slaby committed
90 91
				tty_insert_flip_char(port, DLE, 0);
			tty_insert_flip_char(port, *dp++, 0);
92 93
		}
		if (*dp == DLE)
Jiri Slaby's avatar
Jiri Slaby committed
94
			tty_insert_flip_char(port, DLE, 0);
95 96
		last = *dp;
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
97
#endif
98
		if (len > 1)
99
			tty_insert_flip_string(port, skb->data, len - 1);
100 101
		last = skb->data[len - 1];
#ifdef CONFIG_ISDN_AUDIO
Linus Torvalds's avatar
Linus Torvalds committed
102
	}
103 104
#endif
	if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
Jiri Slaby's avatar
Jiri Slaby committed
105
		tty_insert_flip_char(port, last, 0xFF);
106
	else
Jiri Slaby's avatar
Jiri Slaby committed
107
		tty_insert_flip_char(port, last, TTY_NORMAL);
Jiri Slaby's avatar
Jiri Slaby committed
108
	tty_flip_buffer_push(port);
109 110 111
	kfree_skb(skb);

	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

/* 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;
	modem_info *info;

	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
128 129 130 131 132 133 134 135 136
		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
137
#ifdef CONFIG_ISDN_AUDIO
138 139 140 141
		isdn_audio_eval_dtmf(info);
		if ((info->vonline & 1) && (info->emu.vpar[1]))
			isdn_audio_eval_silence(info);
#endif
Jiri Slaby's avatar
Jiri Slaby committed
142 143 144 145 146 147 148 149 150 151 152 153
		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,
						&info->port, 0);
			else
				r = isdn_readbchan_tty(info->isdn_driver,
						info->isdn_channel,
						&info->port, 1);
			if (r)
				tty_flip_buffer_push(&info->port);
154 155
		} else
			r = 1;
Jiri Slaby's avatar
Jiri Slaby committed
156

157 158 159 160 161
		if (r) {
			info->rcvsched = 0;
			resched = 1;
		} else
			info->rcvsched = 1;
Linus Torvalds's avatar
Linus Torvalds committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	}
	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;
184

Linus Torvalds's avatar
Linus Torvalds committed
185 186 187 188 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
	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]) {
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		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
241 242 243 244 245 246 247 248 249 250 251 252 253 254
		}
		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
255
	/* Try to deliver directly via tty-buf if queue is empty */
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	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;
}

279
static void
280
isdn_tty_cleanup_xmit(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283 284 285 286 287 288
{
	skb_queue_purge(&info->xmit_queue);
#ifdef CONFIG_ISDN_AUDIO
	skb_queue_purge(&info->dtmf_queue);
#endif
}

static void
289
isdn_tty_tint(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
290 291 292 293 294 295 296 297 298
{
	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) {
Jiri Slaby's avatar
Jiri Slaby committed
299
		struct tty_struct *tty = info->port.tty;
Linus Torvalds's avatar
Linus Torvalds committed
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
		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
330
isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
Linus Torvalds's avatar
Linus Torvalds committed
331
{
332
	unsigned char *p = &info->port.xmit_buf[info->xmit_count];
Linus Torvalds's avatar
Linus Torvalds committed
333 334 335 336 337 338
	int count = 0;

	while (len > 0) {
		if (m->lastDLE) {
			m->lastDLE = 0;
			switch (*p) {
339 340 341 342 343 344 345 346 347 348 349 350 351 352
			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
353
#ifdef ISDN_DEBUG_MODEM_VOICE
354 355 356
				printk(KERN_DEBUG
				       "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
				       info->line);
Linus Torvalds's avatar
Linus Torvalds committed
357
#endif
358 359
				isdn_tty_at_cout("\020\003", info);
				if (!info->vonline) {
Linus Torvalds's avatar
Linus Torvalds committed
360
#ifdef ISDN_DEBUG_MODEM_VOICE
361 362 363
					printk(KERN_DEBUG
					       "DLEdown: send VCON on ttyI%d\n",
					       info->line);
Linus Torvalds's avatar
Linus Torvalds committed
364
#endif
365 366 367 368 369 370 371 372 373 374
					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
375 376 377 378 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
			}
		} 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
421
isdn_tty_senddown(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
{
	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;
445
	if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
446
		info->msr &= ~UART_MSR_CTS;
447
	info->lsr &= ~UART_LSR_TEMT;
Linus Torvalds's avatar
Linus Torvalds committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	/* 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);
476
	memcpy(skb_put(skb, buflen), info->port.xmit_buf, buflen);
Linus Torvalds's avatar
Linus Torvalds committed
477 478 479 480 481 482 483 484 485 486 487 488 489
	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]) {
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
		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
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
		}
	}
#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
540
 * into the tty's buffer.
Linus Torvalds's avatar
Linus Torvalds committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554
 */
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
555
isdn_tty_modem_ncarrier(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
556 557 558 559 560 561 562 563 564 565
{
	if (info->ncarrier) {
		info->nc_timer.expires = jiffies + HZ;
		add_timer(&info->nc_timer);
	}
}

/*
 * return the usage calculated by si and layer 2 protocol
 */
566
static int
Linus Torvalds's avatar
Linus Torvalds committed
567 568 569 570 571 572
isdn_calc_usage(int si, int l2)
{
	int usg = ISDN_USAGE_MODEM;

#ifdef CONFIG_ISDN_AUDIO
	if (si == 1) {
573 574 575 576
		switch (l2) {
		case ISDN_PROTO_L2_MODEM:
			usg = ISDN_USAGE_MODEM;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
577
#ifdef CONFIG_ISDN_TTY_FAX
578 579 580
		case ISDN_PROTO_L2_FAX:
			usg = ISDN_USAGE_FAX;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
581
#endif
582 583 584 585
		case ISDN_PROTO_L2_TRANS:
		default:
			usg = ISDN_USAGE_VOICE;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
586 587 588
		}
	}
#endif
589
	return (usg);
Linus Torvalds's avatar
Linus Torvalds committed
590 591 592 593 594 595
}

/* isdn_tty_dial() performs dialing of a tty an the necessary
 * setup of the lower levels before that.
 */
static void
596
isdn_tty_dial(char *n, modem_info *info, atemu *m)
Linus Torvalds's avatar
Linus Torvalds committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
{
	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
613 614
	if ((si == 1) &&
	    (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds's avatar
Linus Torvalds committed
615
#ifdef CONFIG_ISDN_TTY_FAX
616
	    && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds's avatar
Linus Torvalds committed
617 618 619 620 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
#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
684
isdn_tty_modem_hup(modem_info *info, int local)
Linus Torvalds's avatar
Linus Torvalds committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
{
	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;
Jiri Slaby's avatar
Jiri Slaby committed
704
	isdn_tty_flush_buffer(info->port.tty);
Linus Torvalds's avatar
Linus Torvalds committed
705 706 707 708 709 710 711 712 713 714 715 716 717
	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;
718 719 720 721 722 723 724 725
	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
726 727
#endif
	if ((info->msr & UART_MSR_RI) &&
728
	    (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
Linus Torvalds's avatar
Linus Torvalds committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
		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;
	}
}

/*
751
 * Begin of a CAPI like interface, currently used only for
Linus Torvalds's avatar
Linus Torvalds committed
752 753 754
 * supplementary service (CAPI 2.0 part III)
 */
#include <linux/isdn/capicmd.h>
755
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
756 757 758

int
isdn_tty_capi_facility(capi_msg *cm) {
759
	return (-1); /* dummy */
Linus Torvalds's avatar
Linus Torvalds committed
760 761 762 763 764
}

/* isdn_tty_suspend() tries to suspend the current tty connection
 */
static void
765
isdn_tty_suspend(char *id, modem_info *info, atemu *m)
Linus Torvalds's avatar
Linus Torvalds committed
766 767
{
	isdn_ctrl cmd;
768

Linus Torvalds's avatar
Linus Torvalds committed
769 770 771 772 773 774 775 776 777 778
	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)) {
779
		cmd.parm.cmsg.Length = l + 18;
Linus Torvalds's avatar
Linus Torvalds committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
		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
798
 * setup of the lower levels before that. unfortunately here is no
Linus Torvalds's avatar
Linus Torvalds committed
799 800 801 802 803 804
 * 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
805
isdn_tty_resume(char *id, modem_info *info, atemu *m)
Linus Torvalds's avatar
Linus Torvalds committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
{
	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
824 825
	if ((si == 1) &&
	    (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds's avatar
Linus Torvalds committed
826
#ifdef CONFIG_ISDN_TTY_FAX
827
	    && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds's avatar
Linus Torvalds committed
828 829 830 831 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
#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;
869
		cmd.parm.cmsg.Length = l + 18;
Linus Torvalds's avatar
Linus Torvalds committed
870 871 872 873 874
		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;
875
		cmd.parm.cmsg.para[2] = l + 3;
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878 879
		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);
880
		cmd.command = CAPI_PUT_MESSAGE;
Linus Torvalds's avatar
Linus Torvalds committed
881 882 883 884 885 886 887 888 889 890 891 892 893
		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
894
isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
Linus Torvalds's avatar
Linus Torvalds committed
895 896 897 898 899 900 901 902 903 904
{
	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;

905 906 907
	l = min(strlen(msg), sizeof(cmd.parm) - sizeof(cmd.parm.cmsg)
		+ sizeof(cmd.parm.cmsg.para) - 2);

Linus Torvalds's avatar
Linus Torvalds committed
908 909 910 911 912 913 914 915 916 917 918
	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
919 920
	if ((si == 1) &&
	    (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds's avatar
Linus Torvalds committed
921
#ifdef CONFIG_ISDN_TTY_FAX
922
	    && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds's avatar
Linus Torvalds committed
923 924 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
#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;
963
		cmd.parm.cmsg.Length = l + 14;
Linus Torvalds's avatar
Linus Torvalds committed
964 965 966
		cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
		cmd.parm.cmsg.Subcommand = CAPI_REQ;
		cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
967
		cmd.parm.cmsg.para[0] = l + 1;
Linus Torvalds's avatar
Linus Torvalds committed
968
		strncpy(&cmd.parm.cmsg.para[1], msg, l);
969 970
		cmd.parm.cmsg.para[l + 1] = 0xd;
		cmd.command = CAPI_PUT_MESSAGE;
Linus Torvalds's avatar
Linus Torvalds committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984
/*		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",
985
		       name, routine);
Linus Torvalds's avatar
Linus Torvalds committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
		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
1002
isdn_tty_change_speed(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
1003
{
Jiri Slaby's avatar
Jiri Slaby committed
1004
	struct tty_port *port = &info->port;
Linus Torvalds's avatar
Linus Torvalds committed
1005
	uint cflag,
1006 1007
		cval,
		quot;
Linus Torvalds's avatar
Linus Torvalds committed
1008 1009
	int i;

1010
	if (!port->tty)
Linus Torvalds's avatar
Linus Torvalds committed
1011
		return;
1012
	cflag = port->tty->termios.c_cflag;
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015 1016 1017

	quot = i = cflag & CBAUD;
	if (i & CBAUDEX) {
		i &= ~CBAUDEX;
		if (i < 1 || i > 2)
1018
			port->tty->termios.c_cflag &= ~CBAUDEX;
Linus Torvalds's avatar
Linus Torvalds committed
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
		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;

	if (cflag & CLOCAL)
Jiri Slaby's avatar
Jiri Slaby committed
1047
		port->flags &= ~ASYNC_CHECK_CD;
Linus Torvalds's avatar
Linus Torvalds committed
1048
	else {
Jiri Slaby's avatar
Jiri Slaby committed
1049
		port->flags |= ASYNC_CHECK_CD;
Linus Torvalds's avatar
Linus Torvalds committed
1050 1051 1052 1053
	}
}

static int
1054
isdn_tty_startup(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
1055
{
Jiri Slaby's avatar
Jiri Slaby committed
1056
	if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds's avatar
Linus Torvalds committed
1057 1058 1059 1060 1061 1062 1063 1064 1065
		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;
Jiri Slaby's avatar
Jiri Slaby committed
1066 1067
	if (info->port.tty)
		clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1068 1069 1070 1071 1072
	/*
	 * and set the speed of the serial port
	 */
	isdn_tty_change_speed(info);

Jiri Slaby's avatar
Jiri Slaby committed
1073
	info->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds's avatar
Linus Torvalds committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	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
1084
isdn_tty_shutdown(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
1085
{
Jiri Slaby's avatar
Jiri Slaby committed
1086
	if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds's avatar
Linus Torvalds committed
1087 1088 1089 1090 1091 1092
		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;
1093
	if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
Linus Torvalds's avatar
Linus Torvalds committed
1094 1095 1096 1097 1098 1099 1100 1101 1102
		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);
		}
	}
Jiri Slaby's avatar
Jiri Slaby committed
1103 1104
	if (info->port.tty)
		set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1105

Jiri Slaby's avatar
Jiri Slaby committed
1106
	info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds's avatar
Linus Torvalds committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
}

/* 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
1119
isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
{
	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));
1149
			memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
Linus Torvalds's avatar
Linus Torvalds committed
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
#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
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
				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
1193
#ifdef ISDN_DEBUG_MODEM_VOICE
1194
					printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
Linus Torvalds's avatar
Linus Torvalds committed
1195
#endif
1196 1197
					info->xmit_count += cc;
				} else
Linus Torvalds's avatar
Linus Torvalds committed
1198
#endif
1199
					info->xmit_count += c;
Linus Torvalds's avatar
Linus Torvalds committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
		} 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);
1218
	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
Linus Torvalds's avatar
Linus Torvalds committed
1219 1220 1221 1222 1223 1224 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
		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;
1278
	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
Linus Torvalds's avatar
Linus Torvalds committed
1279 1280 1281 1282 1283 1284 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
		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
1335
isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
Linus Torvalds's avatar
Linus Torvalds committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
{
	u_char status;
	uint result;

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


static int
1347
isdn_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1348 1349 1350 1351
{
	modem_info *info = (modem_info *) tty->driver_data;
	u_char control, status;

1352
	if (isdn_tty_paranoia_check(info, tty->name, __func__))
Linus Torvalds's avatar
Linus Torvalds committed
1353
		return -ENODEV;
1354
	if (tty_io_error(tty))
Linus Torvalds's avatar
Linus Torvalds committed
1355 1356
		return -EIO;

Arnd Bergmann's avatar
Arnd Bergmann committed
1357
	mutex_lock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1358 1359 1360 1361 1362 1363
#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
1364
	mutex_unlock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1365
	return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1366 1367 1368 1369 1370
		| ((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
1371 1372 1373
}

static int
1374
isdn_tty_tiocmset(struct tty_struct *tty,
1375
		  unsigned int set, unsigned int clear)
Linus Torvalds's avatar
Linus Torvalds committed
1376 1377 1378
{
	modem_info *info = (modem_info *) tty->driver_data;

1379
	if (isdn_tty_paranoia_check(info, tty->name, __func__))
Linus Torvalds's avatar
Linus Torvalds committed
1380
		return -ENODEV;
1381
	if (tty_io_error(tty))
Linus Torvalds's avatar
Linus Torvalds committed
1382 1383 1384 1385 1386 1387
		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
1388
	mutex_lock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	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
1410
	mutex_unlock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1411 1412 1413 1414
	return 0;
}

static int
1415
isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
Linus Torvalds's avatar
Linus Torvalds committed
1416 1417 1418 1419 1420 1421
{
	modem_info *info = (modem_info *) tty->driver_data;
	int retval;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
		return -ENODEV;
1422
	if (tty_io_error(tty))
Linus Torvalds's avatar
Linus Torvalds committed
1423 1424
		return -EIO;
	switch (cmd) {
1425
	case TCSBRK:   /* SVID version: non-zero arg --> no break */
Linus Torvalds's avatar
Linus Torvalds committed
1426
#ifdef ISDN_DEBUG_MODEM_IOCTL
1427 1428 1429 1430 1431 1432 1433 1434
		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
1435
#ifdef ISDN_DEBUG_MODEM_IOCTL
1436 1437 1438 1439 1440 1441 1442 1443
		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
1444
#ifdef ISDN_DEBUG_MODEM_IOCTL
1445
		printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
Linus Torvalds's avatar
Linus Torvalds committed
1446
#endif
1447 1448
		return isdn_tty_get_lsr_info(info, (uint __user *) arg);
	default:
Linus Torvalds's avatar
Linus Torvalds committed
1449
#ifdef ISDN_DEBUG_MODEM_IOCTL
1450
		printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
Linus Torvalds's avatar
Linus Torvalds committed
1451
#endif
1452
		return -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
1453 1454 1455 1456 1457
	}
	return 0;
}

static void
Alan Cox's avatar
Alan Cox committed
1458
isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds's avatar
Linus Torvalds committed
1459 1460 1461 1462 1463 1464
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (!old_termios)
		isdn_tty_change_speed(info);
	else {
1465 1466 1467
		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
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
			return;
		isdn_tty_change_speed(info);
	}
}

/*
 * ------------------------------------------------------------
 * isdn_tty_open() and friends
 * ------------------------------------------------------------
 */
1478

Jiri Slaby's avatar
Jiri Slaby committed
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
static int isdn_tty_install(struct tty_driver *driver, struct tty_struct *tty)
{
	modem_info *info = &dev->mdm.info[tty->index];

	if (isdn_tty_paranoia_check(info, tty->name, __func__))
		return -ENODEV;

	tty->driver_data = info;

	return tty_port_install(&info->port, driver, tty);
}

Linus Torvalds's avatar
Linus Torvalds committed
1491 1492 1493 1494 1495 1496 1497 1498 1499
/*
 * 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)
{
Jiri Slaby's avatar
Jiri Slaby committed
1500 1501
	modem_info *info = tty->driver_data;
	struct tty_port *port = &info->port;
1502
	int retval;
Linus Torvalds's avatar
Linus Torvalds committed
1503 1504

#ifdef ISDN_DEBUG_MODEM_OPEN
1505
	printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
Jiri Slaby's avatar
Jiri Slaby committed
1506
	       port->count);
Linus Torvalds's avatar
Linus Torvalds committed
1507
#endif
Jiri Slaby's avatar
Jiri Slaby committed
1508 1509
	port->count++;
	port->tty = tty;
Linus Torvalds's avatar
Linus Torvalds committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
	/*
	 * 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;
	}
1520
	retval = tty_port_block_til_ready(port, tty, filp);
Linus Torvalds's avatar
Linus Torvalds committed
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
	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;
Jiri Slaby's avatar
Jiri Slaby committed
1541
	struct tty_port *port = &info->port;
Linus Torvalds's avatar
Linus Torvalds committed
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
	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;
	}
Jiri Slaby's avatar
Jiri Slaby committed
1552
	if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds's avatar
Linus Torvalds committed
1553 1554 1555 1556 1557 1558 1559 1560
		/*
		 * 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, "
Jiri Slaby's avatar
Jiri Slaby committed
1561 1562
		       "info->count is %d\n", port->count);
		port->count = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1563
	}
Jiri Slaby's avatar
Jiri Slaby committed
1564
	if (--port->count < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1565
		printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
Jiri Slaby's avatar
Jiri Slaby committed
1566 1567
		       info->line, port->count);
		port->count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1568
	}