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