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