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