1 /*
2  * Linux ISDN subsystem, tty functions and AT-command emulator (linklevel).
3  *
4  * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
5  * Copyright 1995,96    by Thinking Objects Software GmbH Wuerzburg
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11 #undef ISDN_TTY_STAT_DEBUG
12 
13 #include <linux/isdn.h>
14 #include <linux/serial.h> /* ASYNC_* flags */
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/mutex.h>
18 #include <linux/sched/signal.h>
19 #include "isdn_common.h"
20 #include "isdn_tty.h"
21 #ifdef CONFIG_ISDN_AUDIO
22 #include "isdn_audio.h"
23 #define VBUF 0x3e0
24 #define VBUFX (VBUF/16)
25 #endif
26 
27 #define FIX_FILE_TRANSFER
28 #define	DUMMY_HAYES_AT
29 
30 /* Prototypes */
31 
32 static DEFINE_MUTEX(modem_info_mutex);
33 static int isdn_tty_edit_at(const char *, int, modem_info *);
34 static void isdn_tty_check_esc(const u_char *, u_char, int, int *, u_long *);
35 static void isdn_tty_modem_reset_regs(modem_info *, int);
36 static void isdn_tty_cmd_ATA(modem_info *);
37 static void isdn_tty_flush_buffer(struct tty_struct *);
38 static void isdn_tty_modem_result(int, modem_info *);
39 #ifdef CONFIG_ISDN_AUDIO
40 static int isdn_tty_countDLE(unsigned char *, int);
41 #endif
42 
43 /* Leave this unchanged unless you know what you do! */
44 #define MODEM_PARANOIA_CHECK
45 #define MODEM_DO_RESTART
46 
47 static int bit2si[8] =
48 {1, 5, 7, 7, 7, 7, 7, 7};
49 static int si2bit[8] =
50 {4, 1, 4, 4, 4, 4, 4, 4};
51 
52 /* isdn_tty_try_read() is called from within isdn_tty_rcv_skb()
53  * to stuff incoming data directly into a tty's flip-buffer. This
54  * is done to speed up tty-receiving if the receive-queue is empty.
55  * This routine MUST be called with interrupts off.
56  * Return:
57  *  1 = Success
58  *  0 = Failure, data has to be buffered and later processed by
59  *      isdn_tty_readmodem().
60  */
61 static int
isdn_tty_try_read(modem_info * info,struct sk_buff * skb)62 isdn_tty_try_read(modem_info *info, struct sk_buff *skb)
63 {
64 	struct tty_port *port = &info->port;
65 	int c;
66 	int len;
67 	char last;
68 
69 	if (!info->online)
70 		return 0;
71 
72 	if (!(info->mcr & UART_MCR_RTS))
73 		return 0;
74 
75 	len = skb->len
76 #ifdef CONFIG_ISDN_AUDIO
77 		+ ISDN_AUDIO_SKB_DLECOUNT(skb)
78 #endif
79 		;
80 
81 	c = tty_buffer_request_room(port, len);
82 	if (c < len)
83 		return 0;
84 
85 #ifdef CONFIG_ISDN_AUDIO
86 	if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
87 		int l = skb->len;
88 		unsigned char *dp = skb->data;
89 		while (--l) {
90 			if (*dp == DLE)
91 				tty_insert_flip_char(port, DLE, 0);
92 			tty_insert_flip_char(port, *dp++, 0);
93 		}
94 		if (*dp == DLE)
95 			tty_insert_flip_char(port, DLE, 0);
96 		last = *dp;
97 	} else {
98 #endif
99 		if (len > 1)
100 			tty_insert_flip_string(port, skb->data, len - 1);
101 		last = skb->data[len - 1];
102 #ifdef CONFIG_ISDN_AUDIO
103 	}
104 #endif
105 	if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
106 		tty_insert_flip_char(port, last, 0xFF);
107 	else
108 		tty_insert_flip_char(port, last, TTY_NORMAL);
109 	tty_flip_buffer_push(port);
110 	kfree_skb(skb);
111 
112 	return 1;
113 }
114 
115 /* isdn_tty_readmodem() is called periodically from within timer-interrupt.
116  * It tries getting received data from the receive queue an stuff it into
117  * the tty's flip-buffer.
118  */
119 void
isdn_tty_readmodem(void)120 isdn_tty_readmodem(void)
121 {
122 	int resched = 0;
123 	int midx;
124 	int i;
125 	int r;
126 	modem_info *info;
127 
128 	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
129 		midx = dev->m_idx[i];
130 		if (midx < 0)
131 			continue;
132 
133 		info = &dev->mdm.info[midx];
134 		if (!info->online)
135 			continue;
136 
137 		r = 0;
138 #ifdef CONFIG_ISDN_AUDIO
139 		isdn_audio_eval_dtmf(info);
140 		if ((info->vonline & 1) && (info->emu.vpar[1]))
141 			isdn_audio_eval_silence(info);
142 #endif
143 		if (info->mcr & UART_MCR_RTS) {
144 			/* CISCO AsyncPPP Hack */
145 			if (!(info->emu.mdmreg[REG_CPPP] & BIT_CPPP))
146 				r = isdn_readbchan_tty(info->isdn_driver,
147 						info->isdn_channel,
148 						&info->port, 0);
149 			else
150 				r = isdn_readbchan_tty(info->isdn_driver,
151 						info->isdn_channel,
152 						&info->port, 1);
153 			if (r)
154 				tty_flip_buffer_push(&info->port);
155 		} else
156 			r = 1;
157 
158 		if (r) {
159 			info->rcvsched = 0;
160 			resched = 1;
161 		} else
162 			info->rcvsched = 1;
163 	}
164 	if (!resched)
165 		isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
166 }
167 
168 int
isdn_tty_rcv_skb(int i,int di,int channel,struct sk_buff * skb)169 isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
170 {
171 	ulong flags;
172 	int midx;
173 #ifdef CONFIG_ISDN_AUDIO
174 	int ifmt;
175 #endif
176 	modem_info *info;
177 
178 	if ((midx = dev->m_idx[i]) < 0) {
179 		/* if midx is invalid, packet is not for tty */
180 		return 0;
181 	}
182 	info = &dev->mdm.info[midx];
183 #ifdef CONFIG_ISDN_AUDIO
184 	ifmt = 1;
185 
186 	if ((info->vonline) && (!info->emu.vpar[4]))
187 		isdn_audio_calc_dtmf(info, skb->data, skb->len, ifmt);
188 	if ((info->vonline & 1) && (info->emu.vpar[1]))
189 		isdn_audio_calc_silence(info, skb->data, skb->len, ifmt);
190 #endif
191 	if ((info->online < 2)
192 #ifdef CONFIG_ISDN_AUDIO
193 	    && (!(info->vonline & 1))
194 #endif
195 		) {
196 		/* If Modem not listening, drop data */
197 		kfree_skb(skb);
198 		return 1;
199 	}
200 	if (info->emu.mdmreg[REG_T70] & BIT_T70) {
201 		if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT) {
202 			/* T.70 decoding: throw away the T.70 header (2 or 4 bytes)   */
203 			if (skb->data[0] == 3) /* pure data packet -> 4 byte headers  */
204 				skb_pull(skb, 4);
205 			else
206 				if (skb->data[0] == 1) /* keepalive packet -> 2 byte hdr  */
207 					skb_pull(skb, 2);
208 		} else
209 			/* T.70 decoding: Simply throw away the T.70 header (4 bytes) */
210 			if ((skb->data[0] == 1) && ((skb->data[1] == 0) || (skb->data[1] == 1)))
211 				skb_pull(skb, 4);
212 	}
213 #ifdef CONFIG_ISDN_AUDIO
214 	ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
215 	ISDN_AUDIO_SKB_LOCK(skb) = 0;
216 	if (info->vonline & 1) {
217 		/* voice conversion/compression */
218 		switch (info->emu.vpar[3]) {
219 		case 2:
220 		case 3:
221 		case 4:
222 			/* adpcm
223 			 * Since compressed data takes less
224 			 * space, we can overwrite the buffer.
225 			 */
226 			skb_trim(skb, isdn_audio_xlaw2adpcm(info->adpcmr,
227 							    ifmt,
228 							    skb->data,
229 							    skb->data,
230 							    skb->len));
231 			break;
232 		case 5:
233 			/* a-law */
234 			if (!ifmt)
235 				isdn_audio_ulaw2alaw(skb->data, skb->len);
236 			break;
237 		case 6:
238 			/* u-law */
239 			if (ifmt)
240 				isdn_audio_alaw2ulaw(skb->data, skb->len);
241 			break;
242 		}
243 		ISDN_AUDIO_SKB_DLECOUNT(skb) =
244 			isdn_tty_countDLE(skb->data, skb->len);
245 	}
246 #ifdef CONFIG_ISDN_TTY_FAX
247 	else {
248 		if (info->faxonline & 2) {
249 			isdn_tty_fax_bitorder(info, skb);
250 			ISDN_AUDIO_SKB_DLECOUNT(skb) =
251 				isdn_tty_countDLE(skb->data, skb->len);
252 		}
253 	}
254 #endif
255 #endif
256 	/* Try to deliver directly via tty-buf if queue is empty */
257 	spin_lock_irqsave(&info->readlock, flags);
258 	if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
259 		if (isdn_tty_try_read(info, skb)) {
260 			spin_unlock_irqrestore(&info->readlock, flags);
261 			return 1;
262 		}
263 	/* Direct deliver failed or queue wasn't empty.
264 	 * Queue up for later dequeueing via timer-irq.
265 	 */
266 	__skb_queue_tail(&dev->drv[di]->rpqueue[channel], skb);
267 	dev->drv[di]->rcvcount[channel] +=
268 		(skb->len
269 #ifdef CONFIG_ISDN_AUDIO
270 		 + ISDN_AUDIO_SKB_DLECOUNT(skb)
271 #endif
272 			);
273 	spin_unlock_irqrestore(&info->readlock, flags);
274 	/* Schedule dequeuing */
275 	if ((dev->modempoll) && (info->rcvsched))
276 		isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
277 	return 1;
278 }
279 
280 static void
isdn_tty_cleanup_xmit(modem_info * info)281 isdn_tty_cleanup_xmit(modem_info *info)
282 {
283 	skb_queue_purge(&info->xmit_queue);
284 #ifdef CONFIG_ISDN_AUDIO
285 	skb_queue_purge(&info->dtmf_queue);
286 #endif
287 }
288 
289 static void
isdn_tty_tint(modem_info * info)290 isdn_tty_tint(modem_info *info)
291 {
292 	struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
293 	int len, slen;
294 
295 	if (!skb)
296 		return;
297 	len = skb->len;
298 	if ((slen = isdn_writebuf_skb_stub(info->isdn_driver,
299 					   info->isdn_channel, 1, skb)) == len) {
300 		struct tty_struct *tty = info->port.tty;
301 		info->send_outstanding++;
302 		info->msr &= ~UART_MSR_CTS;
303 		info->lsr &= ~UART_LSR_TEMT;
304 		tty_wakeup(tty);
305 		return;
306 	}
307 	if (slen < 0) {
308 		/* Error: no channel, already shutdown, or wrong parameter */
309 		dev_kfree_skb(skb);
310 		return;
311 	}
312 	skb_queue_head(&info->xmit_queue, skb);
313 }
314 
315 #ifdef CONFIG_ISDN_AUDIO
316 static int
isdn_tty_countDLE(unsigned char * buf,int len)317 isdn_tty_countDLE(unsigned char *buf, int len)
318 {
319 	int count = 0;
320 
321 	while (len--)
322 		if (*buf++ == DLE)
323 			count++;
324 	return count;
325 }
326 
327 /* This routine is called from within isdn_tty_write() to perform
328  * DLE-decoding when sending audio-data.
329  */
330 static int
isdn_tty_handleDLEdown(modem_info * info,atemu * m,int len)331 isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
332 {
333 	unsigned char *p = &info->port.xmit_buf[info->xmit_count];
334 	int count = 0;
335 
336 	while (len > 0) {
337 		if (m->lastDLE) {
338 			m->lastDLE = 0;
339 			switch (*p) {
340 			case DLE:
341 				/* Escape code */
342 				if (len > 1)
343 					memmove(p, p + 1, len - 1);
344 				p--;
345 				count++;
346 				break;
347 			case ETX:
348 				/* End of data */
349 				info->vonline |= 4;
350 				return count;
351 			case DC4:
352 				/* Abort RX */
353 				info->vonline &= ~1;
354 #ifdef ISDN_DEBUG_MODEM_VOICE
355 				printk(KERN_DEBUG
356 				       "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
357 				       info->line);
358 #endif
359 				isdn_tty_at_cout("\020\003", info);
360 				if (!info->vonline) {
361 #ifdef ISDN_DEBUG_MODEM_VOICE
362 					printk(KERN_DEBUG
363 					       "DLEdown: send VCON on ttyI%d\n",
364 					       info->line);
365 #endif
366 					isdn_tty_at_cout("\r\nVCON\r\n", info);
367 				}
368 				/* Fall through */
369 			case 'q':
370 			case 's':
371 				/* Silence */
372 				if (len > 1)
373 					memmove(p, p + 1, len - 1);
374 				p--;
375 				break;
376 			}
377 		} else {
378 			if (*p == DLE)
379 				m->lastDLE = 1;
380 			else
381 				count++;
382 		}
383 		p++;
384 		len--;
385 	}
386 	if (len < 0) {
387 		printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
388 		return 0;
389 	}
390 	return count;
391 }
392 
393 /* This routine is called from within isdn_tty_write() when receiving
394  * audio-data. It interrupts receiving, if an character other than
395  * ^S or ^Q is sent.
396  */
397 static int
isdn_tty_end_vrx(const char * buf,int c)398 isdn_tty_end_vrx(const char *buf, int c)
399 {
400 	char ch;
401 
402 	while (c--) {
403 		ch = *buf;
404 		if ((ch != 0x11) && (ch != 0x13))
405 			return 1;
406 		buf++;
407 	}
408 	return 0;
409 }
410 
411 static int voice_cf[7] =
412 {0, 0, 4, 3, 2, 0, 0};
413 
414 #endif                          /* CONFIG_ISDN_AUDIO */
415 
416 /* isdn_tty_senddown() is called either directly from within isdn_tty_write()
417  * or via timer-interrupt from within isdn_tty_modem_xmit(). It pulls
418  * outgoing data from the tty's xmit-buffer, handles voice-decompression or
419  * T.70 if necessary, and finally queues it up for sending via isdn_tty_tint.
420  */
421 static void
isdn_tty_senddown(modem_info * info)422 isdn_tty_senddown(modem_info *info)
423 {
424 	int buflen;
425 	int skb_res;
426 #ifdef CONFIG_ISDN_AUDIO
427 	int audio_len;
428 #endif
429 	struct sk_buff *skb;
430 
431 #ifdef CONFIG_ISDN_AUDIO
432 	if (info->vonline & 4) {
433 		info->vonline &= ~6;
434 		if (!info->vonline) {
435 #ifdef ISDN_DEBUG_MODEM_VOICE
436 			printk(KERN_DEBUG
437 			       "senddown: send VCON on ttyI%d\n",
438 			       info->line);
439 #endif
440 			isdn_tty_at_cout("\r\nVCON\r\n", info);
441 		}
442 	}
443 #endif
444 	if (!(buflen = info->xmit_count))
445 		return;
446 	if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
447 		info->msr &= ~UART_MSR_CTS;
448 	info->lsr &= ~UART_LSR_TEMT;
449 	/* info->xmit_count is modified here and in isdn_tty_write().
450 	 * So we return here if isdn_tty_write() is in the
451 	 * critical section.
452 	 */
453 	atomic_inc(&info->xmit_lock);
454 	if (!(atomic_dec_and_test(&info->xmit_lock)))
455 		return;
456 	if (info->isdn_driver < 0) {
457 		info->xmit_count = 0;
458 		return;
459 	}
460 	skb_res = dev->drv[info->isdn_driver]->interface->hl_hdrlen + 4;
461 #ifdef CONFIG_ISDN_AUDIO
462 	if (info->vonline & 2)
463 		audio_len = buflen * voice_cf[info->emu.vpar[3]];
464 	else
465 		audio_len = 0;
466 	skb = dev_alloc_skb(skb_res + buflen + audio_len);
467 #else
468 	skb = dev_alloc_skb(skb_res + buflen);
469 #endif
470 	if (!skb) {
471 		printk(KERN_WARNING
472 		       "isdn_tty: Out of memory in ttyI%d senddown\n",
473 		       info->line);
474 		return;
475 	}
476 	skb_reserve(skb, skb_res);
477 	skb_put_data(skb, info->port.xmit_buf, buflen);
478 	info->xmit_count = 0;
479 #ifdef CONFIG_ISDN_AUDIO
480 	if (info->vonline & 2) {
481 		/* For now, ifmt is fixed to 1 (alaw), since this
482 		 * is used with ISDN everywhere in the world, except
483 		 * US, Canada and Japan.
484 		 * Later, when US-ISDN protocols are implemented,
485 		 * this setting will depend on the D-channel protocol.
486 		 */
487 		int ifmt = 1;
488 
489 		/* voice conversion/decompression */
490 		switch (info->emu.vpar[3]) {
491 		case 2:
492 		case 3:
493 		case 4:
494 			/* adpcm, compatible to ZyXel 1496 modem
495 			 * with ROM revision 6.01
496 			 */
497 			audio_len = isdn_audio_adpcm2xlaw(info->adpcms,
498 							  ifmt,
499 							  skb->data,
500 							  skb_put(skb, audio_len),
501 							  buflen);
502 			skb_pull(skb, buflen);
503 			skb_trim(skb, audio_len);
504 			break;
505 		case 5:
506 			/* a-law */
507 			if (!ifmt)
508 				isdn_audio_alaw2ulaw(skb->data,
509 						     buflen);
510 			break;
511 		case 6:
512 			/* u-law */
513 			if (ifmt)
514 				isdn_audio_ulaw2alaw(skb->data,
515 						     buflen);
516 			break;
517 		}
518 	}
519 #endif                          /* CONFIG_ISDN_AUDIO */
520 	if (info->emu.mdmreg[REG_T70] & BIT_T70) {
521 		/* Add T.70 simplified header */
522 		if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT)
523 			memcpy(skb_push(skb, 2), "\1\0", 2);
524 		else
525 			memcpy(skb_push(skb, 4), "\1\0\1\0", 4);
526 	}
527 	skb_queue_tail(&info->xmit_queue, skb);
528 }
529 
530 /************************************************************
531  *
532  * Modem-functions
533  *
534  * mostly "stolen" from original Linux-serial.c and friends.
535  *
536  ************************************************************/
537 
538 /* The next routine is called once from within timer-interrupt
539  * triggered within isdn_tty_modem_ncarrier(). It calls
540  * isdn_tty_modem_result() to stuff a "NO CARRIER" Message
541  * into the tty's buffer.
542  */
543 static void
isdn_tty_modem_do_ncarrier(struct timer_list * t)544 isdn_tty_modem_do_ncarrier(struct timer_list *t)
545 {
546 	modem_info *info = from_timer(info, t, nc_timer);
547 	isdn_tty_modem_result(RESULT_NO_CARRIER, info);
548 }
549 
550 /* Next routine is called, whenever the DTR-signal is raised.
551  * It checks the ncarrier-flag, and triggers the above routine
552  * when necessary. The ncarrier-flag is set, whenever DTR goes
553  * low.
554  */
555 static void
isdn_tty_modem_ncarrier(modem_info * info)556 isdn_tty_modem_ncarrier(modem_info *info)
557 {
558 	if (info->ncarrier) {
559 		info->nc_timer.expires = jiffies + HZ;
560 		add_timer(&info->nc_timer);
561 	}
562 }
563 
564 /*
565  * return the usage calculated by si and layer 2 protocol
566  */
567 static int
isdn_calc_usage(int si,int l2)568 isdn_calc_usage(int si, int l2)
569 {
570 	int usg = ISDN_USAGE_MODEM;
571 
572 #ifdef CONFIG_ISDN_AUDIO
573 	if (si == 1) {
574 		switch (l2) {
575 		case ISDN_PROTO_L2_MODEM:
576 			usg = ISDN_USAGE_MODEM;
577 			break;
578 #ifdef CONFIG_ISDN_TTY_FAX
579 		case ISDN_PROTO_L2_FAX:
580 			usg = ISDN_USAGE_FAX;
581 			break;
582 #endif
583 		case ISDN_PROTO_L2_TRANS:
584 		default:
585 			usg = ISDN_USAGE_VOICE;
586 			break;
587 		}
588 	}
589 #endif
590 	return (usg);
591 }
592 
593 /* isdn_tty_dial() performs dialing of a tty an the necessary
594  * setup of the lower levels before that.
595  */
596 static void
isdn_tty_dial(char * n,modem_info * info,atemu * m)597 isdn_tty_dial(char *n, modem_info *info, atemu *m)
598 {
599 	int usg = ISDN_USAGE_MODEM;
600 	int si = 7;
601 	int l2 = m->mdmreg[REG_L2PROT];
602 	u_long flags;
603 	isdn_ctrl cmd;
604 	int i;
605 	int j;
606 
607 	for (j = 7; j >= 0; j--)
608 		if (m->mdmreg[REG_SI1] & (1 << j)) {
609 			si = bit2si[j];
610 			break;
611 		}
612 	usg = isdn_calc_usage(si, l2);
613 #ifdef CONFIG_ISDN_AUDIO
614 	if ((si == 1) &&
615 	    (l2 != ISDN_PROTO_L2_MODEM)
616 #ifdef CONFIG_ISDN_TTY_FAX
617 	    && (l2 != ISDN_PROTO_L2_FAX)
618 #endif
619 		) {
620 		l2 = ISDN_PROTO_L2_TRANS;
621 		usg = ISDN_USAGE_VOICE;
622 	}
623 #endif
624 	m->mdmreg[REG_SI1I] = si2bit[si];
625 	spin_lock_irqsave(&dev->lock, flags);
626 	i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
627 	if (i < 0) {
628 		spin_unlock_irqrestore(&dev->lock, flags);
629 		isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
630 	} else {
631 		info->isdn_driver = dev->drvmap[i];
632 		info->isdn_channel = dev->chanmap[i];
633 		info->drv_index = i;
634 		dev->m_idx[i] = info->line;
635 		dev->usage[i] |= ISDN_USAGE_OUTGOING;
636 		info->last_dir = 1;
637 		strcpy(info->last_num, n);
638 		isdn_info_update();
639 		spin_unlock_irqrestore(&dev->lock, flags);
640 		cmd.driver = info->isdn_driver;
641 		cmd.arg = info->isdn_channel;
642 		cmd.command = ISDN_CMD_CLREAZ;
643 		isdn_command(&cmd);
644 		strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
645 		cmd.driver = info->isdn_driver;
646 		cmd.command = ISDN_CMD_SETEAZ;
647 		isdn_command(&cmd);
648 		cmd.driver = info->isdn_driver;
649 		cmd.command = ISDN_CMD_SETL2;
650 		info->last_l2 = l2;
651 		cmd.arg = info->isdn_channel + (l2 << 8);
652 		isdn_command(&cmd);
653 		cmd.driver = info->isdn_driver;
654 		cmd.command = ISDN_CMD_SETL3;
655 		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
656 #ifdef CONFIG_ISDN_TTY_FAX
657 		if (l2 == ISDN_PROTO_L2_FAX) {
658 			cmd.parm.fax = info->fax;
659 			info->fax->direction = ISDN_TTY_FAX_CONN_OUT;
660 		}
661 #endif
662 		isdn_command(&cmd);
663 		cmd.driver = info->isdn_driver;
664 		cmd.arg = info->isdn_channel;
665 		sprintf(cmd.parm.setup.phone, "%s", n);
666 		sprintf(cmd.parm.setup.eazmsn, "%s",
667 			isdn_map_eaz2msn(m->msn, info->isdn_driver));
668 		cmd.parm.setup.si1 = si;
669 		cmd.parm.setup.si2 = m->mdmreg[REG_SI2];
670 		cmd.command = ISDN_CMD_DIAL;
671 		info->dialing = 1;
672 		info->emu.carrierwait = 0;
673 		strcpy(dev->num[i], n);
674 		isdn_info_update();
675 		isdn_command(&cmd);
676 		isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
677 	}
678 }
679 
680 /* isdn_tty_hangup() disassociates a tty from the real
681  * ISDN-line (hangup). The usage-status is cleared
682  * and some cleanup is done also.
683  */
684 void
isdn_tty_modem_hup(modem_info * info,int local)685 isdn_tty_modem_hup(modem_info *info, int local)
686 {
687 	isdn_ctrl cmd;
688 	int di, ch;
689 
690 	if (!info)
691 		return;
692 
693 	di = info->isdn_driver;
694 	ch = info->isdn_channel;
695 	if (di < 0 || ch < 0)
696 		return;
697 
698 	info->isdn_driver = -1;
699 	info->isdn_channel = -1;
700 
701 #ifdef ISDN_DEBUG_MODEM_HUP
702 	printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
703 #endif
704 	info->rcvsched = 0;
705 	isdn_tty_flush_buffer(info->port.tty);
706 	if (info->online) {
707 		info->last_lhup = local;
708 		info->online = 0;
709 		isdn_tty_modem_result(RESULT_NO_CARRIER, info);
710 	}
711 #ifdef CONFIG_ISDN_AUDIO
712 	info->vonline = 0;
713 #ifdef CONFIG_ISDN_TTY_FAX
714 	info->faxonline = 0;
715 	info->fax->phase = ISDN_FAX_PHASE_IDLE;
716 #endif
717 	info->emu.vpar[4] = 0;
718 	info->emu.vpar[5] = 8;
719 	kfree(info->dtmf_state);
720 	info->dtmf_state = NULL;
721 	kfree(info->silence_state);
722 	info->silence_state = NULL;
723 	kfree(info->adpcms);
724 	info->adpcms = NULL;
725 	kfree(info->adpcmr);
726 	info->adpcmr = NULL;
727 #endif
728 	if ((info->msr & UART_MSR_RI) &&
729 	    (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
730 		isdn_tty_modem_result(RESULT_RUNG, info);
731 	info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
732 	info->lsr |= UART_LSR_TEMT;
733 
734 	if (local) {
735 		cmd.driver = di;
736 		cmd.command = ISDN_CMD_HANGUP;
737 		cmd.arg = ch;
738 		isdn_command(&cmd);
739 	}
740 
741 	isdn_all_eaz(di, ch);
742 	info->emu.mdmreg[REG_RINGCNT] = 0;
743 	isdn_free_channel(di, ch, 0);
744 
745 	if (info->drv_index >= 0) {
746 		dev->m_idx[info->drv_index] = -1;
747 		info->drv_index = -1;
748 	}
749 }
750 
751 /*
752  * Begin of a CAPI like interface, currently used only for
753  * supplementary service (CAPI 2.0 part III)
754  */
755 #include <linux/isdn/capicmd.h>
756 #include <linux/module.h>
757 
758 int
isdn_tty_capi_facility(capi_msg * cm)759 isdn_tty_capi_facility(capi_msg *cm) {
760 	return (-1); /* dummy */
761 }
762 
763 /* isdn_tty_suspend() tries to suspend the current tty connection
764  */
765 static void
isdn_tty_suspend(char * id,modem_info * info,atemu * m)766 isdn_tty_suspend(char *id, modem_info *info, atemu *m)
767 {
768 	isdn_ctrl cmd;
769 
770 	int l;
771 
772 	if (!info)
773 		return;
774 
775 #ifdef ISDN_DEBUG_MODEM_SERVICES
776 	printk(KERN_DEBUG "Msusp ttyI%d\n", info->line);
777 #endif
778 	l = strlen(id);
779 	if ((info->isdn_driver >= 0)) {
780 		cmd.parm.cmsg.Length = l + 18;
781 		cmd.parm.cmsg.Command = CAPI_FACILITY;
782 		cmd.parm.cmsg.Subcommand = CAPI_REQ;
783 		cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
784 		cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
785 		cmd.parm.cmsg.para[1] = 0;
786 		cmd.parm.cmsg.para[2] = l + 3;
787 		cmd.parm.cmsg.para[3] = 4; /* 16 bit 0x0004 Suspend */
788 		cmd.parm.cmsg.para[4] = 0;
789 		cmd.parm.cmsg.para[5] = l;
790 		memcpy(&cmd.parm.cmsg.para[6], id, l);
791 		cmd.command = CAPI_PUT_MESSAGE;
792 		cmd.driver = info->isdn_driver;
793 		cmd.arg = info->isdn_channel;
794 		isdn_command(&cmd);
795 	}
796 }
797 
798 /* isdn_tty_resume() tries to resume a suspended call
799  * setup of the lower levels before that. unfortunately here is no
800  * checking for compatibility of used protocols implemented by Q931
801  * It does the same things like isdn_tty_dial, the last command
802  * is different, may be we can merge it.
803  */
804 
805 static void
isdn_tty_resume(char * id,modem_info * info,atemu * m)806 isdn_tty_resume(char *id, modem_info *info, atemu *m)
807 {
808 	int usg = ISDN_USAGE_MODEM;
809 	int si = 7;
810 	int l2 = m->mdmreg[REG_L2PROT];
811 	isdn_ctrl cmd;
812 	ulong flags;
813 	int i;
814 	int j;
815 	int l;
816 
817 	l = strlen(id);
818 	for (j = 7; j >= 0; j--)
819 		if (m->mdmreg[REG_SI1] & (1 << j)) {
820 			si = bit2si[j];
821 			break;
822 		}
823 	usg = isdn_calc_usage(si, l2);
824 #ifdef CONFIG_ISDN_AUDIO
825 	if ((si == 1) &&
826 	    (l2 != ISDN_PROTO_L2_MODEM)
827 #ifdef CONFIG_ISDN_TTY_FAX
828 	    && (l2 != ISDN_PROTO_L2_FAX)
829 #endif
830 		) {
831 		l2 = ISDN_PROTO_L2_TRANS;
832 		usg = ISDN_USAGE_VOICE;
833 	}
834 #endif
835 	m->mdmreg[REG_SI1I] = si2bit[si];
836 	spin_lock_irqsave(&dev->lock, flags);
837 	i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
838 	if (i < 0) {
839 		spin_unlock_irqrestore(&dev->lock, flags);
840 		isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
841 	} else {
842 		info->isdn_driver = dev->drvmap[i];
843 		info->isdn_channel = dev->chanmap[i];
844 		info->drv_index = i;
845 		dev->m_idx[i] = info->line;
846 		dev->usage[i] |= ISDN_USAGE_OUTGOING;
847 		info->last_dir = 1;
848 //		strcpy(info->last_num, n);
849 		isdn_info_update();
850 		spin_unlock_irqrestore(&dev->lock, flags);
851 		cmd.driver = info->isdn_driver;
852 		cmd.arg = info->isdn_channel;
853 		cmd.command = ISDN_CMD_CLREAZ;
854 		isdn_command(&cmd);
855 		strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
856 		cmd.driver = info->isdn_driver;
857 		cmd.command = ISDN_CMD_SETEAZ;
858 		isdn_command(&cmd);
859 		cmd.driver = info->isdn_driver;
860 		cmd.command = ISDN_CMD_SETL2;
861 		info->last_l2 = l2;
862 		cmd.arg = info->isdn_channel + (l2 << 8);
863 		isdn_command(&cmd);
864 		cmd.driver = info->isdn_driver;
865 		cmd.command = ISDN_CMD_SETL3;
866 		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
867 		isdn_command(&cmd);
868 		cmd.driver = info->isdn_driver;
869 		cmd.arg = info->isdn_channel;
870 		cmd.parm.cmsg.Length = l + 18;
871 		cmd.parm.cmsg.Command = CAPI_FACILITY;
872 		cmd.parm.cmsg.Subcommand = CAPI_REQ;
873 		cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
874 		cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
875 		cmd.parm.cmsg.para[1] = 0;
876 		cmd.parm.cmsg.para[2] = l + 3;
877 		cmd.parm.cmsg.para[3] = 5; /* 16 bit 0x0005 Resume */
878 		cmd.parm.cmsg.para[4] = 0;
879 		cmd.parm.cmsg.para[5] = l;
880 		memcpy(&cmd.parm.cmsg.para[6], id, l);
881 		cmd.command = CAPI_PUT_MESSAGE;
882 		info->dialing = 1;
883 //		strcpy(dev->num[i], n);
884 		isdn_info_update();
885 		isdn_command(&cmd);
886 		isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
887 	}
888 }
889 
890 /* isdn_tty_send_msg() sends a message to a HL driver
891  * This is used for hybrid modem cards to send AT commands to it
892  */
893 
894 static void
isdn_tty_send_msg(modem_info * info,atemu * m,char * msg)895 isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
896 {
897 	int usg = ISDN_USAGE_MODEM;
898 	int si = 7;
899 	int l2 = m->mdmreg[REG_L2PROT];
900 	isdn_ctrl cmd;
901 	ulong flags;
902 	int i;
903 	int j;
904 	int l;
905 
906 	l = min(strlen(msg), sizeof(cmd.parm) - sizeof(cmd.parm.cmsg)
907 		+ sizeof(cmd.parm.cmsg.para) - 2);
908 
909 	if (!l) {
910 		isdn_tty_modem_result(RESULT_ERROR, info);
911 		return;
912 	}
913 	for (j = 7; j >= 0; j--)
914 		if (m->mdmreg[REG_SI1] & (1 << j)) {
915 			si = bit2si[j];
916 			break;
917 		}
918 	usg = isdn_calc_usage(si, l2);
919 #ifdef CONFIG_ISDN_AUDIO
920 	if ((si == 1) &&
921 	    (l2 != ISDN_PROTO_L2_MODEM)
922 #ifdef CONFIG_ISDN_TTY_FAX
923 	    && (l2 != ISDN_PROTO_L2_FAX)
924 #endif
925 		) {
926 		l2 = ISDN_PROTO_L2_TRANS;
927 		usg = ISDN_USAGE_VOICE;
928 	}
929 #endif
930 	m->mdmreg[REG_SI1I] = si2bit[si];
931 	spin_lock_irqsave(&dev->lock, flags);
932 	i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
933 	if (i < 0) {
934 		spin_unlock_irqrestore(&dev->lock, flags);
935 		isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
936 	} else {
937 		info->isdn_driver = dev->drvmap[i];
938 		info->isdn_channel = dev->chanmap[i];
939 		info->drv_index = i;
940 		dev->m_idx[i] = info->line;
941 		dev->usage[i] |= ISDN_USAGE_OUTGOING;
942 		info->last_dir = 1;
943 		isdn_info_update();
944 		spin_unlock_irqrestore(&dev->lock, flags);
945 		cmd.driver = info->isdn_driver;
946 		cmd.arg = info->isdn_channel;
947 		cmd.command = ISDN_CMD_CLREAZ;
948 		isdn_command(&cmd);
949 		strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
950 		cmd.driver = info->isdn_driver;
951 		cmd.command = ISDN_CMD_SETEAZ;
952 		isdn_command(&cmd);
953 		cmd.driver = info->isdn_driver;
954 		cmd.command = ISDN_CMD_SETL2;
955 		info->last_l2 = l2;
956 		cmd.arg = info->isdn_channel + (l2 << 8);
957 		isdn_command(&cmd);
958 		cmd.driver = info->isdn_driver;
959 		cmd.command = ISDN_CMD_SETL3;
960 		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
961 		isdn_command(&cmd);
962 		cmd.driver = info->isdn_driver;
963 		cmd.arg = info->isdn_channel;
964 		cmd.parm.cmsg.Length = l + 14;
965 		cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
966 		cmd.parm.cmsg.Subcommand = CAPI_REQ;
967 		cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
968 		cmd.parm.cmsg.para[0] = l + 1;
969 		strncpy(&cmd.parm.cmsg.para[1], msg, l);
970 		cmd.parm.cmsg.para[l + 1] = 0xd;
971 		cmd.command = CAPI_PUT_MESSAGE;
972 /*		info->dialing = 1;
973 		strcpy(dev->num[i], n);
974 		isdn_info_update();
975 */
976 		isdn_command(&cmd);
977 	}
978 }
979 
980 static inline int
isdn_tty_paranoia_check(modem_info * info,char * name,const char * routine)981 isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
982 {
983 #ifdef MODEM_PARANOIA_CHECK
984 	if (!info) {
985 		printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
986 		       name, routine);
987 		return 1;
988 	}
989 	if (info->magic != ISDN_ASYNC_MAGIC) {
990 		printk(KERN_WARNING "isdn_tty: bad magic for modem struct %s in %s\n",
991 		       name, routine);
992 		return 1;
993 	}
994 #endif
995 	return 0;
996 }
997 
998 /*
999  * This routine is called to set the UART divisor registers to match
1000  * the specified baud rate for a serial port.
1001  */
1002 static void
isdn_tty_change_speed(modem_info * info)1003 isdn_tty_change_speed(modem_info *info)
1004 {
1005 	struct tty_port *port = &info->port;
1006 	uint cflag,
1007 		cval,
1008 		quot;
1009 	int i;
1010 
1011 	if (!port->tty)
1012 		return;
1013 	cflag = port->tty->termios.c_cflag;
1014 
1015 	quot = i = cflag & CBAUD;
1016 	if (i & CBAUDEX) {
1017 		i &= ~CBAUDEX;
1018 		if (i < 1 || i > 2)
1019 			port->tty->termios.c_cflag &= ~CBAUDEX;
1020 		else
1021 			i += 15;
1022 	}
1023 	if (quot) {
1024 		info->mcr |= UART_MCR_DTR;
1025 		isdn_tty_modem_ncarrier(info);
1026 	} else {
1027 		info->mcr &= ~UART_MCR_DTR;
1028 		if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1029 #ifdef ISDN_DEBUG_MODEM_HUP
1030 			printk(KERN_DEBUG "Mhup in changespeed\n");
1031 #endif
1032 			if (info->online)
1033 				info->ncarrier = 1;
1034 			isdn_tty_modem_reset_regs(info, 0);
1035 			isdn_tty_modem_hup(info, 1);
1036 		}
1037 		return;
1038 	}
1039 	/* byte size and parity */
1040 	cval = cflag & (CSIZE | CSTOPB);
1041 	cval >>= 4;
1042 	if (cflag & PARENB)
1043 		cval |= UART_LCR_PARITY;
1044 	if (!(cflag & PARODD))
1045 		cval |= UART_LCR_EPAR;
1046 
1047 	tty_port_set_check_carrier(port, ~cflag & CLOCAL);
1048 }
1049 
1050 static int
isdn_tty_startup(modem_info * info)1051 isdn_tty_startup(modem_info *info)
1052 {
1053 	if (tty_port_initialized(&info->port))
1054 		return 0;
1055 	isdn_lock_drivers();
1056 #ifdef ISDN_DEBUG_MODEM_OPEN
1057 	printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
1058 #endif
1059 	/*
1060 	 * Now, initialize the UART
1061 	 */
1062 	info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
1063 	if (info->port.tty)
1064 		clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
1065 	/*
1066 	 * and set the speed of the serial port
1067 	 */
1068 	isdn_tty_change_speed(info);
1069 
1070 	tty_port_set_initialized(&info->port, 1);
1071 	info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
1072 	info->send_outstanding = 0;
1073 	return 0;
1074 }
1075 
1076 /*
1077  * This routine will shutdown a serial port; interrupts are disabled, and
1078  * DTR is dropped if the hangup on close termio flag is on.
1079  */
1080 static void
isdn_tty_shutdown(modem_info * info)1081 isdn_tty_shutdown(modem_info *info)
1082 {
1083 	if (!tty_port_initialized(&info->port))
1084 		return;
1085 #ifdef ISDN_DEBUG_MODEM_OPEN
1086 	printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
1087 #endif
1088 	isdn_unlock_drivers();
1089 	info->msr &= ~UART_MSR_RI;
1090 	if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
1091 		info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
1092 		if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1093 			isdn_tty_modem_reset_regs(info, 0);
1094 #ifdef ISDN_DEBUG_MODEM_HUP
1095 			printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
1096 #endif
1097 			isdn_tty_modem_hup(info, 1);
1098 		}
1099 	}
1100 	if (info->port.tty)
1101 		set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1102 
1103 	tty_port_set_initialized(&info->port, 0);
1104 }
1105 
1106 /* isdn_tty_write() is the main send-routine. It is called from the upper
1107  * levels within the kernel to perform sending data. Depending on the
1108  * online-flag it either directs output to the at-command-interpreter or
1109  * to the lower level. Additional tasks done here:
1110  *  - If online, check for escape-sequence (+++)
1111  *  - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
1112  *  - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
1113  *  - If dialing, abort dial.
1114  */
1115 static int
isdn_tty_write(struct tty_struct * tty,const u_char * buf,int count)1116 isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
1117 {
1118 	int c;
1119 	int total = 0;
1120 	modem_info *info = (modem_info *) tty->driver_data;
1121 	atemu *m = &info->emu;
1122 
1123 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write"))
1124 		return 0;
1125 	/* See isdn_tty_senddown() */
1126 	atomic_inc(&info->xmit_lock);
1127 	while (1) {
1128 		c = count;
1129 		if (c > info->xmit_size - info->xmit_count)
1130 			c = info->xmit_size - info->xmit_count;
1131 		if (info->isdn_driver >= 0 && c > dev->drv[info->isdn_driver]->maxbufsize)
1132 			c = dev->drv[info->isdn_driver]->maxbufsize;
1133 		if (c <= 0)
1134 			break;
1135 		if ((info->online > 1)
1136 #ifdef CONFIG_ISDN_AUDIO
1137 		    || (info->vonline & 3)
1138 #endif
1139 			) {
1140 #ifdef CONFIG_ISDN_AUDIO
1141 			if (!info->vonline)
1142 #endif
1143 				isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
1144 						   &(m->pluscount),
1145 						   &(m->lastplus));
1146 			memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
1147 #ifdef CONFIG_ISDN_AUDIO
1148 			if (info->vonline) {
1149 				int cc = isdn_tty_handleDLEdown(info, m, c);
1150 				if (info->vonline & 2) {
1151 					if (!cc) {
1152 						/* If DLE decoding results in zero-transmit, but
1153 						 * c originally was non-zero, do a wakeup.
1154 						 */
1155 						tty_wakeup(tty);
1156 						info->msr |= UART_MSR_CTS;
1157 						info->lsr |= UART_LSR_TEMT;
1158 					}
1159 					info->xmit_count += cc;
1160 				}
1161 				if ((info->vonline & 3) == 1) {
1162 					/* Do NOT handle Ctrl-Q or Ctrl-S
1163 					 * when in full-duplex audio mode.
1164 					 */
1165 					if (isdn_tty_end_vrx(buf, c)) {
1166 						info->vonline &= ~1;
1167 #ifdef ISDN_DEBUG_MODEM_VOICE
1168 						printk(KERN_DEBUG
1169 						       "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
1170 						       info->line);
1171 #endif
1172 						isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
1173 					}
1174 				}
1175 			} else
1176 				if (TTY_IS_FCLASS1(info)) {
1177 					int cc = isdn_tty_handleDLEdown(info, m, c);
1178 
1179 					if (info->vonline & 4) { /* ETX seen */
1180 						isdn_ctrl c;
1181 
1182 						c.command = ISDN_CMD_FAXCMD;
1183 						c.driver = info->isdn_driver;
1184 						c.arg = info->isdn_channel;
1185 						c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
1186 						c.parm.aux.subcmd = ETX;
1187 						isdn_command(&c);
1188 					}
1189 					info->vonline = 0;
1190 #ifdef ISDN_DEBUG_MODEM_VOICE
1191 					printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
1192 #endif
1193 					info->xmit_count += cc;
1194 				} else
1195 #endif
1196 					info->xmit_count += c;
1197 		} else {
1198 			info->msr |= UART_MSR_CTS;
1199 			info->lsr |= UART_LSR_TEMT;
1200 			if (info->dialing) {
1201 				info->dialing = 0;
1202 #ifdef ISDN_DEBUG_MODEM_HUP
1203 				printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
1204 #endif
1205 				isdn_tty_modem_result(RESULT_NO_CARRIER, info);
1206 				isdn_tty_modem_hup(info, 1);
1207 			} else
1208 				c = isdn_tty_edit_at(buf, c, info);
1209 		}
1210 		buf += c;
1211 		count -= c;
1212 		total += c;
1213 	}
1214 	atomic_dec(&info->xmit_lock);
1215 	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
1216 		if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
1217 			isdn_tty_senddown(info);
1218 			isdn_tty_tint(info);
1219 		}
1220 		isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1221 	}
1222 	return total;
1223 }
1224 
1225 static int
isdn_tty_write_room(struct tty_struct * tty)1226 isdn_tty_write_room(struct tty_struct *tty)
1227 {
1228 	modem_info *info = (modem_info *) tty->driver_data;
1229 	int ret;
1230 
1231 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write_room"))
1232 		return 0;
1233 	if (!info->online)
1234 		return info->xmit_size;
1235 	ret = info->xmit_size - info->xmit_count;
1236 	return (ret < 0) ? 0 : ret;
1237 }
1238 
1239 static int
isdn_tty_chars_in_buffer(struct tty_struct * tty)1240 isdn_tty_chars_in_buffer(struct tty_struct *tty)
1241 {
1242 	modem_info *info = (modem_info *) tty->driver_data;
1243 
1244 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_chars_in_buffer"))
1245 		return 0;
1246 	if (!info->online)
1247 		return 0;
1248 	return (info->xmit_count);
1249 }
1250 
1251 static void
isdn_tty_flush_buffer(struct tty_struct * tty)1252 isdn_tty_flush_buffer(struct tty_struct *tty)
1253 {
1254 	modem_info *info;
1255 
1256 	if (!tty) {
1257 		return;
1258 	}
1259 	info = (modem_info *) tty->driver_data;
1260 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
1261 		return;
1262 	}
1263 	isdn_tty_cleanup_xmit(info);
1264 	info->xmit_count = 0;
1265 	tty_wakeup(tty);
1266 }
1267 
1268 static void
isdn_tty_flush_chars(struct tty_struct * tty)1269 isdn_tty_flush_chars(struct tty_struct *tty)
1270 {
1271 	modem_info *info = (modem_info *) tty->driver_data;
1272 
1273 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
1274 		return;
1275 	if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
1276 		isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1277 }
1278 
1279 /*
1280  * ------------------------------------------------------------
1281  * isdn_tty_throttle()
1282  *
1283  * This routine is called by the upper-layer tty layer to signal that
1284  * incoming characters should be throttled.
1285  * ------------------------------------------------------------
1286  */
1287 static void
isdn_tty_throttle(struct tty_struct * tty)1288 isdn_tty_throttle(struct tty_struct *tty)
1289 {
1290 	modem_info *info = (modem_info *) tty->driver_data;
1291 
1292 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_throttle"))
1293 		return;
1294 	if (I_IXOFF(tty))
1295 		info->x_char = STOP_CHAR(tty);
1296 	info->mcr &= ~UART_MCR_RTS;
1297 }
1298 
1299 static void
isdn_tty_unthrottle(struct tty_struct * tty)1300 isdn_tty_unthrottle(struct tty_struct *tty)
1301 {
1302 	modem_info *info = (modem_info *) tty->driver_data;
1303 
1304 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_unthrottle"))
1305 		return;
1306 	if (I_IXOFF(tty)) {
1307 		if (info->x_char)
1308 			info->x_char = 0;
1309 		else
1310 			info->x_char = START_CHAR(tty);
1311 	}
1312 	info->mcr |= UART_MCR_RTS;
1313 }
1314 
1315 /*
1316  * ------------------------------------------------------------
1317  * isdn_tty_ioctl() and friends
1318  * ------------------------------------------------------------
1319  */
1320 
1321 /*
1322  * isdn_tty_get_lsr_info - get line status register info
1323  *
1324  * Purpose: Let user call ioctl() to get info when the UART physically
1325  *          is emptied.  On bus types like RS485, the transmitter must
1326  *          release the bus after transmitting. This must be done when
1327  *          the transmit shift register is empty, not be done when the
1328  *          transmit holding register is empty.  This functionality
1329  *          allows RS485 driver to be written in user space.
1330  */
1331 static int
isdn_tty_get_lsr_info(modem_info * info,uint __user * value)1332 isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
1333 {
1334 	u_char status;
1335 	uint result;
1336 
1337 	status = info->lsr;
1338 	result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1339 	return put_user(result, value);
1340 }
1341 
1342 
1343 static int
isdn_tty_tiocmget(struct tty_struct * tty)1344 isdn_tty_tiocmget(struct tty_struct *tty)
1345 {
1346 	modem_info *info = (modem_info *) tty->driver_data;
1347 	u_char control, status;
1348 
1349 	if (isdn_tty_paranoia_check(info, tty->name, __func__))
1350 		return -ENODEV;
1351 	if (tty_io_error(tty))
1352 		return -EIO;
1353 
1354 	mutex_lock(&modem_info_mutex);
1355 #ifdef ISDN_DEBUG_MODEM_IOCTL
1356 	printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
1357 #endif
1358 
1359 	control = info->mcr;
1360 	status = info->msr;
1361 	mutex_unlock(&modem_info_mutex);
1362 	return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1363 		| ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1364 		| ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1365 		| ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1366 		| ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1367 		| ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1368 }
1369 
1370 static int
isdn_tty_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)1371 isdn_tty_tiocmset(struct tty_struct *tty,
1372 		  unsigned int set, unsigned int clear)
1373 {
1374 	modem_info *info = (modem_info *) tty->driver_data;
1375 
1376 	if (isdn_tty_paranoia_check(info, tty->name, __func__))
1377 		return -ENODEV;
1378 	if (tty_io_error(tty))
1379 		return -EIO;
1380 
1381 #ifdef ISDN_DEBUG_MODEM_IOCTL
1382 	printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
1383 #endif
1384 
1385 	mutex_lock(&modem_info_mutex);
1386 	if (set & TIOCM_RTS)
1387 		info->mcr |= UART_MCR_RTS;
1388 	if (set & TIOCM_DTR) {
1389 		info->mcr |= UART_MCR_DTR;
1390 		isdn_tty_modem_ncarrier(info);
1391 	}
1392 
1393 	if (clear & TIOCM_RTS)
1394 		info->mcr &= ~UART_MCR_RTS;
1395 	if (clear & TIOCM_DTR) {
1396 		info->mcr &= ~UART_MCR_DTR;
1397 		if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1398 			isdn_tty_modem_reset_regs(info, 0);
1399 #ifdef ISDN_DEBUG_MODEM_HUP
1400 			printk(KERN_DEBUG "Mhup in TIOCMSET\n");
1401 #endif
1402 			if (info->online)
1403 				info->ncarrier = 1;
1404 			isdn_tty_modem_hup(info, 1);
1405 		}
1406 	}
1407 	mutex_unlock(&modem_info_mutex);
1408 	return 0;
1409 }
1410 
1411 static int
isdn_tty_ioctl(struct tty_struct * tty,uint cmd,ulong arg)1412 isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
1413 {
1414 	modem_info *info = (modem_info *) tty->driver_data;
1415 	int retval;
1416 
1417 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
1418 		return -ENODEV;
1419 	if (tty_io_error(tty))
1420 		return -EIO;
1421 	switch (cmd) {
1422 	case TCSBRK:   /* SVID version: non-zero arg --> no break */
1423 #ifdef ISDN_DEBUG_MODEM_IOCTL
1424 		printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
1425 #endif
1426 		retval = tty_check_change(tty);
1427 		if (retval)
1428 			return retval;
1429 		tty_wait_until_sent(tty, 0);
1430 		return 0;
1431 	case TCSBRKP:  /* support for POSIX tcsendbreak() */
1432 #ifdef ISDN_DEBUG_MODEM_IOCTL
1433 		printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
1434 #endif
1435 		retval = tty_check_change(tty);
1436 		if (retval)
1437 			return retval;
1438 		tty_wait_until_sent(tty, 0);
1439 		return 0;
1440 	case TIOCSERGETLSR:	/* Get line status register */
1441 #ifdef ISDN_DEBUG_MODEM_IOCTL
1442 		printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
1443 #endif
1444 		return isdn_tty_get_lsr_info(info, (uint __user *) arg);
1445 	default:
1446 #ifdef ISDN_DEBUG_MODEM_IOCTL
1447 		printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
1448 #endif
1449 		return -ENOIOCTLCMD;
1450 	}
1451 	return 0;
1452 }
1453 
1454 static void
isdn_tty_set_termios(struct tty_struct * tty,struct ktermios * old_termios)1455 isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1456 {
1457 	modem_info *info = (modem_info *) tty->driver_data;
1458 
1459 	mutex_lock(&modem_info_mutex);
1460 	if (!old_termios)
1461 		isdn_tty_change_speed(info);
1462 	else {
1463 		if (tty->termios.c_cflag == old_termios->c_cflag &&
1464 		    tty->termios.c_ispeed == old_termios->c_ispeed &&
1465 		    tty->termios.c_ospeed == old_termios->c_ospeed) {
1466 			mutex_unlock(&modem_info_mutex);
1467 			return;
1468 		}
1469 		isdn_tty_change_speed(info);
1470 	}
1471 	mutex_unlock(&modem_info_mutex);
1472 }
1473 
1474 /*
1475  * ------------------------------------------------------------
1476  * isdn_tty_open() and friends
1477  * ------------------------------------------------------------
1478  */
1479 
isdn_tty_install(struct tty_driver * driver,struct tty_struct * tty)1480 static int isdn_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1481 {
1482 	modem_info *info = &dev->mdm.info[tty->index];
1483 
1484 	if (isdn_tty_paranoia_check(info, tty->name, __func__))
1485 		return -ENODEV;
1486 
1487 	tty->driver_data = info;
1488 
1489 	return tty_port_install(&info->port, driver, tty);
1490 }
1491 
1492 /*
1493  * This routine is called whenever a serial port is opened.  It
1494  * enables interrupts for a serial port, linking in its async structure into
1495  * the IRQ chain.   It also performs the serial-specific
1496  * initialization for the tty structure.
1497  */
1498 static int
isdn_tty_open(struct tty_struct * tty,struct file * filp)1499 isdn_tty_open(struct tty_struct *tty, struct file *filp)
1500 {
1501 	modem_info *info = tty->driver_data;
1502 	struct tty_port *port = &info->port;
1503 	int retval;
1504 
1505 #ifdef ISDN_DEBUG_MODEM_OPEN
1506 	printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
1507 	       port->count);
1508 #endif
1509 	port->count++;
1510 	port->tty = tty;
1511 	/*
1512 	 * Start up serial port
1513 	 */
1514 	retval = isdn_tty_startup(info);
1515 	if (retval) {
1516 #ifdef ISDN_DEBUG_MODEM_OPEN
1517 		printk(KERN_DEBUG "isdn_tty_open return after startup\n");
1518 #endif
1519 		return retval;
1520 	}
1521 	retval = tty_port_block_til_ready(port, tty, filp);
1522 	if (retval) {
1523 #ifdef ISDN_DEBUG_MODEM_OPEN
1524 		printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
1525 #endif
1526 		return retval;
1527 	}
1528 #ifdef ISDN_DEBUG_MODEM_OPEN
1529 	printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
1530 #endif
1531 	dev->modempoll++;
1532 #ifdef ISDN_DEBUG_MODEM_OPEN
1533 	printk(KERN_DEBUG "isdn_tty_open normal exit\n");
1534 #endif
1535 	return 0;
1536 }
1537 
1538 static void
isdn_tty_close(struct tty_struct * tty,struct file * filp)1539 isdn_tty_close(struct tty_struct *tty, struct file *filp)
1540 {
1541 	modem_info *info = (modem_info *) tty->driver_data;
1542 	struct tty_port *port = &info->port;
1543 	ulong timeout;
1544 
1545 	if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
1546 		return;
1547 	if (tty_hung_up_p(filp)) {
1548 #ifdef ISDN_DEBUG_MODEM_OPEN
1549 		printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
1550 #endif
1551 		return;
1552 	}
1553 	if ((tty->count == 1) && (port->count != 1)) {
1554 		/*
1555 		 * Uh, oh.  tty->count is 1, which means that the tty
1556 		 * structure will be freed.  Info->count should always
1557 		 * be one in these conditions.  If it's greater than
1558 		 * one, we've got real problems, since it means the
1559 		 * serial port won't be shutdown.
1560 		 */
1561 		printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
1562 		       "info->count is %d\n", port->count);
1563 		port->count = 1;
1564 	}
1565 	if (--port->count < 0) {
1566 		printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
1567 		       info->line, port->count);
1568 		port->count = 0;
1569 	}
1570 	if (port->count) {
1571 #ifdef ISDN_DEBUG_MODEM_OPEN
1572 		printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
1573 #endif
1574 		return;
1575 	}
1576 	info->closing = 1;
1577 
1578 	tty->closing = 1;
1579 	/*
1580 	 * At this point we stop accepting input.  To do this, we
1581 	 * disable the receive line status interrupts, and tell the
1582 	 * interrupt driver to stop checking the data ready bit in the
1583 	 * line status register.
1584 	 */
1585 	if (tty_port_initialized(port)) {
1586 		tty_wait_until_sent(tty, 3000);	/* 30 seconds timeout */
1587 		/*
1588 		 * Before we drop DTR, make sure the UART transmitter
1589 		 * has completely drained; this is especially
1590 		 * important if there is a transmit FIFO!
1591 		 */
1592 		timeout = jiffies + HZ;
1593 		while (!(info->lsr & UART_LSR_TEMT)) {
1594 			schedule_timeout_interruptible(20);
1595 			if (time_after(jiffies, timeout))
1596 				break;
1597 		}
1598 	}
1599 	dev->modempoll--;
1600 	isdn_tty_shutdown(info);
1601 	isdn_tty_flush_buffer(tty);
1602 	tty_ldisc_flush(tty);
1603 	port->tty = NULL;
1604 	info->ncarrier = 0;
1605 
1606 	tty_port_close_end(port, tty);
1607 	info->closing = 0;
1608 #ifdef ISDN_DEBUG_MODEM_OPEN
1609 	printk(KERN_DEBUG "isdn_tty_close normal exit\n");
1610 #endif
1611 }
1612 
1613 /*
1614  * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1615  */
1616 static void
isdn_tty_hangup(struct tty_struct * tty)1617 isdn_tty_hangup(struct tty_struct *tty)
1618 {
1619 	modem_info *info = (modem_info *) tty->driver_data;
1620 	struct tty_port *port = &info->port;
1621 
1622 	if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
1623 		return;
1624 	isdn_tty_shutdown(info);
1625 	port->count = 0;
1626 	tty_port_set_active(port, 0);
1627 	port->tty = NULL;
1628 	wake_up_interruptible(&port->open_wait);
1629 }
1630 
1631 /* This routine initializes all emulator-data.
1632  */
1633 static void
isdn_tty_reset_profile(atemu * m)1634 isdn_tty_reset_profile(atemu *m)
1635 {
1636 	m->profile[0] = 0;
1637 	m->profile[1] = 0;
1638 	m->profile[2] = 43;
1639 	m->profile[3] = 13;
1640 	m->profile[4] = 10;
1641 	m->profile[5] = 8;
1642 	m->profile[6] = 3;
1643 	m->profile[7] = 60;
1644 	m->profile[8] = 2;
1645 	m->profile[9] = 6;
1646 	m->profile[10] = 7;
1647 	m->profile[11] = 70;
1648 	m->profile[12] = 0x45;
1649 	m->profile[13] = 4;
1650 	m->profile[14] = ISDN_PROTO_L2_X75I;
1651 	m->profile[15] = ISDN_PROTO_L3_TRANS;
1652 	m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
1653 	m->profile[17] = ISDN_MODEM_WINSIZE;
1654 	m->profile[18] = 4;
1655 	m->profile[19] = 0;
1656 	m->profile[20] = 0;
1657 	m->profile[23] = 0;
1658 	m->pmsn[0] = '\0';
1659 	m->plmsn[0] = '\0';
1660 }
1661 
1662 #ifdef CONFIG_ISDN_AUDIO
1663 static void
isdn_tty_modem_reset_vpar(atemu * m)1664 isdn_tty_modem_reset_vpar(atemu *m)
1665 {
1666 	m->vpar[0] = 2;         /* Voice-device            (2 = phone line) */
1667 	m->vpar[1] = 0;         /* Silence detection level (0 = none      ) */
1668 	m->vpar[2] = 70;        /* Silence interval        (7 sec.        ) */
1669 	m->vpar[3] = 2;         /* Compression type        (1 = ADPCM-2   ) */
1670 	m->vpar[4] = 0;         /* DTMF detection level    (0 = softcode  ) */
1671 	m->vpar[5] = 8;         /* DTMF interval           (8 * 5 ms.     ) */
1672 }
1673 #endif
1674 
1675 #ifdef CONFIG_ISDN_TTY_FAX
1676 static void
isdn_tty_modem_reset_faxpar(modem_info * info)1677 isdn_tty_modem_reset_faxpar(modem_info *info)
1678 {
1679 	T30_s *f = info->fax;
1680 
1681 	f->code = 0;
1682 	f->phase = ISDN_FAX_PHASE_IDLE;
1683 	f->direction = 0;
1684 	f->resolution = 1;	/* fine */
1685 	f->rate = 5;		/* 14400 bit/s */
1686 	f->width = 0;
1687 	f->length = 0;
1688 	f->compression = 0;
1689 	f->ecm = 0;
1690 	f->binary = 0;
1691 	f->scantime = 0;
1692 	memset(&f->id[0], 32, FAXIDLEN - 1);
1693 	f->id[FAXIDLEN - 1] = 0;
1694 	f->badlin = 0;
1695 	f->badmul = 0;
1696 	f->bor = 0;
1697 	f->nbc = 0;
1698 	f->cq = 0;
1699 	f->cr = 0;
1700 	f->ctcrty = 0;
1701 	f->minsp = 0;
1702 	f->phcto = 30;
1703 	f->rel = 0;
1704 	memset(&f->pollid[0], 32, FAXIDLEN - 1);
1705 	f->pollid[FAXIDLEN - 1] = 0;
1706 }
1707 #endif
1708 
1709 static void
isdn_tty_modem_reset_regs(modem_info * info,int force)1710 isdn_tty_modem_reset_regs(modem_info *info, int force)
1711 {
1712 	atemu *m = &info->emu;
1713 	if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
1714 		memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
1715 		memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
1716 		memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
1717 		info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
1718 	}
1719 #ifdef CONFIG_ISDN_AUDIO
1720 	isdn_tty_modem_reset_vpar(m);
1721 #endif
1722 #ifdef CONFIG_ISDN_TTY_FAX
1723 	isdn_tty_modem_reset_faxpar(info);
1724 #endif
1725 	m->mdmcmdl = 0;
1726 }
1727 
1728 static void
modem_write_profile(atemu * m)1729 modem_write_profile(atemu *m)
1730 {
1731 	memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
1732 	memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
1733 	memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
1734 	if (dev->profd)
1735 		send_sig(SIGIO, dev->profd, 1);
1736 }
1737 
1738 static const struct tty_operations modem_ops = {
1739 	.install = isdn_tty_install,
1740 	.open = isdn_tty_open,
1741 	.close = isdn_tty_close,
1742 	.write = isdn_tty_write,
1743 	.flush_chars = isdn_tty_flush_chars,
1744 	.write_room = isdn_tty_write_room,
1745 	.chars_in_buffer = isdn_tty_chars_in_buffer,
1746 	.flush_buffer = isdn_tty_flush_buffer,
1747 	.ioctl = isdn_tty_ioctl,
1748 	.throttle = isdn_tty_throttle,
1749 	.unthrottle = isdn_tty_unthrottle,
1750 	.set_termios = isdn_tty_set_termios,
1751 	.hangup = isdn_tty_hangup,
1752 	.tiocmget = isdn_tty_tiocmget,
1753 	.tiocmset = isdn_tty_tiocmset,
1754 };
1755 
isdn_tty_carrier_raised(struct tty_port * port)1756 static int isdn_tty_carrier_raised(struct tty_port *port)
1757 {
1758 	modem_info *info = container_of(port, modem_info, port);
1759 	return info->msr & UART_MSR_DCD;
1760 }
1761 
1762 static const struct tty_port_operations isdn_tty_port_ops = {
1763 	.carrier_raised = isdn_tty_carrier_raised,
1764 };
1765 
1766 int
isdn_tty_modem_init(void)1767 isdn_tty_modem_init(void)
1768 {
1769 	isdn_modem_t	*m;
1770 	int		i, retval;
1771 	modem_info	*info;
1772 
1773 	m = &dev->mdm;
1774 	m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
1775 	if (!m->tty_modem)
1776 		return -ENOMEM;
1777 	m->tty_modem->name = "ttyI";
1778 	m->tty_modem->major = ISDN_TTY_MAJOR;
1779 	m->tty_modem->minor_start = 0;
1780 	m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
1781 	m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
1782 	m->tty_modem->init_termios = tty_std_termios;
1783 	m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1784 	m->tty_modem->flags = TTY_DRIVER_REAL_RAW;
1785 	m->tty_modem->driver_name = "isdn_tty";
1786 	tty_set_operations(m->tty_modem, &modem_ops);
1787 	retval = tty_register_driver(m->tty_modem);
1788 	if (retval) {
1789 		printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
1790 		goto err;
1791 	}
1792 	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1793 		info = &m->info[i];
1794 #ifdef CONFIG_ISDN_TTY_FAX
1795 		if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
1796 			printk(KERN_ERR "Could not allocate fax t30-buffer\n");
1797 			retval = -ENOMEM;
1798 			goto err_unregister;
1799 		}
1800 #endif
1801 		tty_port_init(&info->port);
1802 		info->port.ops = &isdn_tty_port_ops;
1803 		spin_lock_init(&info->readlock);
1804 		sprintf(info->last_cause, "0000");
1805 		sprintf(info->last_num, "none");
1806 		info->last_dir = 0;
1807 		info->last_lhup = 1;
1808 		info->last_l2 = -1;
1809 		info->last_si = 0;
1810 		isdn_tty_reset_profile(&info->emu);
1811 		isdn_tty_modem_reset_regs(info, 1);
1812 		info->magic = ISDN_ASYNC_MAGIC;
1813 		info->line = i;
1814 		info->x_char = 0;
1815 		info->isdn_driver = -1;
1816 		info->isdn_channel = -1;
1817 		info->drv_index = -1;
1818 		info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
1819 		timer_setup(&info->nc_timer, isdn_tty_modem_do_ncarrier, 0);
1820 		skb_queue_head_init(&info->xmit_queue);
1821 #ifdef CONFIG_ISDN_AUDIO
1822 		skb_queue_head_init(&info->dtmf_queue);
1823 #endif
1824 		info->port.xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5,
1825 				GFP_KERNEL);
1826 		if (!info->port.xmit_buf) {
1827 			printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
1828 			retval = -ENOMEM;
1829 			goto err_unregister;
1830 		}
1831 		/* Make room for T.70 header */
1832 		info->port.xmit_buf += 4;
1833 	}
1834 	return 0;
1835 err_unregister:
1836 	for (i--; i >= 0; i--) {
1837 		info = &m->info[i];
1838 #ifdef CONFIG_ISDN_TTY_FAX
1839 		kfree(info->fax);
1840 #endif
1841 		kfree(info->port.xmit_buf - 4);
1842 		info->port.xmit_buf = NULL;
1843 		tty_port_destroy(&info->port);
1844 	}
1845 	tty_unregister_driver(m->tty_modem);
1846 err:
1847 	put_tty_driver(m->tty_modem);
1848 	m->tty_modem = NULL;
1849 	return retval;
1850 }
1851 
1852 void
isdn_tty_exit(void)1853 isdn_tty_exit(void)
1854 {
1855 	modem_info *info;
1856 	int i;
1857 
1858 	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1859 		info = &dev->mdm.info[i];
1860 		isdn_tty_cleanup_xmit(info);
1861 #ifdef CONFIG_ISDN_TTY_FAX
1862 		kfree(info->fax);
1863 #endif
1864 		kfree(info->port.xmit_buf - 4);
1865 		info->port.xmit_buf = NULL;
1866 		tty_port_destroy(&info->port);
1867 	}
1868 	tty_unregister_driver(dev->mdm.tty_modem);
1869 	put_tty_driver(dev->mdm.tty_modem);
1870 	dev->mdm.tty_modem = NULL;
1871 }
1872 
1873 
1874 /*
1875  * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
1876  *      match the MSN against the MSNs (glob patterns) defined for tty_emulator,
1877  *      and return 0 for match, 1 for no match, 2 if MSN could match if longer.
1878  */
1879 
1880 static int
isdn_tty_match_icall(char * cid,atemu * emu,int di)1881 isdn_tty_match_icall(char *cid, atemu *emu, int di)
1882 {
1883 #ifdef ISDN_DEBUG_MODEM_ICALL
1884 	printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
1885 	       emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
1886 	       emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
1887 #endif
1888 	if (strlen(emu->lmsn)) {
1889 		char *p = emu->lmsn;
1890 		char *q;
1891 		int  tmp;
1892 		int  ret = 0;
1893 
1894 		while (1) {
1895 			if ((q = strchr(p, ';')))
1896 				*q = '\0';
1897 			if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
1898 				ret = tmp;
1899 #ifdef ISDN_DEBUG_MODEM_ICALL
1900 			printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
1901 			       p, isdn_map_eaz2msn(emu->msn, di), tmp);
1902 #endif
1903 			if (q) {
1904 				*q = ';';
1905 				p = q;
1906 				p++;
1907 			}
1908 			if (!tmp)
1909 				return 0;
1910 			if (!q)
1911 				break;
1912 		}
1913 		return ret;
1914 	} else {
1915 		int tmp;
1916 		tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
1917 #ifdef ISDN_DEBUG_MODEM_ICALL
1918 		printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
1919 		       isdn_map_eaz2msn(emu->msn, di), tmp);
1920 #endif
1921 		return tmp;
1922 	}
1923 }
1924 
1925 /*
1926  * An incoming call-request has arrived.
1927  * Search the tty-devices for an appropriate device and bind
1928  * it to the ISDN-Channel.
1929  * Return:
1930  *
1931  *  0 = No matching device found.
1932  *  1 = A matching device found.
1933  *  3 = No match found, but eventually would match, if
1934  *      CID is longer.
1935  */
1936 int
isdn_tty_find_icall(int di,int ch,setup_parm * setup)1937 isdn_tty_find_icall(int di, int ch, setup_parm *setup)
1938 {
1939 	char *eaz;
1940 	int i;
1941 	int wret;
1942 	int idx;
1943 	int si1;
1944 	int si2;
1945 	char *nr;
1946 	ulong flags;
1947 
1948 	if (!setup->phone[0]) {
1949 		nr = "0";
1950 		printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
1951 	} else
1952 		nr = setup->phone;
1953 	si1 = (int) setup->si1;
1954 	si2 = (int) setup->si2;
1955 	if (!setup->eazmsn[0]) {
1956 		printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
1957 		eaz = "0";
1958 	} else
1959 		eaz = setup->eazmsn;
1960 #ifdef ISDN_DEBUG_MODEM_ICALL
1961 	printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
1962 #endif
1963 	wret = 0;
1964 	spin_lock_irqsave(&dev->lock, flags);
1965 	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1966 		modem_info *info = &dev->mdm.info[i];
1967 
1968 		if (info->port.count == 0)
1969 			continue;
1970 		if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) &&  /* SI1 is matching */
1971 		    (info->emu.mdmreg[REG_SI2] == si2))	{         /* SI2 is matching */
1972 			idx = isdn_dc2minor(di, ch);
1973 #ifdef ISDN_DEBUG_MODEM_ICALL
1974 			printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
1975 			printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
1976 			       info->port.flags, info->isdn_driver,
1977 			       info->isdn_channel, dev->usage[idx]);
1978 #endif
1979 			if (
1980 #ifndef FIX_FILE_TRANSFER
1981 			    tty_port_active(&info->port) &&
1982 #endif
1983 				(info->isdn_driver == -1) &&
1984 				(info->isdn_channel == -1) &&
1985 				(USG_NONE(dev->usage[idx]))) {
1986 				int matchret;
1987 
1988 				if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
1989 					wret = matchret;
1990 				if (!matchret) {                  /* EAZ is matching */
1991 					info->isdn_driver = di;
1992 					info->isdn_channel = ch;
1993 					info->drv_index = idx;
1994 					dev->m_idx[idx] = info->line;
1995 					dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
1996 					dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
1997 					strcpy(dev->num[idx], nr);
1998 					strcpy(info->emu.cpn, eaz);
1999 					info->emu.mdmreg[REG_SI1I] = si2bit[si1];
2000 					info->emu.mdmreg[REG_PLAN] = setup->plan;
2001 					info->emu.mdmreg[REG_SCREEN] = setup->screen;
2002 					isdn_info_update();
2003 					spin_unlock_irqrestore(&dev->lock, flags);
2004 					printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2005 					       info->line);
2006 					info->msr |= UART_MSR_RI;
2007 					isdn_tty_modem_result(RESULT_RING, info);
2008 					isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2009 					return 1;
2010 				}
2011 			}
2012 		}
2013 	}
2014 	spin_unlock_irqrestore(&dev->lock, flags);
2015 	printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
2016 	       ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
2017 	return (wret == 2) ? 3 : 0;
2018 }
2019 
2020 int
isdn_tty_stat_callback(int i,isdn_ctrl * c)2021 isdn_tty_stat_callback(int i, isdn_ctrl *c)
2022 {
2023 	int mi;
2024 	modem_info *info;
2025 	char *e;
2026 
2027 	if (i < 0)
2028 		return 0;
2029 	if ((mi = dev->m_idx[i]) >= 0) {
2030 		info = &dev->mdm.info[mi];
2031 		switch (c->command) {
2032 		case ISDN_STAT_CINF:
2033 			printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2034 			info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2035 			if (e == (char *)c->parm.num)
2036 				info->emu.charge = 0;
2037 
2038 			break;
2039 		case ISDN_STAT_BSENT:
2040 #ifdef ISDN_TTY_STAT_DEBUG
2041 			printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
2042 #endif
2043 			if ((info->isdn_driver == c->driver) &&
2044 			    (info->isdn_channel == c->arg)) {
2045 				info->msr |= UART_MSR_CTS;
2046 				if (info->send_outstanding)
2047 					if (!(--info->send_outstanding))
2048 						info->lsr |= UART_LSR_TEMT;
2049 				isdn_tty_tint(info);
2050 				return 1;
2051 			}
2052 			break;
2053 		case ISDN_STAT_CAUSE:
2054 #ifdef ISDN_TTY_STAT_DEBUG
2055 			printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2056 #endif
2057 			/* Signal cause to tty-device */
2058 			strncpy(info->last_cause, c->parm.num, 5);
2059 			return 1;
2060 		case ISDN_STAT_DISPLAY:
2061 #ifdef ISDN_TTY_STAT_DEBUG
2062 			printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2063 #endif
2064 			/* Signal display to tty-device */
2065 			if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2066 			    !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2067 				isdn_tty_at_cout("\r\n", info);
2068 				isdn_tty_at_cout("DISPLAY: ", info);
2069 				isdn_tty_at_cout(c->parm.display, info);
2070 				isdn_tty_at_cout("\r\n", info);
2071 			}
2072 			return 1;
2073 		case ISDN_STAT_DCONN:
2074 #ifdef ISDN_TTY_STAT_DEBUG
2075 			printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2076 #endif
2077 			if (tty_port_active(&info->port)) {
2078 				if (info->dialing == 1) {
2079 					info->dialing = 2;
2080 					return 1;
2081 				}
2082 			}
2083 			break;
2084 		case ISDN_STAT_DHUP:
2085 #ifdef ISDN_TTY_STAT_DEBUG
2086 			printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2087 #endif
2088 			if (tty_port_active(&info->port)) {
2089 				if (info->dialing == 1)
2090 					isdn_tty_modem_result(RESULT_BUSY, info);
2091 				if (info->dialing > 1)
2092 					isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2093 				info->dialing = 0;
2094 #ifdef ISDN_DEBUG_MODEM_HUP
2095 				printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2096 #endif
2097 				isdn_tty_modem_hup(info, 0);
2098 				return 1;
2099 			}
2100 			break;
2101 		case ISDN_STAT_BCONN:
2102 #ifdef ISDN_TTY_STAT_DEBUG
2103 			printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2104 #endif
2105 			/* Wake up any processes waiting
2106 			 * for incoming call of this device when
2107 			 * DCD follow the state of incoming carrier
2108 			 */
2109 			if (info->port.blocked_open &&
2110 			    (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
2111 				wake_up_interruptible(&info->port.open_wait);
2112 			}
2113 
2114 			/* Schedule CONNECT-Message to any tty
2115 			 * waiting for it and
2116 			 * set DCD-bit of its modem-status.
2117 			 */
2118 			if (tty_port_active(&info->port) ||
2119 			    (info->port.blocked_open &&
2120 			     (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
2121 				info->msr |= UART_MSR_DCD;
2122 				info->emu.charge = 0;
2123 				if (info->dialing & 0xf)
2124 					info->last_dir = 1;
2125 				else
2126 					info->last_dir = 0;
2127 				info->dialing = 0;
2128 				info->rcvsched = 1;
2129 				if (USG_MODEM(dev->usage[i])) {
2130 					if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2131 						strcpy(info->emu.connmsg, c->parm.num);
2132 						isdn_tty_modem_result(RESULT_CONNECT, info);
2133 					} else
2134 						isdn_tty_modem_result(RESULT_CONNECT64000, info);
2135 				}
2136 				if (USG_VOICE(dev->usage[i]))
2137 					isdn_tty_modem_result(RESULT_VCON, info);
2138 				return 1;
2139 			}
2140 			break;
2141 		case ISDN_STAT_BHUP:
2142 #ifdef ISDN_TTY_STAT_DEBUG
2143 			printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2144 #endif
2145 			if (tty_port_active(&info->port)) {
2146 #ifdef ISDN_DEBUG_MODEM_HUP
2147 				printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2148 #endif
2149 				isdn_tty_modem_hup(info, 0);
2150 				return 1;
2151 			}
2152 			break;
2153 		case ISDN_STAT_NODCH:
2154 #ifdef ISDN_TTY_STAT_DEBUG
2155 			printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2156 #endif
2157 			if (tty_port_active(&info->port)) {
2158 				if (info->dialing) {
2159 					info->dialing = 0;
2160 					info->last_l2 = -1;
2161 					info->last_si = 0;
2162 					sprintf(info->last_cause, "0000");
2163 					isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2164 				}
2165 				isdn_tty_modem_hup(info, 0);
2166 				return 1;
2167 			}
2168 			break;
2169 		case ISDN_STAT_UNLOAD:
2170 #ifdef ISDN_TTY_STAT_DEBUG
2171 			printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2172 #endif
2173 			for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2174 				info = &dev->mdm.info[i];
2175 				if (info->isdn_driver == c->driver) {
2176 					if (info->online)
2177 						isdn_tty_modem_hup(info, 1);
2178 				}
2179 			}
2180 			return 1;
2181 #ifdef CONFIG_ISDN_TTY_FAX
2182 		case ISDN_STAT_FAXIND:
2183 			if (tty_port_active(&info->port)) {
2184 				isdn_tty_fax_command(info, c);
2185 			}
2186 			break;
2187 #endif
2188 #ifdef CONFIG_ISDN_AUDIO
2189 		case ISDN_STAT_AUDIO:
2190 			if (tty_port_active(&info->port)) {
2191 				switch (c->parm.num[0]) {
2192 				case ISDN_AUDIO_DTMF:
2193 					if (info->vonline) {
2194 						isdn_audio_put_dle_code(info,
2195 									c->parm.num[1]);
2196 					}
2197 					break;
2198 				}
2199 			}
2200 			break;
2201 #endif
2202 		}
2203 	}
2204 	return 0;
2205 }
2206 
2207 /*********************************************************************
2208  Modem-Emulator-Routines
2209 *********************************************************************/
2210 
2211 #define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
2212 
2213 /*
2214  * Put a message from the AT-emulator into receive-buffer of tty,
2215  * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2216  */
2217 void
isdn_tty_at_cout(char * msg,modem_info * info)2218 isdn_tty_at_cout(char *msg, modem_info *info)
2219 {
2220 	struct tty_port *port = &info->port;
2221 	atemu *m = &info->emu;
2222 	char *p;
2223 	char c;
2224 	u_long flags;
2225 	struct sk_buff *skb = NULL;
2226 	char *sp = NULL;
2227 	int l;
2228 
2229 	if (!msg) {
2230 		printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2231 		return;
2232 	}
2233 
2234 	l = strlen(msg);
2235 
2236 	spin_lock_irqsave(&info->readlock, flags);
2237 	if (info->closing) {
2238 		spin_unlock_irqrestore(&info->readlock, flags);
2239 		return;
2240 	}
2241 
2242 	/* use queue instead of direct, if online and */
2243 	/* data is in queue or buffer is full */
2244 	if (info->online && ((tty_buffer_request_room(port, l) < l) ||
2245 			     !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
2246 		skb = alloc_skb(l, GFP_ATOMIC);
2247 		if (!skb) {
2248 			spin_unlock_irqrestore(&info->readlock, flags);
2249 			return;
2250 		}
2251 		sp = skb_put(skb, l);
2252 #ifdef CONFIG_ISDN_AUDIO
2253 		ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2254 		ISDN_AUDIO_SKB_LOCK(skb) = 0;
2255 #endif
2256 	}
2257 
2258 	for (p = msg; *p; p++) {
2259 		switch (*p) {
2260 		case '\r':
2261 			c = m->mdmreg[REG_CR];
2262 			break;
2263 		case '\n':
2264 			c = m->mdmreg[REG_LF];
2265 			break;
2266 		case '\b':
2267 			c = m->mdmreg[REG_BS];
2268 			break;
2269 		default:
2270 			c = *p;
2271 		}
2272 		if (skb) {
2273 			*sp++ = c;
2274 		} else {
2275 			if (tty_insert_flip_char(port, c, TTY_NORMAL) == 0)
2276 				break;
2277 		}
2278 	}
2279 	if (skb) {
2280 		__skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2281 		dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2282 		spin_unlock_irqrestore(&info->readlock, flags);
2283 		/* Schedule dequeuing */
2284 		if (dev->modempoll && info->rcvsched)
2285 			isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2286 
2287 	} else {
2288 		spin_unlock_irqrestore(&info->readlock, flags);
2289 		tty_flip_buffer_push(port);
2290 	}
2291 }
2292 
2293 /*
2294  * Perform ATH Hangup
2295  */
2296 static void
isdn_tty_on_hook(modem_info * info)2297 isdn_tty_on_hook(modem_info *info)
2298 {
2299 	if (info->isdn_channel >= 0) {
2300 #ifdef ISDN_DEBUG_MODEM_HUP
2301 		printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2302 #endif
2303 		isdn_tty_modem_hup(info, 1);
2304 	}
2305 }
2306 
2307 static void
isdn_tty_off_hook(void)2308 isdn_tty_off_hook(void)
2309 {
2310 	printk(KERN_DEBUG "isdn_tty_off_hook\n");
2311 }
2312 
2313 #define PLUSWAIT1 (HZ / 2)      /* 0.5 sec. */
2314 #define PLUSWAIT2 (HZ * 3 / 2)  /* 1.5 sec */
2315 
2316 /*
2317  * Check Buffer for Modem-escape-sequence, activate timer-callback to
2318  * isdn_tty_modem_escape() if sequence found.
2319  *
2320  * Parameters:
2321  *   p          pointer to databuffer
2322  *   plus       escape-character
2323  *   count      length of buffer
2324  *   pluscount  count of valid escape-characters so far
2325  *   lastplus   timestamp of last character
2326  */
2327 static void
isdn_tty_check_esc(const u_char * p,u_char plus,int count,int * pluscount,u_long * lastplus)2328 isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
2329 		   u_long *lastplus)
2330 {
2331 	if (plus > 127)
2332 		return;
2333 	if (count > 3) {
2334 		p += count - 3;
2335 		count = 3;
2336 		*pluscount = 0;
2337 	}
2338 	while (count > 0) {
2339 		if (*(p++) == plus) {
2340 			if ((*pluscount)++) {
2341 				/* Time since last '+' > 0.5 sec. ? */
2342 				if (time_after(jiffies, *lastplus + PLUSWAIT1))
2343 					*pluscount = 1;
2344 			} else {
2345 				/* Time since last non-'+' < 1.5 sec. ? */
2346 				if (time_before(jiffies, *lastplus + PLUSWAIT2))
2347 					*pluscount = 0;
2348 			}
2349 			if ((*pluscount == 3) && (count == 1))
2350 				isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2351 			if (*pluscount > 3)
2352 				*pluscount = 1;
2353 		} else
2354 			*pluscount = 0;
2355 		*lastplus = jiffies;
2356 		count--;
2357 	}
2358 }
2359 
2360 /*
2361  * Return result of AT-emulator to tty-receive-buffer, depending on
2362  * modem-register 12, bit 0 and 1.
2363  * For CONNECT-messages also switch to online-mode.
2364  * For RING-message handle auto-ATA if register 0 != 0
2365  */
2366 
2367 static void
isdn_tty_modem_result(int code,modem_info * info)2368 isdn_tty_modem_result(int code, modem_info *info)
2369 {
2370 	atemu *m = &info->emu;
2371 	static char *msg[] =
2372 		{"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2373 		 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2374 		 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2375 	char s[ISDN_MSNLEN + 10];
2376 
2377 	switch (code) {
2378 	case RESULT_RING:
2379 		m->mdmreg[REG_RINGCNT]++;
2380 		if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2381 			/* Automatically accept incoming call */
2382 			isdn_tty_cmd_ATA(info);
2383 		break;
2384 	case RESULT_NO_CARRIER:
2385 #ifdef ISDN_DEBUG_MODEM_HUP
2386 		printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
2387 		       info->closing, !info->port.tty);
2388 #endif
2389 		m->mdmreg[REG_RINGCNT] = 0;
2390 		del_timer(&info->nc_timer);
2391 		info->ncarrier = 0;
2392 		if (info->closing || !info->port.tty)
2393 			return;
2394 
2395 #ifdef CONFIG_ISDN_AUDIO
2396 		if (info->vonline & 1) {
2397 #ifdef ISDN_DEBUG_MODEM_VOICE
2398 			printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
2399 			       info->line);
2400 #endif
2401 			/* voice-recording, add DLE-ETX */
2402 			isdn_tty_at_cout("\020\003", info);
2403 		}
2404 		if (info->vonline & 2) {
2405 #ifdef ISDN_DEBUG_MODEM_VOICE
2406 			printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
2407 			       info->line);
2408 #endif
2409 			/* voice-playing, add DLE-DC4 */
2410 			isdn_tty_at_cout("\020\024", info);
2411 		}
2412 #endif
2413 		break;
2414 	case RESULT_CONNECT:
2415 	case RESULT_CONNECT64000:
2416 		sprintf(info->last_cause, "0000");
2417 		if (!info->online)
2418 			info->online = 2;
2419 		break;
2420 	case RESULT_VCON:
2421 #ifdef ISDN_DEBUG_MODEM_VOICE
2422 		printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
2423 		       info->line);
2424 #endif
2425 		sprintf(info->last_cause, "0000");
2426 		if (!info->online)
2427 			info->online = 1;
2428 		break;
2429 	} /* switch (code) */
2430 
2431 	if (m->mdmreg[REG_RESP] & BIT_RESP) {
2432 		/* Show results */
2433 		if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
2434 			/* Show numeric results only */
2435 			sprintf(s, "\r\n%d\r\n", code);
2436 			isdn_tty_at_cout(s, info);
2437 		} else {
2438 			if (code == RESULT_RING) {
2439 				/* return if "show RUNG" and ringcounter>1 */
2440 				if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
2441 				    (m->mdmreg[REG_RINGCNT] > 1))
2442 					return;
2443 				/* print CID, _before_ _every_ ring */
2444 				if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
2445 					isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
2446 					isdn_tty_at_cout(dev->num[info->drv_index], info);
2447 					if (m->mdmreg[REG_CDN] & BIT_CDN) {
2448 						isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2449 						isdn_tty_at_cout(info->emu.cpn, info);
2450 					}
2451 				}
2452 			}
2453 			isdn_tty_at_cout("\r\n", info);
2454 			isdn_tty_at_cout(msg[code], info);
2455 			switch (code) {
2456 			case RESULT_CONNECT:
2457 				switch (m->mdmreg[REG_L2PROT]) {
2458 				case ISDN_PROTO_L2_MODEM:
2459 					isdn_tty_at_cout(" ", info);
2460 					isdn_tty_at_cout(m->connmsg, info);
2461 					break;
2462 				}
2463 				break;
2464 			case RESULT_RING:
2465 				/* Append CPN, if enabled */
2466 				if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
2467 					sprintf(s, "/%s", m->cpn);
2468 					isdn_tty_at_cout(s, info);
2469 				}
2470 				/* Print CID only once, _after_ 1st RING */
2471 				if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
2472 				    (m->mdmreg[REG_RINGCNT] == 1)) {
2473 					isdn_tty_at_cout("\r\n", info);
2474 					isdn_tty_at_cout("CALLER NUMBER: ", info);
2475 					isdn_tty_at_cout(dev->num[info->drv_index], info);
2476 					if (m->mdmreg[REG_CDN] & BIT_CDN) {
2477 						isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2478 						isdn_tty_at_cout(info->emu.cpn, info);
2479 					}
2480 				}
2481 				break;
2482 			case RESULT_NO_CARRIER:
2483 			case RESULT_NO_DIALTONE:
2484 			case RESULT_BUSY:
2485 			case RESULT_NO_ANSWER:
2486 				m->mdmreg[REG_RINGCNT] = 0;
2487 				/* Append Cause-Message if enabled */
2488 				if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
2489 					sprintf(s, "/%s", info->last_cause);
2490 					isdn_tty_at_cout(s, info);
2491 				}
2492 				break;
2493 			case RESULT_CONNECT64000:
2494 				/* Append Protocol to CONNECT message */
2495 				switch (m->mdmreg[REG_L2PROT]) {
2496 				case ISDN_PROTO_L2_X75I:
2497 				case ISDN_PROTO_L2_X75UI:
2498 				case ISDN_PROTO_L2_X75BUI:
2499 					isdn_tty_at_cout("/X.75", info);
2500 					break;
2501 				case ISDN_PROTO_L2_HDLC:
2502 					isdn_tty_at_cout("/HDLC", info);
2503 					break;
2504 				case ISDN_PROTO_L2_V11096:
2505 					isdn_tty_at_cout("/V110/9600", info);
2506 					break;
2507 				case ISDN_PROTO_L2_V11019:
2508 					isdn_tty_at_cout("/V110/19200", info);
2509 					break;
2510 				case ISDN_PROTO_L2_V11038:
2511 					isdn_tty_at_cout("/V110/38400", info);
2512 					break;
2513 				}
2514 				if (m->mdmreg[REG_T70] & BIT_T70) {
2515 					isdn_tty_at_cout("/T.70", info);
2516 					if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2517 						isdn_tty_at_cout("+", info);
2518 				}
2519 				break;
2520 			}
2521 			isdn_tty_at_cout("\r\n", info);
2522 		}
2523 	}
2524 	if (code == RESULT_NO_CARRIER) {
2525 		if (info->closing || (!info->port.tty))
2526 			return;
2527 
2528 		if (tty_port_check_carrier(&info->port))
2529 			tty_hangup(info->port.tty);
2530 	}
2531 }
2532 
2533 
2534 /*
2535  * Display a modem-register-value.
2536  */
2537 static void
isdn_tty_show_profile(int ridx,modem_info * info)2538 isdn_tty_show_profile(int ridx, modem_info *info)
2539 {
2540 	char v[6];
2541 
2542 	sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
2543 	isdn_tty_at_cout(v, info);
2544 }
2545 
2546 /*
2547  * Get MSN-string from char-pointer, set pointer to end of number
2548  */
2549 static void
isdn_tty_get_msnstr(char * n,char ** p)2550 isdn_tty_get_msnstr(char *n, char **p)
2551 {
2552 	int limit = ISDN_MSNLEN - 1;
2553 
2554 	while (((*p[0] >= '0' && *p[0] <= '9') ||
2555 		/* Why a comma ??? */
2556 		(*p[0] == ',') || (*p[0] == ':')) &&
2557 	       (limit--))
2558 		*n++ = *p[0]++;
2559 	*n = '\0';
2560 }
2561 
2562 /*
2563  * Get phone-number from modem-commandbuffer
2564  */
2565 static void
isdn_tty_getdial(char * p,char * q,int cnt)2566 isdn_tty_getdial(char *p, char *q, int cnt)
2567 {
2568 	int first = 1;
2569 	int limit = ISDN_MSNLEN - 1;	/* MUST match the size of interface var to avoid
2570 					   buffer overflow */
2571 
2572 	while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
2573 		if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
2574 		    ((*p == 'R') && first) ||
2575 		    (*p == '*') || (*p == '#')) {
2576 			*q++ = *p;
2577 			limit--;
2578 		}
2579 		if (!limit)
2580 			break;
2581 		p++;
2582 		first = 0;
2583 	}
2584 	*q = 0;
2585 }
2586 
2587 #define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
2588 #define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
2589 
2590 static void
isdn_tty_report(modem_info * info)2591 isdn_tty_report(modem_info *info)
2592 {
2593 	atemu *m = &info->emu;
2594 	char s[80];
2595 
2596 	isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
2597 	sprintf(s, "    Remote Number:    %s\r\n", info->last_num);
2598 	isdn_tty_at_cout(s, info);
2599 	sprintf(s, "    Direction:        %s\r\n", info->last_dir ? "outgoing" : "incoming");
2600 	isdn_tty_at_cout(s, info);
2601 	isdn_tty_at_cout("    Layer-2 Protocol: ", info);
2602 	switch (info->last_l2) {
2603 	case ISDN_PROTO_L2_X75I:
2604 		isdn_tty_at_cout("X.75i", info);
2605 		break;
2606 	case ISDN_PROTO_L2_X75UI:
2607 		isdn_tty_at_cout("X.75ui", info);
2608 		break;
2609 	case ISDN_PROTO_L2_X75BUI:
2610 		isdn_tty_at_cout("X.75bui", info);
2611 		break;
2612 	case ISDN_PROTO_L2_HDLC:
2613 		isdn_tty_at_cout("HDLC", info);
2614 		break;
2615 	case ISDN_PROTO_L2_V11096:
2616 		isdn_tty_at_cout("V.110 9600 Baud", info);
2617 		break;
2618 	case ISDN_PROTO_L2_V11019:
2619 		isdn_tty_at_cout("V.110 19200 Baud", info);
2620 		break;
2621 	case ISDN_PROTO_L2_V11038:
2622 		isdn_tty_at_cout("V.110 38400 Baud", info);
2623 		break;
2624 	case ISDN_PROTO_L2_TRANS:
2625 		isdn_tty_at_cout("transparent", info);
2626 		break;
2627 	case ISDN_PROTO_L2_MODEM:
2628 		isdn_tty_at_cout("modem", info);
2629 		break;
2630 	case ISDN_PROTO_L2_FAX:
2631 		isdn_tty_at_cout("fax", info);
2632 		break;
2633 	default:
2634 		isdn_tty_at_cout("unknown", info);
2635 		break;
2636 	}
2637 	if (m->mdmreg[REG_T70] & BIT_T70) {
2638 		isdn_tty_at_cout("/T.70", info);
2639 		if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2640 			isdn_tty_at_cout("+", info);
2641 	}
2642 	isdn_tty_at_cout("\r\n", info);
2643 	isdn_tty_at_cout("    Service:          ", info);
2644 	switch (info->last_si) {
2645 	case 1:
2646 		isdn_tty_at_cout("audio\r\n", info);
2647 		break;
2648 	case 5:
2649 		isdn_tty_at_cout("btx\r\n", info);
2650 		break;
2651 	case 7:
2652 		isdn_tty_at_cout("data\r\n", info);
2653 		break;
2654 	default:
2655 		sprintf(s, "%d\r\n", info->last_si);
2656 		isdn_tty_at_cout(s, info);
2657 		break;
2658 	}
2659 	sprintf(s, "    Hangup location:  %s\r\n", info->last_lhup ? "local" : "remote");
2660 	isdn_tty_at_cout(s, info);
2661 	sprintf(s, "    Last cause:       %s\r\n", info->last_cause);
2662 	isdn_tty_at_cout(s, info);
2663 }
2664 
2665 /*
2666  * Parse AT&.. commands.
2667  */
2668 static int
isdn_tty_cmd_ATand(char ** p,modem_info * info)2669 isdn_tty_cmd_ATand(char **p, modem_info *info)
2670 {
2671 	atemu *m = &info->emu;
2672 	int i;
2673 	char rb[100];
2674 
2675 #define MAXRB (sizeof(rb) - 1)
2676 
2677 	switch (*p[0]) {
2678 	case 'B':
2679 		/* &B - Set Buffersize */
2680 		p[0]++;
2681 		i = isdn_getnum(p);
2682 		if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
2683 			PARSE_ERROR1;
2684 #ifdef CONFIG_ISDN_AUDIO
2685 		if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
2686 			PARSE_ERROR1;
2687 #endif
2688 		m->mdmreg[REG_PSIZE] = i / 16;
2689 		info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2690 		switch (m->mdmreg[REG_L2PROT]) {
2691 		case ISDN_PROTO_L2_V11096:
2692 		case ISDN_PROTO_L2_V11019:
2693 		case ISDN_PROTO_L2_V11038:
2694 			info->xmit_size /= 10;
2695 		}
2696 		break;
2697 	case 'C':
2698 		/* &C - DCD Status */
2699 		p[0]++;
2700 		switch (isdn_getnum(p)) {
2701 		case 0:
2702 			m->mdmreg[REG_DCD] &= ~BIT_DCD;
2703 			break;
2704 		case 1:
2705 			m->mdmreg[REG_DCD] |= BIT_DCD;
2706 			break;
2707 		default:
2708 			PARSE_ERROR1
2709 				}
2710 		break;
2711 	case 'D':
2712 		/* &D - Set DTR-Low-behavior */
2713 		p[0]++;
2714 		switch (isdn_getnum(p)) {
2715 		case 0:
2716 			m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
2717 			m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2718 			break;
2719 		case 2:
2720 			m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2721 			m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
2722 			break;
2723 		case 3:
2724 			m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2725 			m->mdmreg[REG_DTRR] |= BIT_DTRR;
2726 			break;
2727 		default:
2728 			PARSE_ERROR1
2729 				}
2730 		break;
2731 	case 'E':
2732 		/* &E -Set EAZ/MSN */
2733 		p[0]++;
2734 		isdn_tty_get_msnstr(m->msn, p);
2735 		break;
2736 	case 'F':
2737 		/* &F -Set Factory-Defaults */
2738 		p[0]++;
2739 		if (info->msr & UART_MSR_DCD)
2740 			PARSE_ERROR1;
2741 		isdn_tty_reset_profile(m);
2742 		isdn_tty_modem_reset_regs(info, 1);
2743 		break;
2744 #ifdef DUMMY_HAYES_AT
2745 	case 'K':
2746 		/* only for be compilant with common scripts */
2747 		/* &K Flowcontrol - no function */
2748 		p[0]++;
2749 		isdn_getnum(p);
2750 		break;
2751 #endif
2752 	case 'L':
2753 		/* &L -Set Numbers to listen on */
2754 		p[0]++;
2755 		i = 0;
2756 		while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
2757 		       (i < ISDN_LMSNLEN - 1))
2758 			m->lmsn[i++] = *p[0]++;
2759 		m->lmsn[i] = '\0';
2760 		break;
2761 	case 'R':
2762 		/* &R - Set V.110 bitrate adaption */
2763 		p[0]++;
2764 		i = isdn_getnum(p);
2765 		switch (i) {
2766 		case 0:
2767 			/* Switch off V.110, back to X.75 */
2768 			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2769 			m->mdmreg[REG_SI2] = 0;
2770 			info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2771 			break;
2772 		case 9600:
2773 			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
2774 			m->mdmreg[REG_SI2] = 197;
2775 			info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2776 			break;
2777 		case 19200:
2778 			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
2779 			m->mdmreg[REG_SI2] = 199;
2780 			info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2781 			break;
2782 		case 38400:
2783 			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
2784 			m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
2785 			info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
2786 			break;
2787 		default:
2788 			PARSE_ERROR1;
2789 		}
2790 		/* Switch off T.70 */
2791 		m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2792 		/* Set Service 7 */
2793 		m->mdmreg[REG_SI1] |= 4;
2794 		break;
2795 	case 'S':
2796 		/* &S - Set Windowsize */
2797 		p[0]++;
2798 		i = isdn_getnum(p);
2799 		if ((i > 0) && (i < 9))
2800 			m->mdmreg[REG_WSIZE] = i;
2801 		else
2802 			PARSE_ERROR1;
2803 		break;
2804 	case 'V':
2805 		/* &V - Show registers */
2806 		p[0]++;
2807 		isdn_tty_at_cout("\r\n", info);
2808 		for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
2809 			sprintf(rb, "S%02d=%03d%s", i,
2810 				m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
2811 			isdn_tty_at_cout(rb, info);
2812 		}
2813 		sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
2814 			strlen(m->msn) ? m->msn : "None");
2815 		isdn_tty_at_cout(rb, info);
2816 		if (strlen(m->lmsn)) {
2817 			isdn_tty_at_cout("\r\nListen: ", info);
2818 			isdn_tty_at_cout(m->lmsn, info);
2819 			isdn_tty_at_cout("\r\n", info);
2820 		}
2821 		break;
2822 	case 'W':
2823 		/* &W - Write Profile */
2824 		p[0]++;
2825 		switch (*p[0]) {
2826 		case '0':
2827 			p[0]++;
2828 			modem_write_profile(m);
2829 			break;
2830 		default:
2831 			PARSE_ERROR1;
2832 		}
2833 		break;
2834 	case 'X':
2835 		/* &X - Switch to BTX-Mode and T.70 */
2836 		p[0]++;
2837 		switch (isdn_getnum(p)) {
2838 		case 0:
2839 			m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2840 			info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2841 			break;
2842 		case 1:
2843 			m->mdmreg[REG_T70] |= BIT_T70;
2844 			m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
2845 			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2846 			info->xmit_size = 112;
2847 			m->mdmreg[REG_SI1] = 4;
2848 			m->mdmreg[REG_SI2] = 0;
2849 			break;
2850 		case 2:
2851 			m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
2852 			m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2853 			info->xmit_size = 112;
2854 			m->mdmreg[REG_SI1] = 4;
2855 			m->mdmreg[REG_SI2] = 0;
2856 			break;
2857 		default:
2858 			PARSE_ERROR1;
2859 		}
2860 		break;
2861 	default:
2862 		PARSE_ERROR1;
2863 	}
2864 	return 0;
2865 }
2866 
2867 static int
isdn_tty_check_ats(int mreg,int mval,modem_info * info,atemu * m)2868 isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
2869 {
2870 	/* Some plausibility checks */
2871 	switch (mreg) {
2872 	case REG_L2PROT:
2873 		if (mval > ISDN_PROTO_L2_MAX)
2874 			return 1;
2875 		break;
2876 	case REG_PSIZE:
2877 		if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
2878 			return 1;
2879 #ifdef CONFIG_ISDN_AUDIO
2880 		if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
2881 			return 1;
2882 #endif
2883 		info->xmit_size = mval * 16;
2884 		switch (m->mdmreg[REG_L2PROT]) {
2885 		case ISDN_PROTO_L2_V11096:
2886 		case ISDN_PROTO_L2_V11019:
2887 		case ISDN_PROTO_L2_V11038:
2888 			info->xmit_size /= 10;
2889 		}
2890 		break;
2891 	case REG_SI1I:
2892 	case REG_PLAN:
2893 	case REG_SCREEN:
2894 		/* readonly registers */
2895 		return 1;
2896 	}
2897 	return 0;
2898 }
2899 
2900 /*
2901  * Perform ATS command
2902  */
2903 static int
isdn_tty_cmd_ATS(char ** p,modem_info * info)2904 isdn_tty_cmd_ATS(char **p, modem_info *info)
2905 {
2906 	atemu *m = &info->emu;
2907 	int bitpos;
2908 	int mreg;
2909 	int mval;
2910 	int bval;
2911 
2912 	mreg = isdn_getnum(p);
2913 	if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
2914 		PARSE_ERROR1;
2915 	switch (*p[0]) {
2916 	case '=':
2917 		p[0]++;
2918 		mval = isdn_getnum(p);
2919 		if (mval < 0 || mval > 255)
2920 			PARSE_ERROR1;
2921 		if (isdn_tty_check_ats(mreg, mval, info, m))
2922 			PARSE_ERROR1;
2923 		m->mdmreg[mreg] = mval;
2924 		break;
2925 	case '.':
2926 		/* Set/Clear a single bit */
2927 		p[0]++;
2928 		bitpos = isdn_getnum(p);
2929 		if ((bitpos < 0) || (bitpos > 7))
2930 			PARSE_ERROR1;
2931 		switch (*p[0]) {
2932 		case '=':
2933 			p[0]++;
2934 			bval = isdn_getnum(p);
2935 			if (bval < 0 || bval > 1)
2936 				PARSE_ERROR1;
2937 			if (bval)
2938 				mval = m->mdmreg[mreg] | (1 << bitpos);
2939 			else
2940 				mval = m->mdmreg[mreg] & ~(1 << bitpos);
2941 			if (isdn_tty_check_ats(mreg, mval, info, m))
2942 				PARSE_ERROR1;
2943 			m->mdmreg[mreg] = mval;
2944 			break;
2945 		case '?':
2946 			p[0]++;
2947 			isdn_tty_at_cout("\r\n", info);
2948 			isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
2949 					 info);
2950 			break;
2951 		default:
2952 			PARSE_ERROR1;
2953 		}
2954 		break;
2955 	case '?':
2956 		p[0]++;
2957 		isdn_tty_show_profile(mreg, info);
2958 		break;
2959 	default:
2960 		PARSE_ERROR1;
2961 		break;
2962 	}
2963 	return 0;
2964 }
2965 
2966 /*
2967  * Perform ATA command
2968  */
2969 static void
isdn_tty_cmd_ATA(modem_info * info)2970 isdn_tty_cmd_ATA(modem_info *info)
2971 {
2972 	atemu *m = &info->emu;
2973 	isdn_ctrl cmd;
2974 	int l2;
2975 
2976 	if (info->msr & UART_MSR_RI) {
2977 		/* Accept incoming call */
2978 		info->last_dir = 0;
2979 		strcpy(info->last_num, dev->num[info->drv_index]);
2980 		m->mdmreg[REG_RINGCNT] = 0;
2981 		info->msr &= ~UART_MSR_RI;
2982 		l2 = m->mdmreg[REG_L2PROT];
2983 #ifdef CONFIG_ISDN_AUDIO
2984 		/* If more than one bit set in reg18, autoselect Layer2 */
2985 		if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
2986 			if (m->mdmreg[REG_SI1I] == 1) {
2987 				if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
2988 					l2 = ISDN_PROTO_L2_TRANS;
2989 			} else
2990 				l2 = ISDN_PROTO_L2_X75I;
2991 		}
2992 #endif
2993 		cmd.driver = info->isdn_driver;
2994 		cmd.command = ISDN_CMD_SETL2;
2995 		cmd.arg = info->isdn_channel + (l2 << 8);
2996 		info->last_l2 = l2;
2997 		isdn_command(&cmd);
2998 		cmd.driver = info->isdn_driver;
2999 		cmd.command = ISDN_CMD_SETL3;
3000 		cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
3001 #ifdef CONFIG_ISDN_TTY_FAX
3002 		if (l2 == ISDN_PROTO_L2_FAX) {
3003 			cmd.parm.fax = info->fax;
3004 			info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3005 		}
3006 #endif
3007 		isdn_command(&cmd);
3008 		cmd.driver = info->isdn_driver;
3009 		cmd.arg = info->isdn_channel;
3010 		cmd.command = ISDN_CMD_ACCEPTD;
3011 		info->dialing = 16;
3012 		info->emu.carrierwait = 0;
3013 		isdn_command(&cmd);
3014 		isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3015 	} else
3016 		isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3017 }
3018 
3019 #ifdef CONFIG_ISDN_AUDIO
3020 /*
3021  * Parse AT+F.. commands
3022  */
3023 static int
isdn_tty_cmd_PLUSF(char ** p,modem_info * info)3024 isdn_tty_cmd_PLUSF(char **p, modem_info *info)
3025 {
3026 	atemu *m = &info->emu;
3027 	char rs[20];
3028 
3029 	if (!strncmp(p[0], "CLASS", 5)) {
3030 		p[0] += 5;
3031 		switch (*p[0]) {
3032 		case '?':
3033 			p[0]++;
3034 			sprintf(rs, "\r\n%d",
3035 				(m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3036 #ifdef CONFIG_ISDN_TTY_FAX
3037 			if (TTY_IS_FCLASS2(info))
3038 				sprintf(rs, "\r\n2");
3039 			else if (TTY_IS_FCLASS1(info))
3040 				sprintf(rs, "\r\n1");
3041 #endif
3042 			isdn_tty_at_cout(rs, info);
3043 			break;
3044 		case '=':
3045 			p[0]++;
3046 			switch (*p[0]) {
3047 			case '0':
3048 				p[0]++;
3049 				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3050 				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3051 				m->mdmreg[REG_SI1] = 4;
3052 				info->xmit_size =
3053 					m->mdmreg[REG_PSIZE] * 16;
3054 				break;
3055 #ifdef CONFIG_ISDN_TTY_FAX
3056 			case '1':
3057 				p[0]++;
3058 				if (!(dev->global_features &
3059 				      ISDN_FEATURE_L3_FCLASS1))
3060 					PARSE_ERROR1;
3061 				m->mdmreg[REG_SI1] = 1;
3062 				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3063 				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3064 				info->xmit_size =
3065 					m->mdmreg[REG_PSIZE] * 16;
3066 				break;
3067 			case '2':
3068 				p[0]++;
3069 				if (!(dev->global_features &
3070 				      ISDN_FEATURE_L3_FCLASS2))
3071 					PARSE_ERROR1;
3072 				m->mdmreg[REG_SI1] = 1;
3073 				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3074 				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3075 				info->xmit_size =
3076 					m->mdmreg[REG_PSIZE] * 16;
3077 				break;
3078 #endif
3079 			case '8':
3080 				p[0]++;
3081 				/* L2 will change on dialout with si=1 */
3082 				m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3083 				m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3084 				m->mdmreg[REG_SI1] = 5;
3085 				info->xmit_size = VBUF;
3086 				break;
3087 			case '?':
3088 				p[0]++;
3089 				strcpy(rs, "\r\n0,");
3090 #ifdef CONFIG_ISDN_TTY_FAX
3091 				if (dev->global_features &
3092 				    ISDN_FEATURE_L3_FCLASS1)
3093 					strcat(rs, "1,");
3094 				if (dev->global_features &
3095 				    ISDN_FEATURE_L3_FCLASS2)
3096 					strcat(rs, "2,");
3097 #endif
3098 				strcat(rs, "8");
3099 				isdn_tty_at_cout(rs, info);
3100 				break;
3101 			default:
3102 				PARSE_ERROR1;
3103 			}
3104 			break;
3105 		default:
3106 			PARSE_ERROR1;
3107 		}
3108 		return 0;
3109 	}
3110 #ifdef CONFIG_ISDN_TTY_FAX
3111 	return (isdn_tty_cmd_PLUSF_FAX(p, info));
3112 #else
3113 	PARSE_ERROR1;
3114 #endif
3115 }
3116 
3117 /*
3118  * Parse AT+V.. commands
3119  */
3120 static int
isdn_tty_cmd_PLUSV(char ** p,modem_info * info)3121 isdn_tty_cmd_PLUSV(char **p, modem_info *info)
3122 {
3123 	atemu *m = &info->emu;
3124 	isdn_ctrl cmd;
3125 	static char *vcmd[] =
3126 		{"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
3127 	int i;
3128 	int par1;
3129 	int par2;
3130 	char rs[20];
3131 
3132 	i = 0;
3133 	while (vcmd[i]) {
3134 		if (!strncmp(vcmd[i], p[0], 2)) {
3135 			p[0] += 2;
3136 			break;
3137 		}
3138 		i++;
3139 	}
3140 	switch (i) {
3141 	case 0:
3142 		/* AT+VNH - Auto hangup feature */
3143 		switch (*p[0]) {
3144 		case '?':
3145 			p[0]++;
3146 			isdn_tty_at_cout("\r\n1", info);
3147 			break;
3148 		case '=':
3149 			p[0]++;
3150 			switch (*p[0]) {
3151 			case '1':
3152 				p[0]++;
3153 				break;
3154 			case '?':
3155 				p[0]++;
3156 				isdn_tty_at_cout("\r\n1", info);
3157 				break;
3158 			default:
3159 				PARSE_ERROR1;
3160 			}
3161 			break;
3162 		default:
3163 			PARSE_ERROR1;
3164 		}
3165 		break;
3166 	case 1:
3167 		/* AT+VIP - Reset all voice parameters */
3168 		isdn_tty_modem_reset_vpar(m);
3169 		break;
3170 	case 2:
3171 		/* AT+VLS - Select device, accept incoming call */
3172 		switch (*p[0]) {
3173 		case '?':
3174 			p[0]++;
3175 			sprintf(rs, "\r\n%d", m->vpar[0]);
3176 			isdn_tty_at_cout(rs, info);
3177 			break;
3178 		case '=':
3179 			p[0]++;
3180 			switch (*p[0]) {
3181 			case '0':
3182 				p[0]++;
3183 				m->vpar[0] = 0;
3184 				break;
3185 			case '2':
3186 				p[0]++;
3187 				m->vpar[0] = 2;
3188 				break;
3189 			case '?':
3190 				p[0]++;
3191 				isdn_tty_at_cout("\r\n0,2", info);
3192 				break;
3193 			default:
3194 				PARSE_ERROR1;
3195 			}
3196 			break;
3197 		default:
3198 			PARSE_ERROR1;
3199 		}
3200 		break;
3201 	case 3:
3202 		/* AT+VRX - Start recording */
3203 		if (!m->vpar[0])
3204 			PARSE_ERROR1;
3205 		if (info->online != 1) {
3206 			isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3207 			return 1;
3208 		}
3209 		info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3210 		if (!info->dtmf_state) {
3211 			printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3212 			PARSE_ERROR1;
3213 		}
3214 		info->silence_state = isdn_audio_silence_init(info->silence_state);
3215 		if (!info->silence_state) {
3216 			printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3217 			PARSE_ERROR1;
3218 		}
3219 		if (m->vpar[3] < 5) {
3220 			info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3221 			if (!info->adpcmr) {
3222 				printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3223 				PARSE_ERROR1;
3224 			}
3225 		}
3226 #ifdef ISDN_DEBUG_AT
3227 		printk(KERN_DEBUG "AT: +VRX\n");
3228 #endif
3229 		info->vonline |= 1;
3230 		isdn_tty_modem_result(RESULT_CONNECT, info);
3231 		return 0;
3232 		break;
3233 	case 4:
3234 		/* AT+VSD - Silence detection */
3235 		switch (*p[0]) {
3236 		case '?':
3237 			p[0]++;
3238 			sprintf(rs, "\r\n<%d>,<%d>",
3239 				m->vpar[1],
3240 				m->vpar[2]);
3241 			isdn_tty_at_cout(rs, info);
3242 			break;
3243 		case '=':
3244 			p[0]++;
3245 			if ((*p[0] >= '0') && (*p[0] <= '9')) {
3246 				par1 = isdn_getnum(p);
3247 				if ((par1 < 0) || (par1 > 31))
3248 					PARSE_ERROR1;
3249 				if (*p[0] != ',')
3250 					PARSE_ERROR1;
3251 				p[0]++;
3252 				par2 = isdn_getnum(p);
3253 				if ((par2 < 0) || (par2 > 255))
3254 					PARSE_ERROR1;
3255 				m->vpar[1] = par1;
3256 				m->vpar[2] = par2;
3257 				break;
3258 			} else
3259 				if (*p[0] == '?') {
3260 					p[0]++;
3261 					isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3262 							 info);
3263 					break;
3264 				} else
3265 					PARSE_ERROR1;
3266 			break;
3267 		default:
3268 			PARSE_ERROR1;
3269 		}
3270 		break;
3271 	case 5:
3272 		/* AT+VSM - Select compression */
3273 		switch (*p[0]) {
3274 		case '?':
3275 			p[0]++;
3276 			sprintf(rs, "\r\n<%d>,<%d><8000>",
3277 				m->vpar[3],
3278 				m->vpar[1]);
3279 			isdn_tty_at_cout(rs, info);
3280 			break;
3281 		case '=':
3282 			p[0]++;
3283 			switch (*p[0]) {
3284 			case '2':
3285 			case '3':
3286 			case '4':
3287 			case '5':
3288 			case '6':
3289 				par1 = isdn_getnum(p);
3290 				if ((par1 < 2) || (par1 > 6))
3291 					PARSE_ERROR1;
3292 				m->vpar[3] = par1;
3293 				break;
3294 			case '?':
3295 				p[0]++;
3296 				isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3297 						 info);
3298 				isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3299 						 info);
3300 				isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3301 						 info);
3302 				isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3303 						 info);
3304 				isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3305 						 info);
3306 				break;
3307 			default:
3308 				PARSE_ERROR1;
3309 			}
3310 			break;
3311 		default:
3312 			PARSE_ERROR1;
3313 		}
3314 		break;
3315 	case 6:
3316 		/* AT+VTX - Start sending */
3317 		if (!m->vpar[0])
3318 			PARSE_ERROR1;
3319 		if (info->online != 1) {
3320 			isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3321 			return 1;
3322 		}
3323 		info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3324 		if (!info->dtmf_state) {
3325 			printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3326 			PARSE_ERROR1;
3327 		}
3328 		if (m->vpar[3] < 5) {
3329 			info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3330 			if (!info->adpcms) {
3331 				printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3332 				PARSE_ERROR1;
3333 			}
3334 		}
3335 #ifdef ISDN_DEBUG_AT
3336 		printk(KERN_DEBUG "AT: +VTX\n");
3337 #endif
3338 		m->lastDLE = 0;
3339 		info->vonline |= 2;
3340 		isdn_tty_modem_result(RESULT_CONNECT, info);
3341 		return 0;
3342 		break;
3343 	case 7:
3344 		/* AT+VDD - DTMF detection */
3345 		switch (*p[0]) {
3346 		case '?':
3347 			p[0]++;
3348 			sprintf(rs, "\r\n<%d>,<%d>",
3349 				m->vpar[4],
3350 				m->vpar[5]);
3351 			isdn_tty_at_cout(rs, info);
3352 			break;
3353 		case '=':
3354 			p[0]++;
3355 			if ((*p[0] >= '0') && (*p[0] <= '9')) {
3356 				if (info->online != 1)
3357 					PARSE_ERROR1;
3358 				par1 = isdn_getnum(p);
3359 				if ((par1 < 0) || (par1 > 15))
3360 					PARSE_ERROR1;
3361 				if (*p[0] != ',')
3362 					PARSE_ERROR1;
3363 				p[0]++;
3364 				par2 = isdn_getnum(p);
3365 				if ((par2 < 0) || (par2 > 255))
3366 					PARSE_ERROR1;
3367 				m->vpar[4] = par1;
3368 				m->vpar[5] = par2;
3369 				cmd.driver = info->isdn_driver;
3370 				cmd.command = ISDN_CMD_AUDIO;
3371 				cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3372 				cmd.parm.num[0] = par1;
3373 				cmd.parm.num[1] = par2;
3374 				isdn_command(&cmd);
3375 				break;
3376 			} else
3377 				if (*p[0] == '?') {
3378 					p[0]++;
3379 					isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3380 							 info);
3381 					break;
3382 				} else
3383 					PARSE_ERROR1;
3384 			break;
3385 		default:
3386 			PARSE_ERROR1;
3387 		}
3388 		break;
3389 	default:
3390 		PARSE_ERROR1;
3391 	}
3392 	return 0;
3393 }
3394 #endif                          /* CONFIG_ISDN_AUDIO */
3395 
3396 /*
3397  * Parse and perform an AT-command-line.
3398  */
3399 static void
isdn_tty_parse_at(modem_info * info)3400 isdn_tty_parse_at(modem_info *info)
3401 {
3402 	atemu *m = &info->emu;
3403 	char *p;
3404 	char ds[ISDN_MSNLEN];
3405 
3406 #ifdef ISDN_DEBUG_AT
3407 	printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
3408 #endif
3409 	for (p = &m->mdmcmd[2]; *p;) {
3410 		switch (*p) {
3411 		case ' ':
3412 			p++;
3413 			break;
3414 		case 'A':
3415 			/* A - Accept incoming call */
3416 			p++;
3417 			isdn_tty_cmd_ATA(info);
3418 			return;
3419 		case 'D':
3420 			/* D - Dial */
3421 			if (info->msr & UART_MSR_DCD)
3422 				PARSE_ERROR;
3423 			if (info->msr & UART_MSR_RI) {
3424 				isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3425 				return;
3426 			}
3427 			isdn_tty_getdial(++p, ds, sizeof ds);
3428 			p += strlen(p);
3429 			if (!strlen(m->msn))
3430 				isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
3431 			else if (strlen(ds))
3432 				isdn_tty_dial(ds, info, m);
3433 			else
3434 				PARSE_ERROR;
3435 			return;
3436 		case 'E':
3437 			/* E - Turn Echo on/off */
3438 			p++;
3439 			switch (isdn_getnum(&p)) {
3440 			case 0:
3441 				m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
3442 				break;
3443 			case 1:
3444 				m->mdmreg[REG_ECHO] |= BIT_ECHO;
3445 				break;
3446 			default:
3447 				PARSE_ERROR;
3448 			}
3449 			break;
3450 		case 'H':
3451 			/* H - On/Off-hook */
3452 			p++;
3453 			switch (*p) {
3454 			case '0':
3455 				p++;
3456 				isdn_tty_on_hook(info);
3457 				break;
3458 			case '1':
3459 				p++;
3460 				isdn_tty_off_hook();
3461 				break;
3462 			default:
3463 				isdn_tty_on_hook(info);
3464 				break;
3465 			}
3466 			break;
3467 		case 'I':
3468 			/* I - Information */
3469 			p++;
3470 			isdn_tty_at_cout("\r\nLinux ISDN", info);
3471 			switch (*p) {
3472 			case '0':
3473 			case '1':
3474 				p++;
3475 				break;
3476 			case '2':
3477 				p++;
3478 				isdn_tty_report(info);
3479 				break;
3480 			case '3':
3481 				p++;
3482 				snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
3483 				isdn_tty_at_cout(ds, info);
3484 				break;
3485 			default:;
3486 			}
3487 			break;
3488 #ifdef DUMMY_HAYES_AT
3489 		case 'L':
3490 		case 'M':
3491 			/* only for be compilant with common scripts */
3492 			/* no function */
3493 			p++;
3494 			isdn_getnum(&p);
3495 			break;
3496 #endif
3497 		case 'O':
3498 			/* O - Go online */
3499 			p++;
3500 			if (info->msr & UART_MSR_DCD)
3501 				/* if B-Channel is up */
3502 				isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
3503 			else
3504 				isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3505 			return;
3506 		case 'Q':
3507 			/* Q - Turn Emulator messages on/off */
3508 			p++;
3509 			switch (isdn_getnum(&p)) {
3510 			case 0:
3511 				m->mdmreg[REG_RESP] |= BIT_RESP;
3512 				break;
3513 			case 1:
3514 				m->mdmreg[REG_RESP] &= ~BIT_RESP;
3515 				break;
3516 			default:
3517 				PARSE_ERROR;
3518 			}
3519 			break;
3520 		case 'S':
3521 			/* S - Set/Get Register */
3522 			p++;
3523 			if (isdn_tty_cmd_ATS(&p, info))
3524 				return;
3525 			break;
3526 		case 'V':
3527 			/* V - Numeric or ASCII Emulator-messages */
3528 			p++;
3529 			switch (isdn_getnum(&p)) {
3530 			case 0:
3531 				m->mdmreg[REG_RESP] |= BIT_RESPNUM;
3532 				break;
3533 			case 1:
3534 				m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
3535 				break;
3536 			default:
3537 				PARSE_ERROR;
3538 			}
3539 			break;
3540 		case 'Z':
3541 			/* Z - Load Registers from Profile */
3542 			p++;
3543 			if (info->msr & UART_MSR_DCD) {
3544 				info->online = 0;
3545 				isdn_tty_on_hook(info);
3546 			}
3547 			isdn_tty_modem_reset_regs(info, 1);
3548 			break;
3549 		case '+':
3550 			p++;
3551 			switch (*p) {
3552 #ifdef CONFIG_ISDN_AUDIO
3553 			case 'F':
3554 				p++;
3555 				if (isdn_tty_cmd_PLUSF(&p, info))
3556 					return;
3557 				break;
3558 			case 'V':
3559 				if ((!(m->mdmreg[REG_SI1] & 1)) ||
3560 				    (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
3561 					PARSE_ERROR;
3562 				p++;
3563 				if (isdn_tty_cmd_PLUSV(&p, info))
3564 					return;
3565 				break;
3566 #endif                          /* CONFIG_ISDN_AUDIO */
3567 			case 'S':	/* SUSPEND */
3568 				p++;
3569 				isdn_tty_get_msnstr(ds, &p);
3570 				isdn_tty_suspend(ds, info, m);
3571 				break;
3572 			case 'R':	/* RESUME */
3573 				p++;
3574 				isdn_tty_get_msnstr(ds, &p);
3575 				isdn_tty_resume(ds, info, m);
3576 				break;
3577 			case 'M':	/* MESSAGE */
3578 				p++;
3579 				isdn_tty_send_msg(info, m, p);
3580 				break;
3581 			default:
3582 				PARSE_ERROR;
3583 			}
3584 			break;
3585 		case '&':
3586 			p++;
3587 			if (isdn_tty_cmd_ATand(&p, info))
3588 				return;
3589 			break;
3590 		default:
3591 			PARSE_ERROR;
3592 		}
3593 	}
3594 #ifdef CONFIG_ISDN_AUDIO
3595 	if (!info->vonline)
3596 #endif
3597 		isdn_tty_modem_result(RESULT_OK, info);
3598 }
3599 
3600 /* Need own toupper() because standard-toupper is not available
3601  * within modules.
3602  */
3603 #define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
3604 
3605 /*
3606  * Perform line-editing of AT-commands
3607  *
3608  * Parameters:
3609  *   p        inputbuffer
3610  *   count    length of buffer
3611  *   channel  index to line (minor-device)
3612  */
3613 static int
isdn_tty_edit_at(const char * p,int count,modem_info * info)3614 isdn_tty_edit_at(const char *p, int count, modem_info *info)
3615 {
3616 	atemu *m = &info->emu;
3617 	int total = 0;
3618 	u_char c;
3619 	char eb[2];
3620 	int cnt;
3621 
3622 	for (cnt = count; cnt > 0; p++, cnt--) {
3623 		c = *p;
3624 		total++;
3625 		if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
3626 			/* Separator (CR or LF) */
3627 			m->mdmcmd[m->mdmcmdl] = 0;
3628 			if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3629 				eb[0] = c;
3630 				eb[1] = 0;
3631 				isdn_tty_at_cout(eb, info);
3632 			}
3633 			if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
3634 				isdn_tty_parse_at(info);
3635 			m->mdmcmdl = 0;
3636 			continue;
3637 		}
3638 		if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
3639 			/* Backspace-Function */
3640 			if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
3641 				if (m->mdmcmdl)
3642 					m->mdmcmdl--;
3643 				if (m->mdmreg[REG_ECHO] & BIT_ECHO)
3644 					isdn_tty_at_cout("\b", info);
3645 			}
3646 			continue;
3647 		}
3648 		if (cmdchar(c)) {
3649 			if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3650 				eb[0] = c;
3651 				eb[1] = 0;
3652 				isdn_tty_at_cout(eb, info);
3653 			}
3654 			if (m->mdmcmdl < 255) {
3655 				c = my_toupper(c);
3656 				switch (m->mdmcmdl) {
3657 				case 1:
3658 					if (c == 'T') {
3659 						m->mdmcmd[m->mdmcmdl] = c;
3660 						m->mdmcmd[++m->mdmcmdl] = 0;
3661 						break;
3662 					} else
3663 						m->mdmcmdl = 0;
3664 					/* Fall through, check for 'A' */
3665 				case 0:
3666 					if (c == 'A') {
3667 						m->mdmcmd[m->mdmcmdl] = c;
3668 						m->mdmcmd[++m->mdmcmdl] = 0;
3669 					}
3670 					break;
3671 				default:
3672 					m->mdmcmd[m->mdmcmdl] = c;
3673 					m->mdmcmd[++m->mdmcmdl] = 0;
3674 				}
3675 			}
3676 		}
3677 	}
3678 	return total;
3679 }
3680 
3681 /*
3682  * Switch all modem-channels who are online and got a valid
3683  * escape-sequence 1.5 seconds ago, to command-mode.
3684  * This function is called every second via timer-interrupt from within
3685  * timer-dispatcher isdn_timer_function()
3686  */
3687 void
isdn_tty_modem_escape(void)3688 isdn_tty_modem_escape(void)
3689 {
3690 	int ton = 0;
3691 	int i;
3692 	int midx;
3693 
3694 	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
3695 		if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
3696 			modem_info *info = &dev->mdm.info[midx];
3697 			if (info->online) {
3698 				ton = 1;
3699 				if ((info->emu.pluscount == 3) &&
3700 				    time_after(jiffies,
3701 					    info->emu.lastplus + PLUSWAIT2)) {
3702 					info->emu.pluscount = 0;
3703 					info->online = 0;
3704 					isdn_tty_modem_result(RESULT_OK, info);
3705 				}
3706 			}
3707 		}
3708 	isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
3709 }
3710 
3711 /*
3712  * Put a RING-message to all modem-channels who have the RI-bit set.
3713  * This function is called every second via timer-interrupt from within
3714  * timer-dispatcher isdn_timer_function()
3715  */
3716 void
isdn_tty_modem_ring(void)3717 isdn_tty_modem_ring(void)
3718 {
3719 	int ton = 0;
3720 	int i;
3721 
3722 	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3723 		modem_info *info = &dev->mdm.info[i];
3724 		if (info->msr & UART_MSR_RI) {
3725 			ton = 1;
3726 			isdn_tty_modem_result(RESULT_RING, info);
3727 		}
3728 	}
3729 	isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
3730 }
3731 
3732 /*
3733  * For all online tty's, try sending data to
3734  * the lower levels.
3735  */
3736 void
isdn_tty_modem_xmit(void)3737 isdn_tty_modem_xmit(void)
3738 {
3739 	int ton = 1;
3740 	int i;
3741 
3742 	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3743 		modem_info *info = &dev->mdm.info[i];
3744 		if (info->online) {
3745 			ton = 1;
3746 			isdn_tty_senddown(info);
3747 			isdn_tty_tint(info);
3748 		}
3749 	}
3750 	isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
3751 }
3752 
3753 /*
3754  * Check all channels if we have a 'no carrier' timeout.
3755  * Timeout value is set by Register S7.
3756  */
3757 void
isdn_tty_carrier_timeout(void)3758 isdn_tty_carrier_timeout(void)
3759 {
3760 	int ton = 0;
3761 	int i;
3762 
3763 	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3764 		modem_info *info = &dev->mdm.info[i];
3765 		if (!info->dialing)
3766 			continue;
3767 		if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
3768 			info->dialing = 0;
3769 			isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3770 			isdn_tty_modem_hup(info, 1);
3771 		} else
3772 			ton = 1;
3773 	}
3774 	isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
3775 }
3776