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