isdn_tty.c 88.4 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>
18
#include <linux/sched/signal.h>
Linus Torvalds's avatar
Linus Torvalds committed
19
20
21
22
23
24
25
26
27
28
29
30
31
#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
32
static DEFINE_MUTEX(modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
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
61
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
62
isdn_tty_try_read(modem_info *info, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
63
{
64
	struct tty_port *port = &info->port;
Linus Torvalds's avatar
Linus Torvalds committed
65
66
	int c;
	int len;
Alan Cox's avatar
Alan Cox committed
67
	char last;
Linus Torvalds's avatar
Linus Torvalds committed
68

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

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

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

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

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

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

/* 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++) {
129
130
131
132
133
134
135
136
137
		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
138
#ifdef CONFIG_ISDN_AUDIO
139
140
141
142
		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
143
144
145
146
147
148
149
150
151
152
153
154
		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);
155
156
		} else
			r = 1;
Jiri Slaby's avatar
Jiri Slaby committed
157

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

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

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

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

	while (len > 0) {
		if (m->lastDLE) {
			m->lastDLE = 0;
			switch (*p) {
340
341
342
343
344
345
346
347
348
349
350
351
352
353
			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
354
#ifdef ISDN_DEBUG_MODEM_VOICE
355
356
357
				printk(KERN_DEBUG
				       "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
				       info->line);
Linus Torvalds's avatar
Linus Torvalds committed
358
#endif
359
360
				isdn_tty_at_cout("\020\003", info);
				if (!info->vonline) {
Linus Torvalds's avatar
Linus Torvalds committed
361
#ifdef ISDN_DEBUG_MODEM_VOICE
362
363
364
					printk(KERN_DEBUG
					       "DLEdown: send VCON on ttyI%d\n",
					       info->line);
Linus Torvalds's avatar
Linus Torvalds committed
365
#endif
366
367
368
369
370
371
372
373
374
375
					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
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
421
			}
		} 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
422
isdn_tty_senddown(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
{
	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;
446
	if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
447
		info->msr &= ~UART_MSR_CTS;
448
	info->lsr &= ~UART_LSR_TEMT;
Linus Torvalds's avatar
Linus Torvalds committed
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
476
	/* 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);
477
	skb_put_data(skb, info->port.xmit_buf, buflen);
Linus Torvalds's avatar
Linus Torvalds committed
478
479
480
481
482
483
484
485
486
487
488
489
490
	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]) {
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
516
		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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
		}
	}
#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
541
 * into the tty's buffer.
Linus Torvalds's avatar
Linus Torvalds committed
542
543
 */
static void
544
isdn_tty_modem_do_ncarrier(struct timer_list *t)
Linus Torvalds's avatar
Linus Torvalds committed
545
{
546
	modem_info *info = from_timer(info, t, nc_timer);
Linus Torvalds's avatar
Linus Torvalds committed
547
548
549
550
551
552
553
554
555
	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
556
isdn_tty_modem_ncarrier(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
557
558
559
560
561
562
563
564
565
566
{
	if (info->ncarrier) {
		info->nc_timer.expires = jiffies + HZ;
		add_timer(&info->nc_timer);
	}
}

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

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

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

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

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

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

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

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

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

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

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

1047
	tty_port_set_check_carrier(port, ~cflag & CLOCAL);
Linus Torvalds's avatar
Linus Torvalds committed
1048
1049
1050
}

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

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

1103
	tty_port_set_initialized(&info->port, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
}

/* 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
1116
isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
Linus Torvalds's avatar
Linus Torvalds committed
1117
1118
1119
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
{
	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));
1146
			memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
Linus Torvalds's avatar
Linus Torvalds committed
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
#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
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
				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
1190
#ifdef ISDN_DEBUG_MODEM_VOICE
1191
					printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
Linus Torvalds's avatar
Linus Torvalds committed
1192
#endif
1193
1194
					info->xmit_count += cc;
				} else
Linus Torvalds's avatar
Linus Torvalds committed
1195
#endif
1196
					info->xmit_count += c;
Linus Torvalds's avatar
Linus Torvalds committed
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
		} 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);
1215
	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
Linus Torvalds's avatar
Linus Torvalds committed
1216
1217
1218
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
		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;
1275
	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
Linus Torvalds's avatar
Linus Torvalds committed
1276
1277
1278
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
		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
1332
isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
Linus Torvalds's avatar
Linus Torvalds committed
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
{
	u_char status;
	uint result;

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


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

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

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

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

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

static int
1412
isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
Linus Torvalds's avatar
Linus Torvalds committed
1413
1414
1415
1416
1417
{
	modem_info *info = (modem_info *) tty->driver_data;

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
		return -ENODEV;
1418
	if (tty_io_error(tty))
Linus Torvalds's avatar
Linus Torvalds committed
1419
1420
		return -EIO;
	switch (cmd) {
1421
	case TIOCSERGETLSR:	/* Get line status register */
Linus Torvalds's avatar
Linus Torvalds committed
1422
#ifdef ISDN_DEBUG_MODEM_IOCTL
1423
		printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
Linus Torvalds's avatar
Linus Torvalds committed
1424
#endif
1425
1426
		return isdn_tty_get_lsr_info(info, (uint __user *) arg);
	default:
Linus Torvalds's avatar
Linus Torvalds committed
1427
#ifdef ISDN_DEBUG_MODEM_IOCTL
1428
		printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
Linus Torvalds's avatar
Linus Torvalds committed
1429
#endif
1430
		return -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
1431
1432
1433
1434
1435
	}
	return 0;
}

static void
Alan Cox's avatar
Alan Cox committed
1436
isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds's avatar
Linus Torvalds committed
1437
1438
1439
{
	modem_info *info = (modem_info *) tty->driver_data;

1440
	mutex_lock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1441
1442
1443
	if (!old_termios)
		isdn_tty_change_speed(info);
	else {
1444
1445
		if (tty->termios.c_cflag == old_termios->c_cflag &&
		    tty->termios.c_ispeed == old_termios->c_ispeed &&
1446
1447
		    tty->termios.c_ospeed == old_termios->c_ospeed) {
			mutex_unlock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1448
			return;
1449
		}
Linus Torvalds's avatar
Linus Torvalds committed
1450
1451
		isdn_tty_change_speed(info);
	}
1452
	mutex_unlock(&modem_info_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1453
1454
1455
1456
1457
1458
1459
}

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

Jiri Slaby's avatar
Jiri Slaby committed
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
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
1473
1474
1475
1476
1477
1478
1479
1480
1481
/*
 * 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
1482
1483
	modem_info *info = tty->driver_data;
	struct tty_port *port = &info->port;
1484
	int retval;
Linus Torvalds's avatar
Linus Torvalds committed
1485
1486

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

	tty->closing = 1;
	/*
	 * At this point we stop accepting input.  To do this, we
	 * disable the receive line status interrupts, and tell the
	 * interrupt driver to stop checking the data ready bit in the
	 * line status register.
	 */
1566
	if (tty_port_initialized(port)) {
1567
		tty_wait_until_sent(tty, 3000);	/* 30 seconds timeout */
Linus Torvalds's avatar
Linus Torvalds committed
1568
1569
1570
1571
1572
1573
1574
		/*
		 * Before we drop DTR, make sure the UART transmitter
		 * has completely drained; this is especially
		 * important if there is a transmit FIFO!
		 */
		timeout = jiffies + HZ;
		while (!(info->lsr & UART_LSR_TEMT)) {
1575
			schedule_timeout_interruptible(20);
1576
			if (time_after(jiffies, timeout))
Linus Torvalds's avatar
Linus Torvalds committed
1577
1578
1579
1580
1581
				break;
		}
	}
	dev->modempoll--;
	isdn_tty_shutdown(info);
1582
	isdn_tty_flush_buffer(tty);
Linus Torvalds's avatar
Linus Torvalds committed
1583
	tty_ldisc_flush(tty);
Jiri Slaby's avatar
Jiri Slaby committed
1584
	port->tty = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1585
	info->ncarrier = 0;
1586
1587

	tty_port_close_end(port, tty);
Peter Hurley's avatar
Peter Hurley committed
1588
	info->closing = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
#ifdef ISDN_DEBUG_MODEM_OPEN
	printk(KERN_DEBUG "isdn_tty_close normal exit\n");
#endif
}

/*
 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
 */
static void
isdn_tty_hangup(struct tty_struct *tty)
{
	modem_info *info = (modem_info *) tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1601
	struct tty_port *port = &info->port;
Linus Torvalds's avatar
Linus Torvalds committed
1602
1603
1604
1605

	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
		return;
	isdn_tty_shutdown(info);
Jiri Slaby's avatar
Jiri Slaby committed
1606
	port->count = 0;
1607
	tty_port_set_active(port, 0);
Jiri Slaby's avatar
Jiri Slaby committed
1608
1609
	port->tty = NULL;
	wake_up_interruptible(&port->open_wait);
Linus Torvalds's avatar
Linus Torvalds committed
1610
1611
1612
1613
1614
}

/* This routine initializes all emulator-data.
 */
static void
1615
isdn_tty_reset_profile(atemu *m)
Linus Torvalds's avatar
Linus Torvalds committed
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
{
	m->profile[0] = 0;
	m->profile[1] = 0;
	m->profile[2] = 43;
	m->profile[3] = 13;
	m->profile[4] = 10;
	m->profile[5] = 8;
	m->profile[6] = 3;
	m->profile[7] = 60;
	m->profile[8] = 2;
	m->profile[9] = 6;
	m->profile[10] = 7;
	m->profile[11] = 70;
	m->profile[12] = 0x45;
	m->profile[13] = 4;
	m->profile[14] = ISDN_PROTO_L2_X75I;
	m->profile[15] = ISDN_PROTO_L3_TRANS;
	m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
	m->profile[17] = ISDN_MODEM_WINSIZE;
	m->profile[18] = 4;
	m->profile[19] = 0;
	m->profile[20] = 0;
	m->profile[23] = 0;
	m->pmsn[0] = '\0';
	m->plmsn[0] = '\0';
}

#ifdef CONFIG_ISDN_AUDIO
static void
1645
isdn_tty_modem_reset_vpar(atemu *m)
Linus Torvalds's avatar
Linus Torvalds committed
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
{
	m->vpar[0] = 2;         /* Voice-device            (2 = phone line) */
	m->vpar[1] = 0;         /* Silence detection level (0 = none      ) */
	m->vpar[2] = 70;        /* Silence interval        (7 sec.        ) */
	m->vpar[3] = 2;         /* Compression type        (1 = ADPCM-2   ) */
	m->vpar[4] = 0;         /* DTMF detection level    (0 = softcode  ) */
	m->vpar[5] = 8;         /* DTMF interval           (8 * 5 ms.     ) */
}
#endif

#ifdef CONFIG_ISDN_TTY_FAX
static void
1658
isdn_tty_modem_reset_faxpar(modem_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
{
	T30_s *f = info->fax;

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

static void
1691
isdn_tty_modem_reset_regs(modem_info *info, int force)
Linus Torvalds's avatar
Linus Torvalds committed
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
{
	atemu *m = &info->emu;
	if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
		memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
		memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
		memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
		info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
	}
#ifdef CONFIG_ISDN_AUDIO
	isdn_tty_modem_reset_vpar(m);