blob: 27f70923c224eb4674c589fa9b1b02614fdcd35b [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))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 info->flags |= ISDN_ASYNC_NORMAL_ACTIVE;
1508 return 0;
1509 }
Jiri Slaby05eb48b2012-04-02 13:53:49 +02001510 if (tty->termios->c_cflag & CLOCAL)
1511 do_clocal = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /*
1513 * Block waiting for the carrier detect and the line to become
1514 * free (i.e., not in use by the callout). While we are in
1515 * this loop, info->count is dropped by one, so that
1516 * isdn_tty_close() knows when to free things. We restore it upon
1517 * exit, either normal or abnormal.
1518 */
1519 retval = 0;
1520 add_wait_queue(&info->open_wait, &wait);
1521#ifdef ISDN_DEBUG_MODEM_OPEN
1522 printk(KERN_DEBUG "isdn_tty_block_til_ready before block: ttyi%d, count = %d\n",
1523 info->line, info->count);
1524#endif
1525 if (!(tty_hung_up_p(filp)))
1526 info->count--;
1527 info->blocked_open++;
1528 while (1) {
1529 set_current_state(TASK_INTERRUPTIBLE);
1530 if (tty_hung_up_p(filp) ||
1531 !(info->flags & ISDN_ASYNC_INITIALIZED)) {
1532#ifdef MODEM_DO_RESTART
1533 if (info->flags & ISDN_ASYNC_HUP_NOTIFY)
1534 retval = -EAGAIN;
1535 else
1536 retval = -ERESTARTSYS;
1537#else
1538 retval = -EAGAIN;
1539#endif
1540 break;
1541 }
Jiri Slaby05eb48b2012-04-02 13:53:49 +02001542 if (!(info->flags & ISDN_ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 (do_clocal || (info->msr & UART_MSR_DCD))) {
1544 break;
1545 }
1546 if (signal_pending(current)) {
1547 retval = -ERESTARTSYS;
1548 break;
1549 }
1550#ifdef ISDN_DEBUG_MODEM_OPEN
1551 printk(KERN_DEBUG "isdn_tty_block_til_ready blocking: ttyi%d, count = %d\n",
1552 info->line, info->count);
1553#endif
1554 schedule();
1555 }
1556 current->state = TASK_RUNNING;
1557 remove_wait_queue(&info->open_wait, &wait);
1558 if (!tty_hung_up_p(filp))
1559 info->count++;
1560 info->blocked_open--;
1561#ifdef ISDN_DEBUG_MODEM_OPEN
1562 printk(KERN_DEBUG "isdn_tty_block_til_ready after blocking: ttyi%d, count = %d\n",
1563 info->line, info->count);
1564#endif
1565 if (retval)
1566 return retval;
1567 info->flags |= ISDN_ASYNC_NORMAL_ACTIVE;
1568 return 0;
1569}
1570
1571/*
1572 * This routine is called whenever a serial port is opened. It
1573 * enables interrupts for a serial port, linking in its async structure into
1574 * the IRQ chain. It also performs the serial-specific
1575 * initialization for the tty structure.
1576 */
1577static int
1578isdn_tty_open(struct tty_struct *tty, struct file *filp)
1579{
1580 modem_info *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001581 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Jiri Slaby410235f2012-03-05 14:52:01 +01001583 info = &dev->mdm.info[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_open"))
1585 return -ENODEV;
1586 if (!try_module_get(info->owner)) {
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001587 printk(KERN_WARNING "%s: cannot reserve module\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return -ENODEV;
1589 }
1590#ifdef ISDN_DEBUG_MODEM_OPEN
Joe Perches475be4d2012-02-19 19:52:38 -08001591 printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 info->count);
1593#endif
1594 info->count++;
1595 tty->driver_data = info;
1596 info->tty = tty;
1597 /*
1598 * Start up serial port
1599 */
1600 retval = isdn_tty_startup(info);
1601 if (retval) {
1602#ifdef ISDN_DEBUG_MODEM_OPEN
1603 printk(KERN_DEBUG "isdn_tty_open return after startup\n");
1604#endif
1605 module_put(info->owner);
1606 return retval;
1607 }
1608 retval = isdn_tty_block_til_ready(tty, filp, info);
1609 if (retval) {
1610#ifdef ISDN_DEBUG_MODEM_OPEN
1611 printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
1612#endif
1613 module_put(info->owner);
1614 return retval;
1615 }
1616#ifdef ISDN_DEBUG_MODEM_OPEN
1617 printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
1618#endif
1619 dev->modempoll++;
1620#ifdef ISDN_DEBUG_MODEM_OPEN
1621 printk(KERN_DEBUG "isdn_tty_open normal exit\n");
1622#endif
1623 return 0;
1624}
1625
1626static void
1627isdn_tty_close(struct tty_struct *tty, struct file *filp)
1628{
1629 modem_info *info = (modem_info *) tty->driver_data;
1630 ulong timeout;
1631
1632 if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
1633 return;
1634 if (tty_hung_up_p(filp)) {
1635#ifdef ISDN_DEBUG_MODEM_OPEN
1636 printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
1637#endif
1638 return;
1639 }
1640 if ((tty->count == 1) && (info->count != 1)) {
1641 /*
1642 * Uh, oh. tty->count is 1, which means that the tty
1643 * structure will be freed. Info->count should always
1644 * be one in these conditions. If it's greater than
1645 * one, we've got real problems, since it means the
1646 * serial port won't be shutdown.
1647 */
1648 printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
1649 "info->count is %d\n", info->count);
1650 info->count = 1;
1651 }
1652 if (--info->count < 0) {
1653 printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
1654 info->line, info->count);
1655 info->count = 0;
1656 }
1657 if (info->count) {
1658#ifdef ISDN_DEBUG_MODEM_OPEN
1659 printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
1660#endif
Karsten Keil7cb94782006-03-06 15:42:39 -08001661 module_put(info->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return;
1663 }
1664 info->flags |= ISDN_ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 tty->closing = 1;
1667 /*
1668 * At this point we stop accepting input. To do this, we
1669 * disable the receive line status interrupts, and tell the
1670 * interrupt driver to stop checking the data ready bit in the
1671 * line status register.
1672 */
1673 if (info->flags & ISDN_ASYNC_INITIALIZED) {
Jiri Slaby0b058352011-08-25 15:12:08 +02001674 tty_wait_until_sent_from_close(tty, 3000); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
1676 * Before we drop DTR, make sure the UART transmitter
1677 * has completely drained; this is especially
1678 * important if there is a transmit FIFO!
1679 */
1680 timeout = jiffies + HZ;
1681 while (!(info->lsr & UART_LSR_TEMT)) {
Nishanth Aravamudan24763c42005-11-07 01:01:16 -08001682 schedule_timeout_interruptible(20);
Joe Perches475be4d2012-02-19 19:52:38 -08001683 if (time_after(jiffies, timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 break;
1685 }
1686 }
1687 dev->modempoll--;
1688 isdn_tty_shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001689 isdn_tty_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 tty_ldisc_flush(tty);
1691 info->tty = NULL;
1692 info->ncarrier = 0;
1693 tty->closing = 0;
1694 module_put(info->owner);
1695 if (info->blocked_open) {
1696 msleep_interruptible(500);
1697 wake_up_interruptible(&info->open_wait);
1698 }
1699 info->flags &= ~(ISDN_ASYNC_NORMAL_ACTIVE | ISDN_ASYNC_CLOSING);
1700 wake_up_interruptible(&info->close_wait);
1701#ifdef ISDN_DEBUG_MODEM_OPEN
1702 printk(KERN_DEBUG "isdn_tty_close normal exit\n");
1703#endif
1704}
1705
1706/*
1707 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1708 */
1709static void
1710isdn_tty_hangup(struct tty_struct *tty)
1711{
1712 modem_info *info = (modem_info *) tty->driver_data;
1713
1714 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
1715 return;
1716 isdn_tty_shutdown(info);
1717 info->count = 0;
Jiri Slaby05eb48b2012-04-02 13:53:49 +02001718 info->flags &= ~ISDN_ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 info->tty = NULL;
1720 wake_up_interruptible(&info->open_wait);
1721}
1722
1723/* This routine initializes all emulator-data.
1724 */
1725static void
Joe Perches475be4d2012-02-19 19:52:38 -08001726isdn_tty_reset_profile(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 m->profile[0] = 0;
1729 m->profile[1] = 0;
1730 m->profile[2] = 43;
1731 m->profile[3] = 13;
1732 m->profile[4] = 10;
1733 m->profile[5] = 8;
1734 m->profile[6] = 3;
1735 m->profile[7] = 60;
1736 m->profile[8] = 2;
1737 m->profile[9] = 6;
1738 m->profile[10] = 7;
1739 m->profile[11] = 70;
1740 m->profile[12] = 0x45;
1741 m->profile[13] = 4;
1742 m->profile[14] = ISDN_PROTO_L2_X75I;
1743 m->profile[15] = ISDN_PROTO_L3_TRANS;
1744 m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
1745 m->profile[17] = ISDN_MODEM_WINSIZE;
1746 m->profile[18] = 4;
1747 m->profile[19] = 0;
1748 m->profile[20] = 0;
1749 m->profile[23] = 0;
1750 m->pmsn[0] = '\0';
1751 m->plmsn[0] = '\0';
1752}
1753
1754#ifdef CONFIG_ISDN_AUDIO
1755static void
Joe Perches475be4d2012-02-19 19:52:38 -08001756isdn_tty_modem_reset_vpar(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 m->vpar[0] = 2; /* Voice-device (2 = phone line) */
1759 m->vpar[1] = 0; /* Silence detection level (0 = none ) */
1760 m->vpar[2] = 70; /* Silence interval (7 sec. ) */
1761 m->vpar[3] = 2; /* Compression type (1 = ADPCM-2 ) */
1762 m->vpar[4] = 0; /* DTMF detection level (0 = softcode ) */
1763 m->vpar[5] = 8; /* DTMF interval (8 * 5 ms. ) */
1764}
1765#endif
1766
1767#ifdef CONFIG_ISDN_TTY_FAX
1768static void
Joe Perches475be4d2012-02-19 19:52:38 -08001769isdn_tty_modem_reset_faxpar(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 T30_s *f = info->fax;
1772
1773 f->code = 0;
1774 f->phase = ISDN_FAX_PHASE_IDLE;
1775 f->direction = 0;
1776 f->resolution = 1; /* fine */
1777 f->rate = 5; /* 14400 bit/s */
1778 f->width = 0;
1779 f->length = 0;
1780 f->compression = 0;
1781 f->ecm = 0;
1782 f->binary = 0;
1783 f->scantime = 0;
1784 memset(&f->id[0], 32, FAXIDLEN - 1);
1785 f->id[FAXIDLEN - 1] = 0;
1786 f->badlin = 0;
1787 f->badmul = 0;
1788 f->bor = 0;
1789 f->nbc = 0;
1790 f->cq = 0;
1791 f->cr = 0;
1792 f->ctcrty = 0;
1793 f->minsp = 0;
1794 f->phcto = 30;
1795 f->rel = 0;
1796 memset(&f->pollid[0], 32, FAXIDLEN - 1);
1797 f->pollid[FAXIDLEN - 1] = 0;
1798}
1799#endif
1800
1801static void
Joe Perches475be4d2012-02-19 19:52:38 -08001802isdn_tty_modem_reset_regs(modem_info *info, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 atemu *m = &info->emu;
1805 if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
1806 memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
1807 memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
1808 memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
1809 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
1810 }
1811#ifdef CONFIG_ISDN_AUDIO
1812 isdn_tty_modem_reset_vpar(m);
1813#endif
1814#ifdef CONFIG_ISDN_TTY_FAX
1815 isdn_tty_modem_reset_faxpar(info);
1816#endif
1817 m->mdmcmdl = 0;
1818}
1819
1820static void
Joe Perches475be4d2012-02-19 19:52:38 -08001821modem_write_profile(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
1824 memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
1825 memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
1826 if (dev->profd)
1827 send_sig(SIGIO, dev->profd, 1);
1828}
1829
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001830static const struct tty_operations modem_ops = {
Joe Perches475be4d2012-02-19 19:52:38 -08001831 .open = isdn_tty_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 .close = isdn_tty_close,
1833 .write = isdn_tty_write,
1834 .flush_chars = isdn_tty_flush_chars,
1835 .write_room = isdn_tty_write_room,
1836 .chars_in_buffer = isdn_tty_chars_in_buffer,
1837 .flush_buffer = isdn_tty_flush_buffer,
1838 .ioctl = isdn_tty_ioctl,
1839 .throttle = isdn_tty_throttle,
1840 .unthrottle = isdn_tty_unthrottle,
1841 .set_termios = isdn_tty_set_termios,
1842 .hangup = isdn_tty_hangup,
1843 .tiocmget = isdn_tty_tiocmget,
1844 .tiocmset = isdn_tty_tiocmset,
1845};
1846
1847int
1848isdn_tty_modem_init(void)
1849{
1850 isdn_modem_t *m;
1851 int i, retval;
1852 modem_info *info;
1853
1854 m = &dev->mdm;
1855 m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
1856 if (!m->tty_modem)
1857 return -ENOMEM;
1858 m->tty_modem->name = "ttyI";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 m->tty_modem->major = ISDN_TTY_MAJOR;
1860 m->tty_modem->minor_start = 0;
1861 m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
1862 m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
1863 m->tty_modem->init_termios = tty_std_termios;
1864 m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001865 m->tty_modem->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 m->tty_modem->driver_name = "isdn_tty";
1867 tty_set_operations(m->tty_modem, &modem_ops);
1868 retval = tty_register_driver(m->tty_modem);
1869 if (retval) {
1870 printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
1871 goto err;
1872 }
1873 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1874 info = &m->info[i];
1875#ifdef CONFIG_ISDN_TTY_FAX
1876 if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
1877 printk(KERN_ERR "Could not allocate fax t30-buffer\n");
1878 retval = -ENOMEM;
1879 goto err_unregister;
1880 }
1881#endif
1882#ifdef MODULE
1883 info->owner = THIS_MODULE;
1884#endif
1885 spin_lock_init(&info->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 sprintf(info->last_cause, "0000");
1887 sprintf(info->last_num, "none");
1888 info->last_dir = 0;
1889 info->last_lhup = 1;
1890 info->last_l2 = -1;
1891 info->last_si = 0;
1892 isdn_tty_reset_profile(&info->emu);
1893 isdn_tty_modem_reset_regs(info, 1);
1894 info->magic = ISDN_ASYNC_MAGIC;
1895 info->line = i;
1896 info->tty = NULL;
1897 info->x_char = 0;
1898 info->count = 0;
1899 info->blocked_open = 0;
1900 init_waitqueue_head(&info->open_wait);
1901 init_waitqueue_head(&info->close_wait);
1902 info->isdn_driver = -1;
1903 info->isdn_channel = -1;
1904 info->drv_index = -1;
1905 info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
1906 init_timer(&info->nc_timer);
1907 info->nc_timer.function = isdn_tty_modem_do_ncarrier;
1908 info->nc_timer.data = (unsigned long) info;
1909 skb_queue_head_init(&info->xmit_queue);
1910#ifdef CONFIG_ISDN_AUDIO
1911 skb_queue_head_init(&info->dtmf_queue);
1912#endif
1913 if (!(info->xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5, GFP_KERNEL))) {
1914 printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
1915 retval = -ENOMEM;
1916 goto err_unregister;
1917 }
1918 /* Make room for T.70 header */
1919 info->xmit_buf += 4;
1920 }
1921 return 0;
1922err_unregister:
1923 for (i--; i >= 0; i--) {
1924 info = &m->info[i];
1925#ifdef CONFIG_ISDN_TTY_FAX
1926 kfree(info->fax);
1927#endif
1928 kfree(info->xmit_buf - 4);
1929 }
1930 tty_unregister_driver(m->tty_modem);
Joe Perches475be4d2012-02-19 19:52:38 -08001931err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 put_tty_driver(m->tty_modem);
1933 m->tty_modem = NULL;
1934 return retval;
1935}
1936
1937void
1938isdn_tty_exit(void)
1939{
1940 modem_info *info;
1941 int i;
1942
1943 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1944 info = &dev->mdm.info[i];
1945 isdn_tty_cleanup_xmit(info);
1946#ifdef CONFIG_ISDN_TTY_FAX
1947 kfree(info->fax);
1948#endif
1949 kfree(info->xmit_buf - 4);
1950 }
1951 tty_unregister_driver(dev->mdm.tty_modem);
1952 put_tty_driver(dev->mdm.tty_modem);
1953 dev->mdm.tty_modem = NULL;
1954}
1955
1956
1957/*
1958 * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
1959 * match the MSN against the MSNs (glob patterns) defined for tty_emulator,
1960 * and return 0 for match, 1 for no match, 2 if MSN could match if longer.
1961 */
1962
1963static int
1964isdn_tty_match_icall(char *cid, atemu *emu, int di)
1965{
1966#ifdef ISDN_DEBUG_MODEM_ICALL
1967 printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
1968 emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
1969 emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
1970#endif
1971 if (strlen(emu->lmsn)) {
1972 char *p = emu->lmsn;
1973 char *q;
1974 int tmp;
1975 int ret = 0;
1976
1977 while (1) {
1978 if ((q = strchr(p, ';')))
1979 *q = '\0';
1980 if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
1981 ret = tmp;
1982#ifdef ISDN_DEBUG_MODEM_ICALL
1983 printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
1984 p, isdn_map_eaz2msn(emu->msn, di), tmp);
1985#endif
1986 if (q) {
1987 *q = ';';
1988 p = q;
1989 p++;
1990 }
1991 if (!tmp)
1992 return 0;
1993 if (!q)
1994 break;
1995 }
1996 return ret;
1997 } else {
1998 int tmp;
1999 tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
2000#ifdef ISDN_DEBUG_MODEM_ICALL
Joe Perches475be4d2012-02-19 19:52:38 -08002001 printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
2002 isdn_map_eaz2msn(emu->msn, di), tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003#endif
2004 return tmp;
2005 }
2006}
2007
2008/*
2009 * An incoming call-request has arrived.
2010 * Search the tty-devices for an appropriate device and bind
2011 * it to the ISDN-Channel.
2012 * Return:
2013 *
2014 * 0 = No matching device found.
2015 * 1 = A matching device found.
2016 * 3 = No match found, but eventually would match, if
2017 * CID is longer.
2018 */
2019int
2020isdn_tty_find_icall(int di, int ch, setup_parm *setup)
2021{
2022 char *eaz;
2023 int i;
2024 int wret;
2025 int idx;
2026 int si1;
2027 int si2;
2028 char *nr;
2029 ulong flags;
2030
2031 if (!setup->phone[0]) {
2032 nr = "0";
2033 printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
2034 } else
2035 nr = setup->phone;
2036 si1 = (int) setup->si1;
2037 si2 = (int) setup->si2;
2038 if (!setup->eazmsn[0]) {
2039 printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
2040 eaz = "0";
2041 } else
2042 eaz = setup->eazmsn;
2043#ifdef ISDN_DEBUG_MODEM_ICALL
2044 printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
2045#endif
2046 wret = 0;
2047 spin_lock_irqsave(&dev->lock, flags);
2048 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2049 modem_info *info = &dev->mdm.info[i];
2050
Joe Perches475be4d2012-02-19 19:52:38 -08002051 if (info->count == 0)
2052 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) && /* SI1 is matching */
2054 (info->emu.mdmreg[REG_SI2] == si2)) { /* SI2 is matching */
2055 idx = isdn_dc2minor(di, ch);
2056#ifdef ISDN_DEBUG_MODEM_ICALL
2057 printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
2058 printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
2059 info->flags, info->isdn_driver, info->isdn_channel,
2060 dev->usage[idx]);
2061#endif
2062 if (
2063#ifndef FIX_FILE_TRANSFER
2064 (info->flags & ISDN_ASYNC_NORMAL_ACTIVE) &&
2065#endif
2066 (info->isdn_driver == -1) &&
2067 (info->isdn_channel == -1) &&
2068 (USG_NONE(dev->usage[idx]))) {
2069 int matchret;
2070
2071 if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
2072 wret = matchret;
2073 if (!matchret) { /* EAZ is matching */
2074 info->isdn_driver = di;
2075 info->isdn_channel = ch;
2076 info->drv_index = idx;
2077 dev->m_idx[idx] = info->line;
2078 dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
Joe Perches475be4d2012-02-19 19:52:38 -08002079 dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 strcpy(dev->num[idx], nr);
2081 strcpy(info->emu.cpn, eaz);
2082 info->emu.mdmreg[REG_SI1I] = si2bit[si1];
2083 info->emu.mdmreg[REG_PLAN] = setup->plan;
2084 info->emu.mdmreg[REG_SCREEN] = setup->screen;
2085 isdn_info_update();
2086 spin_unlock_irqrestore(&dev->lock, flags);
2087 printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2088 info->line);
2089 info->msr |= UART_MSR_RI;
2090 isdn_tty_modem_result(RESULT_RING, info);
2091 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2092 return 1;
2093 }
2094 }
2095 }
2096 }
2097 spin_unlock_irqrestore(&dev->lock, flags);
2098 printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
Joe Perches475be4d2012-02-19 19:52:38 -08002099 ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
2100 return (wret == 2) ? 3 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Jiri Slaby05eb48b2012-04-02 13:53:49 +02002103#define TTY_IS_ACTIVE(info) (info->flags & ISDN_ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105int
2106isdn_tty_stat_callback(int i, isdn_ctrl *c)
2107{
2108 int mi;
2109 modem_info *info;
2110 char *e;
2111
2112 if (i < 0)
2113 return 0;
2114 if ((mi = dev->m_idx[i]) >= 0) {
2115 info = &dev->mdm.info[mi];
2116 switch (c->command) {
Joe Perches475be4d2012-02-19 19:52:38 -08002117 case ISDN_STAT_CINF:
2118 printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2119 info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2120 if (e == (char *)c->parm.num)
2121 info->emu.charge = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Joe Perches475be4d2012-02-19 19:52:38 -08002123 break;
2124 case ISDN_STAT_BSENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125#ifdef ISDN_TTY_STAT_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08002126 printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002128 if ((info->isdn_driver == c->driver) &&
2129 (info->isdn_channel == c->arg)) {
2130 info->msr |= UART_MSR_CTS;
2131 if (info->send_outstanding)
2132 if (!(--info->send_outstanding))
2133 info->lsr |= UART_LSR_TEMT;
2134 isdn_tty_tint(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return 1;
Joe Perches475be4d2012-02-19 19:52:38 -08002136 }
2137 break;
2138 case ISDN_STAT_CAUSE:
2139#ifdef ISDN_TTY_STAT_DEBUG
2140 printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2141#endif
2142 /* Signal cause to tty-device */
2143 strncpy(info->last_cause, c->parm.num, 5);
2144 return 1;
2145 case ISDN_STAT_DISPLAY:
2146#ifdef ISDN_TTY_STAT_DEBUG
2147 printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2148#endif
2149 /* Signal display to tty-device */
2150 if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2151 !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2152 isdn_tty_at_cout("\r\n", info);
2153 isdn_tty_at_cout("DISPLAY: ", info);
2154 isdn_tty_at_cout(c->parm.display, info);
2155 isdn_tty_at_cout("\r\n", info);
2156 }
2157 return 1;
2158 case ISDN_STAT_DCONN:
2159#ifdef ISDN_TTY_STAT_DEBUG
2160 printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2161#endif
2162 if (TTY_IS_ACTIVE(info)) {
2163 if (info->dialing == 1) {
2164 info->dialing = 2;
2165 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Joe Perches475be4d2012-02-19 19:52:38 -08002167 }
2168 break;
2169 case ISDN_STAT_DHUP:
2170#ifdef ISDN_TTY_STAT_DEBUG
2171 printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2172#endif
2173 if (TTY_IS_ACTIVE(info)) {
2174 if (info->dialing == 1)
2175 isdn_tty_modem_result(RESULT_BUSY, info);
2176 if (info->dialing > 1)
2177 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2178 info->dialing = 0;
2179#ifdef ISDN_DEBUG_MODEM_HUP
2180 printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2181#endif
2182 isdn_tty_modem_hup(info, 0);
2183 return 1;
2184 }
2185 break;
2186 case ISDN_STAT_BCONN:
2187#ifdef ISDN_TTY_STAT_DEBUG
2188 printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2189#endif
2190 /* Wake up any processes waiting
2191 * for incoming call of this device when
2192 * DCD follow the state of incoming carrier
2193 */
2194 if (info->blocked_open &&
2195 (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
2196 wake_up_interruptible(&info->open_wait);
2197 }
2198
2199 /* Schedule CONNECT-Message to any tty
2200 * waiting for it and
2201 * set DCD-bit of its modem-status.
2202 */
2203 if (TTY_IS_ACTIVE(info) ||
2204 (info->blocked_open && (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
2205 info->msr |= UART_MSR_DCD;
2206 info->emu.charge = 0;
2207 if (info->dialing & 0xf)
2208 info->last_dir = 1;
2209 else
2210 info->last_dir = 0;
2211 info->dialing = 0;
2212 info->rcvsched = 1;
2213 if (USG_MODEM(dev->usage[i])) {
2214 if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2215 strcpy(info->emu.connmsg, c->parm.num);
2216 isdn_tty_modem_result(RESULT_CONNECT, info);
2217 } else
2218 isdn_tty_modem_result(RESULT_CONNECT64000, info);
2219 }
2220 if (USG_VOICE(dev->usage[i]))
2221 isdn_tty_modem_result(RESULT_VCON, info);
2222 return 1;
2223 }
2224 break;
2225 case ISDN_STAT_BHUP:
2226#ifdef ISDN_TTY_STAT_DEBUG
2227 printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2228#endif
2229 if (TTY_IS_ACTIVE(info)) {
2230#ifdef ISDN_DEBUG_MODEM_HUP
2231 printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2232#endif
2233 isdn_tty_modem_hup(info, 0);
2234 return 1;
2235 }
2236 break;
2237 case ISDN_STAT_NODCH:
2238#ifdef ISDN_TTY_STAT_DEBUG
2239 printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2240#endif
2241 if (TTY_IS_ACTIVE(info)) {
2242 if (info->dialing) {
2243 info->dialing = 0;
2244 info->last_l2 = -1;
2245 info->last_si = 0;
2246 sprintf(info->last_cause, "0000");
2247 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2248 }
2249 isdn_tty_modem_hup(info, 0);
2250 return 1;
2251 }
2252 break;
2253 case ISDN_STAT_UNLOAD:
2254#ifdef ISDN_TTY_STAT_DEBUG
2255 printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2256#endif
2257 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2258 info = &dev->mdm.info[i];
2259 if (info->isdn_driver == c->driver) {
2260 if (info->online)
2261 isdn_tty_modem_hup(info, 1);
2262 }
2263 }
2264 return 1;
2265#ifdef CONFIG_ISDN_TTY_FAX
2266 case ISDN_STAT_FAXIND:
2267 if (TTY_IS_ACTIVE(info)) {
2268 isdn_tty_fax_command(info, c);
2269 }
2270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271#endif
2272#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002273 case ISDN_STAT_AUDIO:
2274 if (TTY_IS_ACTIVE(info)) {
2275 switch (c->parm.num[0]) {
2276 case ISDN_AUDIO_DTMF:
2277 if (info->vonline) {
2278 isdn_audio_put_dle_code(info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 c->parm.num[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
Joe Perches475be4d2012-02-19 19:52:38 -08002281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Joe Perches475be4d2012-02-19 19:52:38 -08002283 }
2284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285#endif
2286 }
2287 }
2288 return 0;
2289}
2290
2291/*********************************************************************
2292 Modem-Emulator-Routines
Joe Perches475be4d2012-02-19 19:52:38 -08002293*********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Joe Perches475be4d2012-02-19 19:52:38 -08002295#define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297/*
2298 * Put a message from the AT-emulator into receive-buffer of tty,
2299 * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2300 */
2301void
Joe Perches475be4d2012-02-19 19:52:38 -08002302isdn_tty_at_cout(char *msg, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 struct tty_struct *tty;
2305 atemu *m = &info->emu;
2306 char *p;
2307 char c;
2308 u_long flags;
2309 struct sk_buff *skb = NULL;
2310 char *sp = NULL;
Adrian Bunk1f4d4a82006-03-25 03:08:06 -08002311 int l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (!msg) {
2314 printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2315 return;
2316 }
Adrian Bunk1f4d4a82006-03-25 03:08:06 -08002317
2318 l = strlen(msg);
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 spin_lock_irqsave(&info->readlock, flags);
2321 tty = info->tty;
2322 if ((info->flags & ISDN_ASYNC_CLOSING) || (!tty)) {
2323 spin_unlock_irqrestore(&info->readlock, flags);
2324 return;
2325 }
2326
Alan Cox33f0f882006-01-09 20:54:13 -08002327 /* use queue instead of direct, if online and */
2328 /* data is in queue or buffer is full */
Karsten Keil61b9a262006-02-14 13:53:06 -08002329 if (info->online && ((tty_buffer_request_room(tty, l) < l) ||
Joe Perches475be4d2012-02-19 19:52:38 -08002330 !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
Alan Cox33f0f882006-01-09 20:54:13 -08002331 skb = alloc_skb(l, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (!skb) {
2333 spin_unlock_irqrestore(&info->readlock, flags);
2334 return;
2335 }
Alan Cox33f0f882006-01-09 20:54:13 -08002336 sp = skb_put(skb, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337#ifdef CONFIG_ISDN_AUDIO
2338 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2339 ISDN_AUDIO_SKB_LOCK(skb) = 0;
2340#endif
2341 }
2342
2343 for (p = msg; *p; p++) {
2344 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -08002345 case '\r':
2346 c = m->mdmreg[REG_CR];
2347 break;
2348 case '\n':
2349 c = m->mdmreg[REG_LF];
2350 break;
2351 case '\b':
2352 c = m->mdmreg[REG_BS];
2353 break;
2354 default:
2355 c = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357 if (skb) {
2358 *sp++ = c;
2359 } else {
Joe Perches475be4d2012-02-19 19:52:38 -08002360 if (tty_insert_flip_char(tty, c, TTY_NORMAL) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363 }
2364 if (skb) {
2365 __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2366 dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2367 spin_unlock_irqrestore(&info->readlock, flags);
2368 /* Schedule dequeuing */
Alan Cox33f0f882006-01-09 20:54:13 -08002369 if (dev->modempoll && info->rcvsched)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2371
2372 } else {
2373 spin_unlock_irqrestore(&info->readlock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -08002374 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376}
2377
2378/*
2379 * Perform ATH Hangup
2380 */
2381static void
Joe Perches475be4d2012-02-19 19:52:38 -08002382isdn_tty_on_hook(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 if (info->isdn_channel >= 0) {
2385#ifdef ISDN_DEBUG_MODEM_HUP
2386 printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2387#endif
2388 isdn_tty_modem_hup(info, 1);
2389 }
2390}
2391
2392static void
2393isdn_tty_off_hook(void)
2394{
2395 printk(KERN_DEBUG "isdn_tty_off_hook\n");
2396}
2397
Joe Perches475be4d2012-02-19 19:52:38 -08002398#define PLUSWAIT1 (HZ / 2) /* 0.5 sec. */
2399#define PLUSWAIT2 (HZ * 3 / 2) /* 1.5 sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401/*
2402 * Check Buffer for Modem-escape-sequence, activate timer-callback to
2403 * isdn_tty_modem_escape() if sequence found.
2404 *
2405 * Parameters:
2406 * p pointer to databuffer
2407 * plus escape-character
2408 * count length of buffer
2409 * pluscount count of valid escape-characters so far
2410 * lastplus timestamp of last character
2411 */
2412static void
Joe Perches475be4d2012-02-19 19:52:38 -08002413isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 u_long *lastplus)
2415{
2416 if (plus > 127)
2417 return;
2418 if (count > 3) {
2419 p += count - 3;
2420 count = 3;
2421 *pluscount = 0;
2422 }
2423 while (count > 0) {
2424 if (*(p++) == plus) {
2425 if ((*pluscount)++) {
2426 /* Time since last '+' > 0.5 sec. ? */
2427 if (time_after(jiffies, *lastplus + PLUSWAIT1))
2428 *pluscount = 1;
2429 } else {
2430 /* Time since last non-'+' < 1.5 sec. ? */
2431 if (time_before(jiffies, *lastplus + PLUSWAIT2))
2432 *pluscount = 0;
2433 }
2434 if ((*pluscount == 3) && (count == 1))
2435 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2436 if (*pluscount > 3)
2437 *pluscount = 1;
2438 } else
2439 *pluscount = 0;
2440 *lastplus = jiffies;
2441 count--;
2442 }
2443}
2444
2445/*
2446 * Return result of AT-emulator to tty-receive-buffer, depending on
2447 * modem-register 12, bit 0 and 1.
2448 * For CONNECT-messages also switch to online-mode.
2449 * For RING-message handle auto-ATA if register 0 != 0
2450 */
2451
2452static void
Joe Perches475be4d2012-02-19 19:52:38 -08002453isdn_tty_modem_result(int code, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 atemu *m = &info->emu;
2456 static char *msg[] =
Joe Perches475be4d2012-02-19 19:52:38 -08002457 {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2458 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2459 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2460 char s[ISDN_MSNLEN + 10];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 switch (code) {
Joe Perches475be4d2012-02-19 19:52:38 -08002463 case RESULT_RING:
2464 m->mdmreg[REG_RINGCNT]++;
2465 if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2466 /* Automatically accept incoming call */
2467 isdn_tty_cmd_ATA(info);
2468 break;
2469 case RESULT_NO_CARRIER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470#ifdef ISDN_DEBUG_MODEM_HUP
Joe Perches475be4d2012-02-19 19:52:38 -08002471 printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
2472 (info->flags & ISDN_ASYNC_CLOSING),
2473 (!info->tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002475 m->mdmreg[REG_RINGCNT] = 0;
2476 del_timer(&info->nc_timer);
2477 info->ncarrier = 0;
2478 if ((info->flags & ISDN_ASYNC_CLOSING) || (!info->tty)) {
2479 return;
2480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002482 if (info->vonline & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483#ifdef ISDN_DEBUG_MODEM_VOICE
Joe Perches475be4d2012-02-19 19:52:38 -08002484 printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 info->line);
2486#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002487 /* voice-recording, add DLE-ETX */
2488 isdn_tty_at_cout("\020\003", info);
2489 }
2490 if (info->vonline & 2) {
2491#ifdef ISDN_DEBUG_MODEM_VOICE
2492 printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
2493 info->line);
2494#endif
2495 /* voice-playing, add DLE-DC4 */
2496 isdn_tty_at_cout("\020\024", info);
2497 }
2498#endif
2499 break;
2500 case RESULT_CONNECT:
2501 case RESULT_CONNECT64000:
2502 sprintf(info->last_cause, "0000");
2503 if (!info->online)
2504 info->online = 2;
2505 break;
2506 case RESULT_VCON:
2507#ifdef ISDN_DEBUG_MODEM_VOICE
2508 printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
2509 info->line);
2510#endif
2511 sprintf(info->last_cause, "0000");
2512 if (!info->online)
2513 info->online = 1;
2514 break;
2515 } /* switch (code) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 if (m->mdmreg[REG_RESP] & BIT_RESP) {
2518 /* Show results */
2519 if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
2520 /* Show numeric results only */
2521 sprintf(s, "\r\n%d\r\n", code);
2522 isdn_tty_at_cout(s, info);
2523 } else {
2524 if (code == RESULT_RING) {
Joe Perches475be4d2012-02-19 19:52:38 -08002525 /* return if "show RUNG" and ringcounter>1 */
2526 if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 (m->mdmreg[REG_RINGCNT] > 1))
Joe Perches475be4d2012-02-19 19:52:38 -08002528 return;
2529 /* print CID, _before_ _every_ ring */
2530 if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
2531 isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
2532 isdn_tty_at_cout(dev->num[info->drv_index], info);
2533 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2534 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2535 isdn_tty_at_cout(info->emu.cpn, info);
2536 }
2537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539 isdn_tty_at_cout("\r\n", info);
2540 isdn_tty_at_cout(msg[code], info);
2541 switch (code) {
Joe Perches475be4d2012-02-19 19:52:38 -08002542 case RESULT_CONNECT:
2543 switch (m->mdmreg[REG_L2PROT]) {
2544 case ISDN_PROTO_L2_MODEM:
2545 isdn_tty_at_cout(" ", info);
2546 isdn_tty_at_cout(m->connmsg, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002548 }
2549 break;
2550 case RESULT_RING:
2551 /* Append CPN, if enabled */
2552 if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
2553 sprintf(s, "/%s", m->cpn);
2554 isdn_tty_at_cout(s, info);
2555 }
2556 /* Print CID only once, _after_ 1st RING */
2557 if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
2558 (m->mdmreg[REG_RINGCNT] == 1)) {
2559 isdn_tty_at_cout("\r\n", info);
2560 isdn_tty_at_cout("CALLER NUMBER: ", info);
2561 isdn_tty_at_cout(dev->num[info->drv_index], info);
2562 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2563 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2564 isdn_tty_at_cout(info->emu.cpn, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
Joe Perches475be4d2012-02-19 19:52:38 -08002566 }
2567 break;
2568 case RESULT_NO_CARRIER:
2569 case RESULT_NO_DIALTONE:
2570 case RESULT_BUSY:
2571 case RESULT_NO_ANSWER:
2572 m->mdmreg[REG_RINGCNT] = 0;
2573 /* Append Cause-Message if enabled */
2574 if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
2575 sprintf(s, "/%s", info->last_cause);
2576 isdn_tty_at_cout(s, info);
2577 }
2578 break;
2579 case RESULT_CONNECT64000:
2580 /* Append Protocol to CONNECT message */
2581 switch (m->mdmreg[REG_L2PROT]) {
2582 case ISDN_PROTO_L2_X75I:
2583 case ISDN_PROTO_L2_X75UI:
2584 case ISDN_PROTO_L2_X75BUI:
2585 isdn_tty_at_cout("/X.75", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002587 case ISDN_PROTO_L2_HDLC:
2588 isdn_tty_at_cout("/HDLC", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002590 case ISDN_PROTO_L2_V11096:
2591 isdn_tty_at_cout("/V110/9600", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002593 case ISDN_PROTO_L2_V11019:
2594 isdn_tty_at_cout("/V110/19200", info);
2595 break;
2596 case ISDN_PROTO_L2_V11038:
2597 isdn_tty_at_cout("/V110/38400", info);
2598 break;
2599 }
2600 if (m->mdmreg[REG_T70] & BIT_T70) {
2601 isdn_tty_at_cout("/T.70", info);
2602 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2603 isdn_tty_at_cout("+", info);
2604 }
2605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607 isdn_tty_at_cout("\r\n", info);
2608 }
2609 }
2610 if (code == RESULT_NO_CARRIER) {
2611 if ((info->flags & ISDN_ASYNC_CLOSING) || (!info->tty)) {
2612 return;
2613 }
Jiri Slaby05eb48b2012-04-02 13:53:49 +02002614 if (info->flags & ISDN_ASYNC_CHECK_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 tty_hangup(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617}
2618
2619
2620/*
2621 * Display a modem-register-value.
2622 */
2623static void
Joe Perches475be4d2012-02-19 19:52:38 -08002624isdn_tty_show_profile(int ridx, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 char v[6];
2627
2628 sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
2629 isdn_tty_at_cout(v, info);
2630}
2631
2632/*
2633 * Get MSN-string from char-pointer, set pointer to end of number
2634 */
2635static void
2636isdn_tty_get_msnstr(char *n, char **p)
2637{
2638 int limit = ISDN_MSNLEN - 1;
2639
2640 while (((*p[0] >= '0' && *p[0] <= '9') ||
2641 /* Why a comma ??? */
2642 (*p[0] == ',') || (*p[0] == ':')) &&
Joe Perches475be4d2012-02-19 19:52:38 -08002643 (limit--))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 *n++ = *p[0]++;
2645 *n = '\0';
2646}
2647
2648/*
2649 * Get phone-number from modem-commandbuffer
2650 */
2651static void
Joe Perches475be4d2012-02-19 19:52:38 -08002652isdn_tty_getdial(char *p, char *q, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 int first = 1;
2655 int limit = ISDN_MSNLEN - 1; /* MUST match the size of interface var to avoid
Joe Perches475be4d2012-02-19 19:52:38 -08002656 buffer overflow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Joe Perches475be4d2012-02-19 19:52:38 -08002658 while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
Karsten Keil924ad152007-06-01 00:46:55 -07002660 ((*p == 'R') && first) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 (*p == '*') || (*p == '#')) {
2662 *q++ = *p;
2663 limit--;
2664 }
Joe Perches475be4d2012-02-19 19:52:38 -08002665 if (!limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 break;
2667 p++;
2668 first = 0;
2669 }
2670 *q = 0;
2671}
2672
2673#define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
2674#define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
2675
2676static void
Joe Perches475be4d2012-02-19 19:52:38 -08002677isdn_tty_report(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
2679 atemu *m = &info->emu;
2680 char s[80];
2681
2682 isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
2683 sprintf(s, " Remote Number: %s\r\n", info->last_num);
2684 isdn_tty_at_cout(s, info);
2685 sprintf(s, " Direction: %s\r\n", info->last_dir ? "outgoing" : "incoming");
2686 isdn_tty_at_cout(s, info);
2687 isdn_tty_at_cout(" Layer-2 Protocol: ", info);
2688 switch (info->last_l2) {
Joe Perches475be4d2012-02-19 19:52:38 -08002689 case ISDN_PROTO_L2_X75I:
2690 isdn_tty_at_cout("X.75i", info);
2691 break;
2692 case ISDN_PROTO_L2_X75UI:
2693 isdn_tty_at_cout("X.75ui", info);
2694 break;
2695 case ISDN_PROTO_L2_X75BUI:
2696 isdn_tty_at_cout("X.75bui", info);
2697 break;
2698 case ISDN_PROTO_L2_HDLC:
2699 isdn_tty_at_cout("HDLC", info);
2700 break;
2701 case ISDN_PROTO_L2_V11096:
2702 isdn_tty_at_cout("V.110 9600 Baud", info);
2703 break;
2704 case ISDN_PROTO_L2_V11019:
2705 isdn_tty_at_cout("V.110 19200 Baud", info);
2706 break;
2707 case ISDN_PROTO_L2_V11038:
2708 isdn_tty_at_cout("V.110 38400 Baud", info);
2709 break;
2710 case ISDN_PROTO_L2_TRANS:
2711 isdn_tty_at_cout("transparent", info);
2712 break;
2713 case ISDN_PROTO_L2_MODEM:
2714 isdn_tty_at_cout("modem", info);
2715 break;
2716 case ISDN_PROTO_L2_FAX:
2717 isdn_tty_at_cout("fax", info);
2718 break;
2719 default:
2720 isdn_tty_at_cout("unknown", info);
2721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723 if (m->mdmreg[REG_T70] & BIT_T70) {
2724 isdn_tty_at_cout("/T.70", info);
2725 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2726 isdn_tty_at_cout("+", info);
2727 }
2728 isdn_tty_at_cout("\r\n", info);
2729 isdn_tty_at_cout(" Service: ", info);
2730 switch (info->last_si) {
Joe Perches475be4d2012-02-19 19:52:38 -08002731 case 1:
2732 isdn_tty_at_cout("audio\r\n", info);
2733 break;
2734 case 5:
2735 isdn_tty_at_cout("btx\r\n", info);
2736 break;
2737 case 7:
2738 isdn_tty_at_cout("data\r\n", info);
2739 break;
2740 default:
2741 sprintf(s, "%d\r\n", info->last_si);
2742 isdn_tty_at_cout(s, info);
2743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745 sprintf(s, " Hangup location: %s\r\n", info->last_lhup ? "local" : "remote");
2746 isdn_tty_at_cout(s, info);
2747 sprintf(s, " Last cause: %s\r\n", info->last_cause);
2748 isdn_tty_at_cout(s, info);
2749}
2750
2751/*
2752 * Parse AT&.. commands.
2753 */
2754static int
Joe Perches475be4d2012-02-19 19:52:38 -08002755isdn_tty_cmd_ATand(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 atemu *m = &info->emu;
2758 int i;
2759 char rb[100];
2760
2761#define MAXRB (sizeof(rb) - 1)
2762
2763 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08002764 case 'B':
2765 /* &B - Set Buffersize */
2766 p[0]++;
2767 i = isdn_getnum(p);
2768 if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
2769 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002771 if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
2772 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002774 m->mdmreg[REG_PSIZE] = i / 16;
2775 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2776 switch (m->mdmreg[REG_L2PROT]) {
2777 case ISDN_PROTO_L2_V11096:
2778 case ISDN_PROTO_L2_V11019:
2779 case ISDN_PROTO_L2_V11038:
2780 info->xmit_size /= 10;
2781 }
2782 break;
2783 case 'C':
2784 /* &C - DCD Status */
2785 p[0]++;
2786 switch (isdn_getnum(p)) {
2787 case 0:
2788 m->mdmreg[REG_DCD] &= ~BIT_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002790 case 1:
2791 m->mdmreg[REG_DCD] |= BIT_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002793 default:
2794 PARSE_ERROR1
2795 }
2796 break;
2797 case 'D':
2798 /* &D - Set DTR-Low-behavior */
2799 p[0]++;
2800 switch (isdn_getnum(p)) {
2801 case 0:
2802 m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
2803 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002805 case 2:
2806 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2807 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002809 case 3:
2810 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2811 m->mdmreg[REG_DTRR] |= BIT_DTRR;
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 'E':
2818 /* &E -Set EAZ/MSN */
2819 p[0]++;
2820 isdn_tty_get_msnstr(m->msn, p);
2821 break;
2822 case 'F':
2823 /* &F -Set Factory-Defaults */
2824 p[0]++;
2825 if (info->msr & UART_MSR_DCD)
2826 PARSE_ERROR1;
2827 isdn_tty_reset_profile(m);
2828 isdn_tty_modem_reset_regs(info, 1);
2829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830#ifdef DUMMY_HAYES_AT
Joe Perches475be4d2012-02-19 19:52:38 -08002831 case 'K':
2832 /* only for be compilant with common scripts */
2833 /* &K Flowcontrol - no function */
2834 p[0]++;
2835 isdn_getnum(p);
2836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002838 case 'L':
2839 /* &L -Set Numbers to listen on */
2840 p[0]++;
2841 i = 0;
2842 while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
2843 (i < ISDN_LMSNLEN - 1))
2844 m->lmsn[i++] = *p[0]++;
2845 m->lmsn[i] = '\0';
2846 break;
2847 case 'R':
2848 /* &R - Set V.110 bitrate adaption */
2849 p[0]++;
2850 i = isdn_getnum(p);
2851 switch (i) {
2852 case 0:
2853 /* Switch off V.110, back to X.75 */
2854 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2855 m->mdmreg[REG_SI2] = 0;
2856 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002858 case 9600:
2859 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
2860 m->mdmreg[REG_SI2] = 197;
2861 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002863 case 19200:
2864 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
2865 m->mdmreg[REG_SI2] = 199;
2866 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002868 case 38400:
2869 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
2870 m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
2871 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 break;
2873 default:
2874 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08002875 }
2876 /* Switch off T.70 */
2877 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2878 /* Set Service 7 */
2879 m->mdmreg[REG_SI1] |= 4;
2880 break;
2881 case 'S':
2882 /* &S - Set Windowsize */
2883 p[0]++;
2884 i = isdn_getnum(p);
2885 if ((i > 0) && (i < 9))
2886 m->mdmreg[REG_WSIZE] = i;
2887 else
2888 PARSE_ERROR1;
2889 break;
2890 case 'V':
2891 /* &V - Show registers */
2892 p[0]++;
2893 isdn_tty_at_cout("\r\n", info);
2894 for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
2895 sprintf(rb, "S%02d=%03d%s", i,
2896 m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
2897 isdn_tty_at_cout(rb, info);
2898 }
2899 sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
2900 strlen(m->msn) ? m->msn : "None");
2901 isdn_tty_at_cout(rb, info);
2902 if (strlen(m->lmsn)) {
2903 isdn_tty_at_cout("\r\nListen: ", info);
2904 isdn_tty_at_cout(m->lmsn, info);
2905 isdn_tty_at_cout("\r\n", info);
2906 }
2907 break;
2908 case 'W':
2909 /* &W - Write Profile */
2910 p[0]++;
2911 switch (*p[0]) {
2912 case '0':
2913 p[0]++;
2914 modem_write_profile(m);
2915 break;
2916 default:
2917 PARSE_ERROR1;
2918 }
2919 break;
2920 case 'X':
2921 /* &X - Switch to BTX-Mode and T.70 */
2922 p[0]++;
2923 switch (isdn_getnum(p)) {
2924 case 0:
2925 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2926 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2927 break;
2928 case 1:
2929 m->mdmreg[REG_T70] |= BIT_T70;
2930 m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
2931 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2932 info->xmit_size = 112;
2933 m->mdmreg[REG_SI1] = 4;
2934 m->mdmreg[REG_SI2] = 0;
2935 break;
2936 case 2:
2937 m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
2938 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2939 info->xmit_size = 112;
2940 m->mdmreg[REG_SI1] = 4;
2941 m->mdmreg[REG_SI2] = 0;
2942 break;
2943 default:
2944 PARSE_ERROR1;
2945 }
2946 break;
2947 default:
2948 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 return 0;
2951}
2952
2953static int
Joe Perches475be4d2012-02-19 19:52:38 -08002954isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 /* Some plausibility checks */
2957 switch (mreg) {
Joe Perches475be4d2012-02-19 19:52:38 -08002958 case REG_L2PROT:
2959 if (mval > ISDN_PROTO_L2_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return 1;
Joe Perches475be4d2012-02-19 19:52:38 -08002961 break;
2962 case REG_PSIZE:
2963 if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
2964 return 1;
2965#ifdef CONFIG_ISDN_AUDIO
2966 if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
2967 return 1;
2968#endif
2969 info->xmit_size = mval * 16;
2970 switch (m->mdmreg[REG_L2PROT]) {
2971 case ISDN_PROTO_L2_V11096:
2972 case ISDN_PROTO_L2_V11019:
2973 case ISDN_PROTO_L2_V11038:
2974 info->xmit_size /= 10;
2975 }
2976 break;
2977 case REG_SI1I:
2978 case REG_PLAN:
2979 case REG_SCREEN:
2980 /* readonly registers */
2981 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
2983 return 0;
2984}
2985
2986/*
2987 * Perform ATS command
2988 */
2989static int
Joe Perches475be4d2012-02-19 19:52:38 -08002990isdn_tty_cmd_ATS(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
2992 atemu *m = &info->emu;
2993 int bitpos;
2994 int mreg;
2995 int mval;
2996 int bval;
2997
2998 mreg = isdn_getnum(p);
2999 if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
3000 PARSE_ERROR1;
3001 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003002 case '=':
3003 p[0]++;
3004 mval = isdn_getnum(p);
3005 if (mval < 0 || mval > 255)
3006 PARSE_ERROR1;
3007 if (isdn_tty_check_ats(mreg, mval, info, m))
3008 PARSE_ERROR1;
3009 m->mdmreg[mreg] = mval;
3010 break;
3011 case '.':
3012 /* Set/Clear a single bit */
3013 p[0]++;
3014 bitpos = isdn_getnum(p);
3015 if ((bitpos < 0) || (bitpos > 7))
3016 PARSE_ERROR1;
3017 switch (*p[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 case '=':
3019 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08003020 bval = isdn_getnum(p);
3021 if (bval < 0 || bval > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003023 if (bval)
3024 mval = m->mdmreg[mreg] | (1 << bitpos);
3025 else
3026 mval = m->mdmreg[mreg] & ~(1 << bitpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (isdn_tty_check_ats(mreg, mval, info, m))
3028 PARSE_ERROR1;
3029 m->mdmreg[mreg] = mval;
3030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 case '?':
3032 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08003033 isdn_tty_at_cout("\r\n", info);
3034 isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
3035 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 break;
3037 default:
3038 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003039 }
3040 break;
3041 case '?':
3042 p[0]++;
3043 isdn_tty_show_profile(mreg, info);
3044 break;
3045 default:
3046 PARSE_ERROR1;
3047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049 return 0;
3050}
3051
3052/*
3053 * Perform ATA command
3054 */
3055static void
Joe Perches475be4d2012-02-19 19:52:38 -08003056isdn_tty_cmd_ATA(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 atemu *m = &info->emu;
3059 isdn_ctrl cmd;
3060 int l2;
3061
3062 if (info->msr & UART_MSR_RI) {
3063 /* Accept incoming call */
3064 info->last_dir = 0;
3065 strcpy(info->last_num, dev->num[info->drv_index]);
3066 m->mdmreg[REG_RINGCNT] = 0;
3067 info->msr &= ~UART_MSR_RI;
3068 l2 = m->mdmreg[REG_L2PROT];
3069#ifdef CONFIG_ISDN_AUDIO
3070 /* If more than one bit set in reg18, autoselect Layer2 */
3071 if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
3072 if (m->mdmreg[REG_SI1I] == 1) {
3073 if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
3074 l2 = ISDN_PROTO_L2_TRANS;
3075 } else
3076 l2 = ISDN_PROTO_L2_X75I;
3077 }
3078#endif
3079 cmd.driver = info->isdn_driver;
3080 cmd.command = ISDN_CMD_SETL2;
3081 cmd.arg = info->isdn_channel + (l2 << 8);
3082 info->last_l2 = l2;
3083 isdn_command(&cmd);
3084 cmd.driver = info->isdn_driver;
3085 cmd.command = ISDN_CMD_SETL3;
3086 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
3087#ifdef CONFIG_ISDN_TTY_FAX
3088 if (l2 == ISDN_PROTO_L2_FAX) {
3089 cmd.parm.fax = info->fax;
3090 info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3091 }
3092#endif
3093 isdn_command(&cmd);
3094 cmd.driver = info->isdn_driver;
3095 cmd.arg = info->isdn_channel;
3096 cmd.command = ISDN_CMD_ACCEPTD;
3097 info->dialing = 16;
3098 info->emu.carrierwait = 0;
3099 isdn_command(&cmd);
3100 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3101 } else
3102 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3103}
3104
3105#ifdef CONFIG_ISDN_AUDIO
3106/*
3107 * Parse AT+F.. commands
3108 */
3109static int
Joe Perches475be4d2012-02-19 19:52:38 -08003110isdn_tty_cmd_PLUSF(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 atemu *m = &info->emu;
3113 char rs[20];
3114
3115 if (!strncmp(p[0], "CLASS", 5)) {
3116 p[0] += 5;
3117 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003118 case '?':
3119 p[0]++;
3120 sprintf(rs, "\r\n%d",
3121 (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3122#ifdef CONFIG_ISDN_TTY_FAX
3123 if (TTY_IS_FCLASS2(info))
3124 sprintf(rs, "\r\n2");
3125 else if (TTY_IS_FCLASS1(info))
3126 sprintf(rs, "\r\n1");
3127#endif
3128 isdn_tty_at_cout(rs, info);
3129 break;
3130 case '=':
3131 p[0]++;
3132 switch (*p[0]) {
3133 case '0':
3134 p[0]++;
3135 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3136 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3137 m->mdmreg[REG_SI1] = 4;
3138 info->xmit_size =
3139 m->mdmreg[REG_PSIZE] * 16;
3140 break;
3141#ifdef CONFIG_ISDN_TTY_FAX
3142 case '1':
3143 p[0]++;
3144 if (!(dev->global_features &
3145 ISDN_FEATURE_L3_FCLASS1))
3146 PARSE_ERROR1;
3147 m->mdmreg[REG_SI1] = 1;
3148 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3149 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3150 info->xmit_size =
3151 m->mdmreg[REG_PSIZE] * 16;
3152 break;
3153 case '2':
3154 p[0]++;
3155 if (!(dev->global_features &
3156 ISDN_FEATURE_L3_FCLASS2))
3157 PARSE_ERROR1;
3158 m->mdmreg[REG_SI1] = 1;
3159 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3160 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3161 info->xmit_size =
3162 m->mdmreg[REG_PSIZE] * 16;
3163 break;
3164#endif
3165 case '8':
3166 p[0]++;
3167 /* L2 will change on dialout with si=1 */
3168 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3169 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3170 m->mdmreg[REG_SI1] = 5;
3171 info->xmit_size = VBUF;
3172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 case '?':
3174 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08003175 strcpy(rs, "\r\n0,");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -08003177 if (dev->global_features &
3178 ISDN_FEATURE_L3_FCLASS1)
3179 strcat(rs, "1,");
3180 if (dev->global_features &
3181 ISDN_FEATURE_L3_FCLASS2)
3182 strcat(rs, "2,");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183#endif
Joe Perches475be4d2012-02-19 19:52:38 -08003184 strcat(rs, "8");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 isdn_tty_at_cout(rs, info);
3186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 default:
3188 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003189 }
3190 break;
3191 default:
3192 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194 return 0;
3195 }
3196#ifdef CONFIG_ISDN_TTY_FAX
3197 return (isdn_tty_cmd_PLUSF_FAX(p, info));
3198#else
3199 PARSE_ERROR1;
3200#endif
3201}
3202
3203/*
3204 * Parse AT+V.. commands
3205 */
3206static int
Joe Perches475be4d2012-02-19 19:52:38 -08003207isdn_tty_cmd_PLUSV(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 atemu *m = &info->emu;
3210 isdn_ctrl cmd;
3211 static char *vcmd[] =
Joe Perches475be4d2012-02-19 19:52:38 -08003212 {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 int i;
3214 int par1;
3215 int par2;
3216 char rs[20];
3217
3218 i = 0;
3219 while (vcmd[i]) {
3220 if (!strncmp(vcmd[i], p[0], 2)) {
3221 p[0] += 2;
3222 break;
3223 }
3224 i++;
3225 }
3226 switch (i) {
Joe Perches475be4d2012-02-19 19:52:38 -08003227 case 0:
3228 /* AT+VNH - Auto hangup feature */
3229 switch (*p[0]) {
3230 case '?':
3231 p[0]++;
3232 isdn_tty_at_cout("\r\n1", info);
3233 break;
3234 case '=':
3235 p[0]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003237 case '1':
3238 p[0]++;
3239 break;
3240 case '?':
3241 p[0]++;
3242 isdn_tty_at_cout("\r\n1", info);
3243 break;
3244 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
3247 break;
3248 default:
3249 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003250 }
3251 break;
3252 case 1:
3253 /* AT+VIP - Reset all voice parameters */
3254 isdn_tty_modem_reset_vpar(m);
3255 break;
3256 case 2:
3257 /* AT+VLS - Select device, accept incoming call */
3258 switch (*p[0]) {
3259 case '?':
3260 p[0]++;
3261 sprintf(rs, "\r\n%d", m->vpar[0]);
3262 isdn_tty_at_cout(rs, info);
3263 break;
3264 case '=':
3265 p[0]++;
3266 switch (*p[0]) {
3267 case '0':
3268 p[0]++;
3269 m->vpar[0] = 0;
3270 break;
3271 case '2':
3272 p[0]++;
3273 m->vpar[0] = 2;
3274 break;
3275 case '?':
3276 p[0]++;
3277 isdn_tty_at_cout("\r\n0,2", info);
3278 break;
3279 default:
3280 PARSE_ERROR1;
3281 }
3282 break;
3283 default:
3284 PARSE_ERROR1;
3285 }
3286 break;
3287 case 3:
3288 /* AT+VRX - Start recording */
3289 if (!m->vpar[0])
3290 PARSE_ERROR1;
3291 if (info->online != 1) {
3292 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3293 return 1;
3294 }
3295 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3296 if (!info->dtmf_state) {
3297 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3298 PARSE_ERROR1;
3299 }
3300 info->silence_state = isdn_audio_silence_init(info->silence_state);
3301 if (!info->silence_state) {
3302 printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3303 PARSE_ERROR1;
3304 }
3305 if (m->vpar[3] < 5) {
3306 info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3307 if (!info->adpcmr) {
3308 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3309 PARSE_ERROR1;
3310 }
3311 }
3312#ifdef ISDN_DEBUG_AT
3313 printk(KERN_DEBUG "AT: +VRX\n");
3314#endif
3315 info->vonline |= 1;
3316 isdn_tty_modem_result(RESULT_CONNECT, info);
3317 return 0;
3318 break;
3319 case 4:
3320 /* AT+VSD - Silence detection */
3321 switch (*p[0]) {
3322 case '?':
3323 p[0]++;
3324 sprintf(rs, "\r\n<%d>,<%d>",
3325 m->vpar[1],
3326 m->vpar[2]);
3327 isdn_tty_at_cout(rs, info);
3328 break;
3329 case '=':
3330 p[0]++;
3331 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3332 par1 = isdn_getnum(p);
3333 if ((par1 < 0) || (par1 > 31))
3334 PARSE_ERROR1;
3335 if (*p[0] != ',')
3336 PARSE_ERROR1;
3337 p[0]++;
3338 par2 = isdn_getnum(p);
3339 if ((par2 < 0) || (par2 > 255))
3340 PARSE_ERROR1;
3341 m->vpar[1] = par1;
3342 m->vpar[2] = par2;
3343 break;
3344 } else
3345 if (*p[0] == '?') {
3346 p[0]++;
3347 isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3348 info);
3349 break;
3350 } else
3351 PARSE_ERROR1;
3352 break;
3353 default:
3354 PARSE_ERROR1;
3355 }
3356 break;
3357 case 5:
3358 /* AT+VSM - Select compression */
3359 switch (*p[0]) {
3360 case '?':
3361 p[0]++;
3362 sprintf(rs, "\r\n<%d>,<%d><8000>",
3363 m->vpar[3],
3364 m->vpar[1]);
3365 isdn_tty_at_cout(rs, info);
3366 break;
3367 case '=':
3368 p[0]++;
3369 switch (*p[0]) {
3370 case '2':
3371 case '3':
3372 case '4':
3373 case '5':
3374 case '6':
3375 par1 = isdn_getnum(p);
3376 if ((par1 < 2) || (par1 > 6))
3377 PARSE_ERROR1;
3378 m->vpar[3] = par1;
3379 break;
3380 case '?':
3381 p[0]++;
3382 isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3383 info);
3384 isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3385 info);
3386 isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3387 info);
3388 isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3389 info);
3390 isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3391 info);
3392 break;
3393 default:
3394 PARSE_ERROR1;
3395 }
3396 break;
3397 default:
3398 PARSE_ERROR1;
3399 }
3400 break;
3401 case 6:
3402 /* AT+VTX - Start sending */
3403 if (!m->vpar[0])
3404 PARSE_ERROR1;
3405 if (info->online != 1) {
3406 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3407 return 1;
3408 }
3409 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3410 if (!info->dtmf_state) {
3411 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3412 PARSE_ERROR1;
3413 }
3414 if (m->vpar[3] < 5) {
3415 info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3416 if (!info->adpcms) {
3417 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3418 PARSE_ERROR1;
3419 }
3420 }
3421#ifdef ISDN_DEBUG_AT
3422 printk(KERN_DEBUG "AT: +VTX\n");
3423#endif
3424 m->lastDLE = 0;
3425 info->vonline |= 2;
3426 isdn_tty_modem_result(RESULT_CONNECT, info);
3427 return 0;
3428 break;
3429 case 7:
3430 /* AT+VDD - DTMF detection */
3431 switch (*p[0]) {
3432 case '?':
3433 p[0]++;
3434 sprintf(rs, "\r\n<%d>,<%d>",
3435 m->vpar[4],
3436 m->vpar[5]);
3437 isdn_tty_at_cout(rs, info);
3438 break;
3439 case '=':
3440 p[0]++;
3441 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3442 if (info->online != 1)
3443 PARSE_ERROR1;
3444 par1 = isdn_getnum(p);
3445 if ((par1 < 0) || (par1 > 15))
3446 PARSE_ERROR1;
3447 if (*p[0] != ',')
3448 PARSE_ERROR1;
3449 p[0]++;
3450 par2 = isdn_getnum(p);
3451 if ((par2 < 0) || (par2 > 255))
3452 PARSE_ERROR1;
3453 m->vpar[4] = par1;
3454 m->vpar[5] = par2;
3455 cmd.driver = info->isdn_driver;
3456 cmd.command = ISDN_CMD_AUDIO;
3457 cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3458 cmd.parm.num[0] = par1;
3459 cmd.parm.num[1] = par2;
3460 isdn_command(&cmd);
3461 break;
3462 } else
3463 if (*p[0] == '?') {
3464 p[0]++;
3465 isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3466 info);
3467 break;
3468 } else
3469 PARSE_ERROR1;
3470 break;
3471 default:
3472 PARSE_ERROR1;
3473 }
3474 break;
3475 default:
3476 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478 return 0;
3479}
3480#endif /* CONFIG_ISDN_AUDIO */
3481
3482/*
3483 * Parse and perform an AT-command-line.
3484 */
3485static void
Joe Perches475be4d2012-02-19 19:52:38 -08003486isdn_tty_parse_at(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
3488 atemu *m = &info->emu;
3489 char *p;
Dan Carpenterf417f5e2010-09-04 08:33:03 +00003490 char ds[ISDN_MSNLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492#ifdef ISDN_DEBUG_AT
3493 printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
3494#endif
3495 for (p = &m->mdmcmd[2]; *p;) {
3496 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -08003497 case ' ':
3498 p++;
3499 break;
3500 case 'A':
3501 /* A - Accept incoming call */
3502 p++;
3503 isdn_tty_cmd_ATA(info);
3504 return;
3505 break;
3506 case 'D':
3507 /* D - Dial */
3508 if (info->msr & UART_MSR_DCD)
3509 PARSE_ERROR;
3510 if (info->msr & UART_MSR_RI) {
3511 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 return;
Joe Perches475be4d2012-02-19 19:52:38 -08003513 }
3514 isdn_tty_getdial(++p, ds, sizeof ds);
3515 p += strlen(p);
3516 if (!strlen(m->msn))
3517 isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
3518 else if (strlen(ds))
3519 isdn_tty_dial(ds, info, m);
3520 else
3521 PARSE_ERROR;
3522 return;
3523 case 'E':
3524 /* E - Turn Echo on/off */
3525 p++;
3526 switch (isdn_getnum(&p)) {
3527 case 0:
3528 m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 break;
Joe Perches475be4d2012-02-19 19:52:38 -08003530 case 1:
3531 m->mdmreg[REG_ECHO] |= BIT_ECHO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 break;
3533 default:
3534 PARSE_ERROR;
Joe Perches475be4d2012-02-19 19:52:38 -08003535 }
3536 break;
3537 case 'H':
3538 /* H - On/Off-hook */
3539 p++;
3540 switch (*p) {
3541 case '0':
3542 p++;
3543 isdn_tty_on_hook(info);
3544 break;
3545 case '1':
3546 p++;
3547 isdn_tty_off_hook();
3548 break;
3549 default:
3550 isdn_tty_on_hook(info);
3551 break;
3552 }
3553 break;
3554 case 'I':
3555 /* I - Information */
3556 p++;
3557 isdn_tty_at_cout("\r\nLinux ISDN", info);
3558 switch (*p) {
3559 case '0':
3560 case '1':
3561 p++;
3562 break;
3563 case '2':
3564 p++;
3565 isdn_tty_report(info);
3566 break;
3567 case '3':
3568 p++;
3569 snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
3570 isdn_tty_at_cout(ds, info);
3571 break;
3572 default:;
3573 }
3574 break;
3575#ifdef DUMMY_HAYES_AT
3576 case 'L':
3577 case 'M':
3578 /* only for be compilant with common scripts */
3579 /* no function */
3580 p++;
3581 isdn_getnum(&p);
3582 break;
3583#endif
3584 case 'O':
3585 /* O - Go online */
3586 p++;
3587 if (info->msr & UART_MSR_DCD)
3588 /* if B-Channel is up */
3589 isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
3590 else
3591 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3592 return;
3593 case 'Q':
3594 /* Q - Turn Emulator messages on/off */
3595 p++;
3596 switch (isdn_getnum(&p)) {
3597 case 0:
3598 m->mdmreg[REG_RESP] |= BIT_RESP;
3599 break;
3600 case 1:
3601 m->mdmreg[REG_RESP] &= ~BIT_RESP;
3602 break;
3603 default:
3604 PARSE_ERROR;
3605 }
3606 break;
3607 case 'S':
3608 /* S - Set/Get Register */
3609 p++;
3610 if (isdn_tty_cmd_ATS(&p, info))
3611 return;
3612 break;
3613 case 'V':
3614 /* V - Numeric or ASCII Emulator-messages */
3615 p++;
3616 switch (isdn_getnum(&p)) {
3617 case 0:
3618 m->mdmreg[REG_RESP] |= BIT_RESPNUM;
3619 break;
3620 case 1:
3621 m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
3622 break;
3623 default:
3624 PARSE_ERROR;
3625 }
3626 break;
3627 case 'Z':
3628 /* Z - Load Registers from Profile */
3629 p++;
3630 if (info->msr & UART_MSR_DCD) {
3631 info->online = 0;
3632 isdn_tty_on_hook(info);
3633 }
3634 isdn_tty_modem_reset_regs(info, 1);
3635 break;
3636 case '+':
3637 p++;
3638 switch (*p) {
3639#ifdef CONFIG_ISDN_AUDIO
3640 case 'F':
3641 p++;
3642 if (isdn_tty_cmd_PLUSF(&p, info))
3643 return;
3644 break;
3645 case 'V':
3646 if ((!(m->mdmreg[REG_SI1] & 1)) ||
3647 (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
3648 PARSE_ERROR;
3649 p++;
3650 if (isdn_tty_cmd_PLUSV(&p, info))
3651 return;
3652 break;
3653#endif /* CONFIG_ISDN_AUDIO */
3654 case 'S': /* SUSPEND */
3655 p++;
3656 isdn_tty_get_msnstr(ds, &p);
3657 isdn_tty_suspend(ds, info, m);
3658 break;
3659 case 'R': /* RESUME */
3660 p++;
3661 isdn_tty_get_msnstr(ds, &p);
3662 isdn_tty_resume(ds, info, m);
3663 break;
3664 case 'M': /* MESSAGE */
3665 p++;
3666 isdn_tty_send_msg(info, m, p);
3667 break;
3668 default:
3669 PARSE_ERROR;
3670 }
3671 break;
3672 case '&':
3673 p++;
3674 if (isdn_tty_cmd_ATand(&p, info))
3675 return;
3676 break;
3677 default:
3678 PARSE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 }
3680 }
3681#ifdef CONFIG_ISDN_AUDIO
3682 if (!info->vonline)
3683#endif
3684 isdn_tty_modem_result(RESULT_OK, info);
3685}
3686
3687/* Need own toupper() because standard-toupper is not available
3688 * within modules.
3689 */
Joe Perches475be4d2012-02-19 19:52:38 -08003690#define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692/*
3693 * Perform line-editing of AT-commands
3694 *
3695 * Parameters:
3696 * p inputbuffer
3697 * count length of buffer
3698 * channel index to line (minor-device)
3699 */
3700static int
Joe Perches475be4d2012-02-19 19:52:38 -08003701isdn_tty_edit_at(const char *p, int count, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
3703 atemu *m = &info->emu;
3704 int total = 0;
3705 u_char c;
3706 char eb[2];
3707 int cnt;
3708
3709 for (cnt = count; cnt > 0; p++, cnt--) {
3710 c = *p;
3711 total++;
3712 if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
3713 /* Separator (CR or LF) */
3714 m->mdmcmd[m->mdmcmdl] = 0;
3715 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3716 eb[0] = c;
3717 eb[1] = 0;
3718 isdn_tty_at_cout(eb, info);
3719 }
3720 if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
3721 isdn_tty_parse_at(info);
3722 m->mdmcmdl = 0;
3723 continue;
3724 }
3725 if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
3726 /* Backspace-Function */
3727 if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
3728 if (m->mdmcmdl)
3729 m->mdmcmdl--;
3730 if (m->mdmreg[REG_ECHO] & BIT_ECHO)
3731 isdn_tty_at_cout("\b", info);
3732 }
3733 continue;
3734 }
3735 if (cmdchar(c)) {
3736 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3737 eb[0] = c;
3738 eb[1] = 0;
3739 isdn_tty_at_cout(eb, info);
3740 }
3741 if (m->mdmcmdl < 255) {
3742 c = my_toupper(c);
3743 switch (m->mdmcmdl) {
Joe Perches475be4d2012-02-19 19:52:38 -08003744 case 1:
3745 if (c == 'T') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 m->mdmcmd[m->mdmcmdl] = c;
3747 m->mdmcmd[++m->mdmcmdl] = 0;
Joe Perches475be4d2012-02-19 19:52:38 -08003748 break;
3749 } else
3750 m->mdmcmdl = 0;
3751 /* Fall through, check for 'A' */
3752 case 0:
3753 if (c == 'A') {
3754 m->mdmcmd[m->mdmcmdl] = c;
3755 m->mdmcmd[++m->mdmcmdl] = 0;
3756 }
3757 break;
3758 default:
3759 m->mdmcmd[m->mdmcmdl] = c;
3760 m->mdmcmd[++m->mdmcmdl] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
3762 }
3763 }
3764 }
3765 return total;
3766}
3767
3768/*
3769 * Switch all modem-channels who are online and got a valid
3770 * escape-sequence 1.5 seconds ago, to command-mode.
3771 * This function is called every second via timer-interrupt from within
3772 * timer-dispatcher isdn_timer_function()
3773 */
3774void
3775isdn_tty_modem_escape(void)
3776{
3777 int ton = 0;
3778 int i;
3779 int midx;
3780
3781 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
3782 if (USG_MODEM(dev->usage[i]))
3783 if ((midx = dev->m_idx[i]) >= 0) {
3784 modem_info *info = &dev->mdm.info[midx];
3785 if (info->online) {
3786 ton = 1;
3787 if ((info->emu.pluscount == 3) &&
3788 time_after(jiffies , info->emu.lastplus + PLUSWAIT2)) {
3789 info->emu.pluscount = 0;
3790 info->online = 0;
3791 isdn_tty_modem_result(RESULT_OK, info);
3792 }
3793 }
3794 }
3795 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
3796}
3797
3798/*
3799 * Put a RING-message to all modem-channels who have the RI-bit set.
3800 * This function is called every second via timer-interrupt from within
3801 * timer-dispatcher isdn_timer_function()
3802 */
3803void
3804isdn_tty_modem_ring(void)
3805{
3806 int ton = 0;
3807 int i;
3808
3809 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3810 modem_info *info = &dev->mdm.info[i];
3811 if (info->msr & UART_MSR_RI) {
3812 ton = 1;
3813 isdn_tty_modem_result(RESULT_RING, info);
3814 }
3815 }
3816 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
3817}
3818
3819/*
3820 * For all online tty's, try sending data to
3821 * the lower levels.
3822 */
3823void
3824isdn_tty_modem_xmit(void)
3825{
3826 int ton = 1;
3827 int i;
3828
3829 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3830 modem_info *info = &dev->mdm.info[i];
3831 if (info->online) {
3832 ton = 1;
3833 isdn_tty_senddown(info);
3834 isdn_tty_tint(info);
3835 }
3836 }
3837 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
3838}
3839
3840/*
3841 * Check all channels if we have a 'no carrier' timeout.
3842 * Timeout value is set by Register S7.
3843 */
3844void
3845isdn_tty_carrier_timeout(void)
3846{
3847 int ton = 0;
3848 int i;
3849
3850 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3851 modem_info *info = &dev->mdm.info[i];
3852 if (info->dialing) {
3853 if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
3854 info->dialing = 0;
3855 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3856 isdn_tty_modem_hup(info, 1);
3857 }
3858 else
3859 ton = 1;
3860 }
3861 }
3862 isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
3863}