blob: c81a725fde746f866a77d1aced075cda99164652 [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>
Jiri Slaby6776a2f2012-04-02 13:53:50 +020014#include <linux/serial.h> /* ASYNC_* flags */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/delay.h>
Arnd Bergmann72250d42010-09-14 09:35:04 +000017#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "isdn_common.h"
19#include "isdn_tty.h"
20#ifdef CONFIG_ISDN_AUDIO
21#include "isdn_audio.h"
22#define VBUF 0x3e0
23#define VBUFX (VBUF/16)
24#endif
25
26#define FIX_FILE_TRANSFER
27#define DUMMY_HAYES_AT
28
29/* Prototypes */
30
Arnd Bergmann72250d42010-09-14 09:35:04 +000031static DEFINE_MUTEX(modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int isdn_tty_edit_at(const char *, int, modem_info *);
33static void isdn_tty_check_esc(const u_char *, u_char, int, int *, u_long *);
34static void isdn_tty_modem_reset_regs(modem_info *, int);
35static void isdn_tty_cmd_ATA(modem_info *);
36static void isdn_tty_flush_buffer(struct tty_struct *);
37static void isdn_tty_modem_result(int, modem_info *);
38#ifdef CONFIG_ISDN_AUDIO
39static int isdn_tty_countDLE(unsigned char *, int);
40#endif
41
42/* Leave this unchanged unless you know what you do! */
43#define MODEM_PARANOIA_CHECK
44#define MODEM_DO_RESTART
45
46static int bit2si[8] =
47{1, 5, 7, 7, 7, 7, 7, 7};
48static int si2bit[8] =
49{4, 1, 4, 4, 4, 4, 4, 4};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* isdn_tty_try_read() is called from within isdn_tty_rcv_skb()
52 * to stuff incoming data directly into a tty's flip-buffer. This
53 * is done to speed up tty-receiving if the receive-queue is empty.
54 * This routine MUST be called with interrupts off.
55 * Return:
56 * 1 = Success
57 * 0 = Failure, data has to be buffered and later processed by
58 * isdn_tty_readmodem().
59 */
60static int
Joe Perches475be4d2012-02-19 19:52:38 -080061isdn_tty_try_read(modem_info *info, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 int c;
64 int len;
65 struct tty_struct *tty;
Alan Cox33f0f882006-01-09 20:54:13 -080066 char last;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jiri Slaby37630f42012-04-02 13:53:52 +020068 if (!info->online)
69 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -080070
Jiri Slaby37630f42012-04-02 13:53:52 +020071 tty = info->tty;
72 if (!tty)
73 return 0;
74
75 if (!(info->mcr & UART_MCR_RTS))
76 return 0;
77
78 len = skb->len
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#ifdef CONFIG_ISDN_AUDIO
Jiri Slaby37630f42012-04-02 13:53:52 +020080 + ISDN_AUDIO_SKB_DLECOUNT(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
Jiri Slaby37630f42012-04-02 13:53:52 +020082 ;
83
84 c = tty_buffer_request_room(tty, len);
85 if (c < len)
86 return 0;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#ifdef CONFIG_ISDN_AUDIO
Jiri Slaby37630f42012-04-02 13:53:52 +020089 if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
90 int l = skb->len;
91 unsigned char *dp = skb->data;
92 while (--l) {
93 if (*dp == DLE)
94 tty_insert_flip_char(tty, DLE, 0);
95 tty_insert_flip_char(tty, *dp++, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Jiri Slaby37630f42012-04-02 13:53:52 +020097 if (*dp == DLE)
98 tty_insert_flip_char(tty, DLE, 0);
99 last = *dp;
100 } else {
101#endif
102 if (len > 1)
103 tty_insert_flip_string(tty, skb->data, len - 1);
104 last = skb->data[len - 1];
105#ifdef CONFIG_ISDN_AUDIO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Jiri Slaby37630f42012-04-02 13:53:52 +0200107#endif
108 if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
109 tty_insert_flip_char(tty, last, 0xFF);
110 else
111 tty_insert_flip_char(tty, last, TTY_NORMAL);
112 tty_flip_buffer_push(tty);
113 kfree_skb(skb);
114
115 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/* isdn_tty_readmodem() is called periodically from within timer-interrupt.
119 * It tries getting received data from the receive queue an stuff it into
120 * the tty's flip-buffer.
121 */
122void
123isdn_tty_readmodem(void)
124{
125 int resched = 0;
126 int midx;
127 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int r;
129 struct tty_struct *tty;
130 modem_info *info;
131
132 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
Jiri Slaby37630f42012-04-02 13:53:52 +0200133 midx = dev->m_idx[i];
134 if (midx < 0)
135 continue;
136
137 info = &dev->mdm.info[midx];
138 if (!info->online)
139 continue;
140
141 r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#ifdef CONFIG_ISDN_AUDIO
Jiri Slaby37630f42012-04-02 13:53:52 +0200143 isdn_audio_eval_dtmf(info);
144 if ((info->vonline & 1) && (info->emu.vpar[1]))
145 isdn_audio_eval_silence(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#endif
Jiri Slaby37630f42012-04-02 13:53:52 +0200147 tty = info->tty;
148 if (tty) {
149 if (info->mcr & UART_MCR_RTS) {
150 /* CISCO AsyncPPP Hack */
151 if (!(info->emu.mdmreg[REG_CPPP] & BIT_CPPP))
152 r = isdn_readbchan_tty(info->isdn_driver, info->isdn_channel, tty, 0);
153 else
154 r = isdn_readbchan_tty(info->isdn_driver, info->isdn_channel, tty, 1);
155 if (r)
156 tty_flip_buffer_push(tty);
157 } else
158 r = 1;
159 } else
160 r = 1;
161 if (r) {
162 info->rcvsched = 0;
163 resched = 1;
164 } else
165 info->rcvsched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 if (!resched)
168 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
169}
170
171int
172isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
173{
174 ulong flags;
175 int midx;
176#ifdef CONFIG_ISDN_AUDIO
177 int ifmt;
178#endif
179 modem_info *info;
180
181 if ((midx = dev->m_idx[i]) < 0) {
182 /* if midx is invalid, packet is not for tty */
183 return 0;
184 }
185 info = &dev->mdm.info[midx];
186#ifdef CONFIG_ISDN_AUDIO
187 ifmt = 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if ((info->vonline) && (!info->emu.vpar[4]))
190 isdn_audio_calc_dtmf(info, skb->data, skb->len, ifmt);
191 if ((info->vonline & 1) && (info->emu.vpar[1]))
192 isdn_audio_calc_silence(info, skb->data, skb->len, ifmt);
193#endif
194 if ((info->online < 2)
195#ifdef CONFIG_ISDN_AUDIO
196 && (!(info->vonline & 1))
197#endif
198 ) {
199 /* If Modem not listening, drop data */
200 kfree_skb(skb);
201 return 1;
202 }
203 if (info->emu.mdmreg[REG_T70] & BIT_T70) {
204 if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT) {
205 /* T.70 decoding: throw away the T.70 header (2 or 4 bytes) */
206 if (skb->data[0] == 3) /* pure data packet -> 4 byte headers */
207 skb_pull(skb, 4);
208 else
209 if (skb->data[0] == 1) /* keepalive packet -> 2 byte hdr */
210 skb_pull(skb, 2);
211 } else
212 /* T.70 decoding: Simply throw away the T.70 header (4 bytes) */
213 if ((skb->data[0] == 1) && ((skb->data[1] == 0) || (skb->data[1] == 1)))
214 skb_pull(skb, 4);
215 }
216#ifdef CONFIG_ISDN_AUDIO
217 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
218 ISDN_AUDIO_SKB_LOCK(skb) = 0;
219 if (info->vonline & 1) {
220 /* voice conversion/compression */
221 switch (info->emu.vpar[3]) {
Joe Perches475be4d2012-02-19 19:52:38 -0800222 case 2:
223 case 3:
224 case 4:
225 /* adpcm
226 * Since compressed data takes less
227 * space, we can overwrite the buffer.
228 */
229 skb_trim(skb, isdn_audio_xlaw2adpcm(info->adpcmr,
230 ifmt,
231 skb->data,
232 skb->data,
233 skb->len));
234 break;
235 case 5:
236 /* a-law */
237 if (!ifmt)
238 isdn_audio_ulaw2alaw(skb->data, skb->len);
239 break;
240 case 6:
241 /* u-law */
242 if (ifmt)
243 isdn_audio_alaw2ulaw(skb->data, skb->len);
244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 ISDN_AUDIO_SKB_DLECOUNT(skb) =
247 isdn_tty_countDLE(skb->data, skb->len);
248 }
249#ifdef CONFIG_ISDN_TTY_FAX
250 else {
251 if (info->faxonline & 2) {
252 isdn_tty_fax_bitorder(info, skb);
253 ISDN_AUDIO_SKB_DLECOUNT(skb) =
254 isdn_tty_countDLE(skb->data, skb->len);
255 }
256 }
257#endif
258#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800259 /* Try to deliver directly via tty-buf if queue is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 spin_lock_irqsave(&info->readlock, flags);
261 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
262 if (isdn_tty_try_read(info, skb)) {
263 spin_unlock_irqrestore(&info->readlock, flags);
264 return 1;
265 }
266 /* Direct deliver failed or queue wasn't empty.
267 * Queue up for later dequeueing via timer-irq.
268 */
269 __skb_queue_tail(&dev->drv[di]->rpqueue[channel], skb);
270 dev->drv[di]->rcvcount[channel] +=
271 (skb->len
272#ifdef CONFIG_ISDN_AUDIO
273 + ISDN_AUDIO_SKB_DLECOUNT(skb)
274#endif
275 );
276 spin_unlock_irqrestore(&info->readlock, flags);
277 /* Schedule dequeuing */
278 if ((dev->modempoll) && (info->rcvsched))
279 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
280 return 1;
281}
282
Adrian Bunk3e206b02005-06-25 14:58:35 -0700283static void
Joe Perches475be4d2012-02-19 19:52:38 -0800284isdn_tty_cleanup_xmit(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 skb_queue_purge(&info->xmit_queue);
287#ifdef CONFIG_ISDN_AUDIO
288 skb_queue_purge(&info->dtmf_queue);
289#endif
290}
291
292static void
Joe Perches475be4d2012-02-19 19:52:38 -0800293isdn_tty_tint(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
296 int len, slen;
297
298 if (!skb)
299 return;
300 len = skb->len;
301 if ((slen = isdn_writebuf_skb_stub(info->isdn_driver,
302 info->isdn_channel, 1, skb)) == len) {
303 struct tty_struct *tty = info->tty;
304 info->send_outstanding++;
305 info->msr &= ~UART_MSR_CTS;
306 info->lsr &= ~UART_LSR_TEMT;
307 tty_wakeup(tty);
308 return;
309 }
310 if (slen < 0) {
311 /* Error: no channel, already shutdown, or wrong parameter */
312 dev_kfree_skb(skb);
313 return;
314 }
315 skb_queue_head(&info->xmit_queue, skb);
316}
317
318#ifdef CONFIG_ISDN_AUDIO
319static int
320isdn_tty_countDLE(unsigned char *buf, int len)
321{
322 int count = 0;
323
324 while (len--)
325 if (*buf++ == DLE)
326 count++;
327 return count;
328}
329
330/* This routine is called from within isdn_tty_write() to perform
331 * DLE-decoding when sending audio-data.
332 */
333static int
Joe Perches475be4d2012-02-19 19:52:38 -0800334isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 unsigned char *p = &info->xmit_buf[info->xmit_count];
337 int count = 0;
338
339 while (len > 0) {
340 if (m->lastDLE) {
341 m->lastDLE = 0;
342 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -0800343 case DLE:
344 /* Escape code */
345 if (len > 1)
346 memmove(p, p + 1, len - 1);
347 p--;
348 count++;
349 break;
350 case ETX:
351 /* End of data */
352 info->vonline |= 4;
353 return count;
354 case DC4:
355 /* Abort RX */
356 info->vonline &= ~1;
357#ifdef ISDN_DEBUG_MODEM_VOICE
358 printk(KERN_DEBUG
359 "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
360 info->line);
361#endif
362 isdn_tty_at_cout("\020\003", info);
363 if (!info->vonline) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#ifdef ISDN_DEBUG_MODEM_VOICE
365 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800366 "DLEdown: send VCON on ttyI%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 info->line);
368#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800369 isdn_tty_at_cout("\r\nVCON\r\n", info);
370 }
371 /* Fall through */
372 case 'q':
373 case 's':
374 /* Silence */
375 if (len > 1)
376 memmove(p, p + 1, len - 1);
377 p--;
378 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 } else {
381 if (*p == DLE)
382 m->lastDLE = 1;
383 else
384 count++;
385 }
386 p++;
387 len--;
388 }
389 if (len < 0) {
390 printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
391 return 0;
392 }
393 return count;
394}
395
396/* This routine is called from within isdn_tty_write() when receiving
397 * audio-data. It interrupts receiving, if an character other than
398 * ^S or ^Q is sent.
399 */
400static int
401isdn_tty_end_vrx(const char *buf, int c)
402{
403 char ch;
404
405 while (c--) {
406 ch = *buf;
407 if ((ch != 0x11) && (ch != 0x13))
408 return 1;
409 buf++;
410 }
411 return 0;
412}
413
414static int voice_cf[7] =
415{0, 0, 4, 3, 2, 0, 0};
416
417#endif /* CONFIG_ISDN_AUDIO */
418
419/* isdn_tty_senddown() is called either directly from within isdn_tty_write()
420 * or via timer-interrupt from within isdn_tty_modem_xmit(). It pulls
421 * outgoing data from the tty's xmit-buffer, handles voice-decompression or
422 * T.70 if necessary, and finally queues it up for sending via isdn_tty_tint.
423 */
424static void
Joe Perches475be4d2012-02-19 19:52:38 -0800425isdn_tty_senddown(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 int buflen;
428 int skb_res;
429#ifdef CONFIG_ISDN_AUDIO
430 int audio_len;
431#endif
432 struct sk_buff *skb;
433
434#ifdef CONFIG_ISDN_AUDIO
435 if (info->vonline & 4) {
436 info->vonline &= ~6;
437 if (!info->vonline) {
438#ifdef ISDN_DEBUG_MODEM_VOICE
439 printk(KERN_DEBUG
440 "senddown: send VCON on ttyI%d\n",
441 info->line);
442#endif
443 isdn_tty_at_cout("\r\nVCON\r\n", info);
444 }
445 }
446#endif
447 if (!(buflen = info->xmit_count))
448 return;
Joe Perches475be4d2012-02-19 19:52:38 -0800449 if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 info->msr &= ~UART_MSR_CTS;
Joe Perches475be4d2012-02-19 19:52:38 -0800451 info->lsr &= ~UART_LSR_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* info->xmit_count is modified here and in isdn_tty_write().
453 * So we return here if isdn_tty_write() is in the
454 * critical section.
455 */
456 atomic_inc(&info->xmit_lock);
457 if (!(atomic_dec_and_test(&info->xmit_lock)))
458 return;
459 if (info->isdn_driver < 0) {
460 info->xmit_count = 0;
461 return;
462 }
463 skb_res = dev->drv[info->isdn_driver]->interface->hl_hdrlen + 4;
464#ifdef CONFIG_ISDN_AUDIO
465 if (info->vonline & 2)
466 audio_len = buflen * voice_cf[info->emu.vpar[3]];
467 else
468 audio_len = 0;
469 skb = dev_alloc_skb(skb_res + buflen + audio_len);
470#else
471 skb = dev_alloc_skb(skb_res + buflen);
472#endif
473 if (!skb) {
474 printk(KERN_WARNING
475 "isdn_tty: Out of memory in ttyI%d senddown\n",
476 info->line);
477 return;
478 }
479 skb_reserve(skb, skb_res);
480 memcpy(skb_put(skb, buflen), info->xmit_buf, buflen);
481 info->xmit_count = 0;
482#ifdef CONFIG_ISDN_AUDIO
483 if (info->vonline & 2) {
484 /* For now, ifmt is fixed to 1 (alaw), since this
485 * is used with ISDN everywhere in the world, except
486 * US, Canada and Japan.
487 * Later, when US-ISDN protocols are implemented,
488 * this setting will depend on the D-channel protocol.
489 */
490 int ifmt = 1;
491
492 /* voice conversion/decompression */
493 switch (info->emu.vpar[3]) {
Joe Perches475be4d2012-02-19 19:52:38 -0800494 case 2:
495 case 3:
496 case 4:
497 /* adpcm, compatible to ZyXel 1496 modem
498 * with ROM revision 6.01
499 */
500 audio_len = isdn_audio_adpcm2xlaw(info->adpcms,
501 ifmt,
502 skb->data,
503 skb_put(skb, audio_len),
504 buflen);
505 skb_pull(skb, buflen);
506 skb_trim(skb, audio_len);
507 break;
508 case 5:
509 /* a-law */
510 if (!ifmt)
511 isdn_audio_alaw2ulaw(skb->data,
512 buflen);
513 break;
514 case 6:
515 /* u-law */
516 if (ifmt)
517 isdn_audio_ulaw2alaw(skb->data,
518 buflen);
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 }
522#endif /* CONFIG_ISDN_AUDIO */
523 if (info->emu.mdmreg[REG_T70] & BIT_T70) {
524 /* Add T.70 simplified header */
525 if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT)
526 memcpy(skb_push(skb, 2), "\1\0", 2);
527 else
528 memcpy(skb_push(skb, 4), "\1\0\1\0", 4);
529 }
530 skb_queue_tail(&info->xmit_queue, skb);
531}
532
533/************************************************************
534 *
535 * Modem-functions
536 *
537 * mostly "stolen" from original Linux-serial.c and friends.
538 *
539 ************************************************************/
540
541/* The next routine is called once from within timer-interrupt
542 * triggered within isdn_tty_modem_ncarrier(). It calls
543 * isdn_tty_modem_result() to stuff a "NO CARRIER" Message
Alan Cox33f0f882006-01-09 20:54:13 -0800544 * into the tty's buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
546static void
547isdn_tty_modem_do_ncarrier(unsigned long data)
548{
549 modem_info *info = (modem_info *) data;
550 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
551}
552
553/* Next routine is called, whenever the DTR-signal is raised.
554 * It checks the ncarrier-flag, and triggers the above routine
555 * when necessary. The ncarrier-flag is set, whenever DTR goes
556 * low.
557 */
558static void
Joe Perches475be4d2012-02-19 19:52:38 -0800559isdn_tty_modem_ncarrier(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 if (info->ncarrier) {
562 info->nc_timer.expires = jiffies + HZ;
563 add_timer(&info->nc_timer);
564 }
565}
566
567/*
568 * return the usage calculated by si and layer 2 protocol
569 */
Adrian Bunk3e206b02005-06-25 14:58:35 -0700570static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571isdn_calc_usage(int si, int l2)
572{
573 int usg = ISDN_USAGE_MODEM;
574
575#ifdef CONFIG_ISDN_AUDIO
576 if (si == 1) {
Joe Perches475be4d2012-02-19 19:52:38 -0800577 switch (l2) {
578 case ISDN_PROTO_L2_MODEM:
579 usg = ISDN_USAGE_MODEM;
580 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800582 case ISDN_PROTO_L2_FAX:
583 usg = ISDN_USAGE_FAX;
584 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800586 case ISDN_PROTO_L2_TRANS:
587 default:
588 usg = ISDN_USAGE_VOICE;
589 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 }
592#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800593 return (usg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596/* isdn_tty_dial() performs dialing of a tty an the necessary
597 * setup of the lower levels before that.
598 */
599static void
Joe Perches475be4d2012-02-19 19:52:38 -0800600isdn_tty_dial(char *n, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 int usg = ISDN_USAGE_MODEM;
603 int si = 7;
604 int l2 = m->mdmreg[REG_L2PROT];
605 u_long flags;
606 isdn_ctrl cmd;
607 int i;
608 int j;
609
610 for (j = 7; j >= 0; j--)
611 if (m->mdmreg[REG_SI1] & (1 << j)) {
612 si = bit2si[j];
613 break;
614 }
615 usg = isdn_calc_usage(si, l2);
616#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -0800617 if ((si == 1) &&
618 (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800620 && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#endif
622 ) {
623 l2 = ISDN_PROTO_L2_TRANS;
624 usg = ISDN_USAGE_VOICE;
625 }
626#endif
627 m->mdmreg[REG_SI1I] = si2bit[si];
628 spin_lock_irqsave(&dev->lock, flags);
629 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
630 if (i < 0) {
631 spin_unlock_irqrestore(&dev->lock, flags);
632 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
633 } else {
634 info->isdn_driver = dev->drvmap[i];
635 info->isdn_channel = dev->chanmap[i];
636 info->drv_index = i;
637 dev->m_idx[i] = info->line;
638 dev->usage[i] |= ISDN_USAGE_OUTGOING;
639 info->last_dir = 1;
640 strcpy(info->last_num, n);
641 isdn_info_update();
642 spin_unlock_irqrestore(&dev->lock, flags);
643 cmd.driver = info->isdn_driver;
644 cmd.arg = info->isdn_channel;
645 cmd.command = ISDN_CMD_CLREAZ;
646 isdn_command(&cmd);
647 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
648 cmd.driver = info->isdn_driver;
649 cmd.command = ISDN_CMD_SETEAZ;
650 isdn_command(&cmd);
651 cmd.driver = info->isdn_driver;
652 cmd.command = ISDN_CMD_SETL2;
653 info->last_l2 = l2;
654 cmd.arg = info->isdn_channel + (l2 << 8);
655 isdn_command(&cmd);
656 cmd.driver = info->isdn_driver;
657 cmd.command = ISDN_CMD_SETL3;
658 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
659#ifdef CONFIG_ISDN_TTY_FAX
660 if (l2 == ISDN_PROTO_L2_FAX) {
661 cmd.parm.fax = info->fax;
662 info->fax->direction = ISDN_TTY_FAX_CONN_OUT;
663 }
664#endif
665 isdn_command(&cmd);
666 cmd.driver = info->isdn_driver;
667 cmd.arg = info->isdn_channel;
668 sprintf(cmd.parm.setup.phone, "%s", n);
669 sprintf(cmd.parm.setup.eazmsn, "%s",
670 isdn_map_eaz2msn(m->msn, info->isdn_driver));
671 cmd.parm.setup.si1 = si;
672 cmd.parm.setup.si2 = m->mdmreg[REG_SI2];
673 cmd.command = ISDN_CMD_DIAL;
674 info->dialing = 1;
675 info->emu.carrierwait = 0;
676 strcpy(dev->num[i], n);
677 isdn_info_update();
678 isdn_command(&cmd);
679 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
680 }
681}
682
683/* isdn_tty_hangup() disassociates a tty from the real
684 * ISDN-line (hangup). The usage-status is cleared
685 * and some cleanup is done also.
686 */
687void
Joe Perches475be4d2012-02-19 19:52:38 -0800688isdn_tty_modem_hup(modem_info *info, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 isdn_ctrl cmd;
691 int di, ch;
692
693 if (!info)
694 return;
695
696 di = info->isdn_driver;
697 ch = info->isdn_channel;
698 if (di < 0 || ch < 0)
699 return;
700
701 info->isdn_driver = -1;
702 info->isdn_channel = -1;
703
704#ifdef ISDN_DEBUG_MODEM_HUP
705 printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
706#endif
707 info->rcvsched = 0;
708 isdn_tty_flush_buffer(info->tty);
709 if (info->online) {
710 info->last_lhup = local;
711 info->online = 0;
712 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
713 }
714#ifdef CONFIG_ISDN_AUDIO
715 info->vonline = 0;
716#ifdef CONFIG_ISDN_TTY_FAX
717 info->faxonline = 0;
718 info->fax->phase = ISDN_FAX_PHASE_IDLE;
719#endif
720 info->emu.vpar[4] = 0;
721 info->emu.vpar[5] = 8;
Jesper Juhl3c7208f2005-11-07 01:01:29 -0800722 kfree(info->dtmf_state);
723 info->dtmf_state = NULL;
724 kfree(info->silence_state);
725 info->silence_state = NULL;
726 kfree(info->adpcms);
727 info->adpcms = NULL;
728 kfree(info->adpcmr);
729 info->adpcmr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#endif
731 if ((info->msr & UART_MSR_RI) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800732 (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 isdn_tty_modem_result(RESULT_RUNG, info);
734 info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
735 info->lsr |= UART_LSR_TEMT;
736
737 if (local) {
738 cmd.driver = di;
739 cmd.command = ISDN_CMD_HANGUP;
740 cmd.arg = ch;
741 isdn_command(&cmd);
742 }
743
744 isdn_all_eaz(di, ch);
745 info->emu.mdmreg[REG_RINGCNT] = 0;
746 isdn_free_channel(di, ch, 0);
747
748 if (info->drv_index >= 0) {
749 dev->m_idx[info->drv_index] = -1;
750 info->drv_index = -1;
751 }
752}
753
754/*
Joe Perches475be4d2012-02-19 19:52:38 -0800755 * Begin of a CAPI like interface, currently used only for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * supplementary service (CAPI 2.0 part III)
757 */
758#include <linux/isdn/capicmd.h>
Paul Gortmaker07a97fe2011-08-30 12:08:51 -0400759#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761int
762isdn_tty_capi_facility(capi_msg *cm) {
Joe Perches475be4d2012-02-19 19:52:38 -0800763 return (-1); /* dummy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/* isdn_tty_suspend() tries to suspend the current tty connection
767 */
768static void
Joe Perches475be4d2012-02-19 19:52:38 -0800769isdn_tty_suspend(char *id, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 isdn_ctrl cmd;
Joe Perches475be4d2012-02-19 19:52:38 -0800772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int l;
774
775 if (!info)
776 return;
777
778#ifdef ISDN_DEBUG_MODEM_SERVICES
779 printk(KERN_DEBUG "Msusp ttyI%d\n", info->line);
780#endif
781 l = strlen(id);
782 if ((info->isdn_driver >= 0)) {
Joe Perches475be4d2012-02-19 19:52:38 -0800783 cmd.parm.cmsg.Length = l + 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 cmd.parm.cmsg.Command = CAPI_FACILITY;
785 cmd.parm.cmsg.Subcommand = CAPI_REQ;
786 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
787 cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
788 cmd.parm.cmsg.para[1] = 0;
789 cmd.parm.cmsg.para[2] = l + 3;
790 cmd.parm.cmsg.para[3] = 4; /* 16 bit 0x0004 Suspend */
791 cmd.parm.cmsg.para[4] = 0;
792 cmd.parm.cmsg.para[5] = l;
793 strncpy(&cmd.parm.cmsg.para[6], id, l);
794 cmd.command = CAPI_PUT_MESSAGE;
795 cmd.driver = info->isdn_driver;
796 cmd.arg = info->isdn_channel;
797 isdn_command(&cmd);
798 }
799}
800
801/* isdn_tty_resume() tries to resume a suspended call
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300802 * setup of the lower levels before that. unfortunately here is no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * checking for compatibility of used protocols implemented by Q931
804 * It does the same things like isdn_tty_dial, the last command
805 * is different, may be we can merge it.
806 */
807
808static void
Joe Perches475be4d2012-02-19 19:52:38 -0800809isdn_tty_resume(char *id, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 int usg = ISDN_USAGE_MODEM;
812 int si = 7;
813 int l2 = m->mdmreg[REG_L2PROT];
814 isdn_ctrl cmd;
815 ulong flags;
816 int i;
817 int j;
818 int l;
819
820 l = strlen(id);
821 for (j = 7; j >= 0; j--)
822 if (m->mdmreg[REG_SI1] & (1 << j)) {
823 si = bit2si[j];
824 break;
825 }
826 usg = isdn_calc_usage(si, l2);
827#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -0800828 if ((si == 1) &&
829 (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800831 && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832#endif
833 ) {
834 l2 = ISDN_PROTO_L2_TRANS;
835 usg = ISDN_USAGE_VOICE;
836 }
837#endif
838 m->mdmreg[REG_SI1I] = si2bit[si];
839 spin_lock_irqsave(&dev->lock, flags);
840 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
841 if (i < 0) {
842 spin_unlock_irqrestore(&dev->lock, flags);
843 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
844 } else {
845 info->isdn_driver = dev->drvmap[i];
846 info->isdn_channel = dev->chanmap[i];
847 info->drv_index = i;
848 dev->m_idx[i] = info->line;
849 dev->usage[i] |= ISDN_USAGE_OUTGOING;
850 info->last_dir = 1;
851// strcpy(info->last_num, n);
852 isdn_info_update();
853 spin_unlock_irqrestore(&dev->lock, flags);
854 cmd.driver = info->isdn_driver;
855 cmd.arg = info->isdn_channel;
856 cmd.command = ISDN_CMD_CLREAZ;
857 isdn_command(&cmd);
858 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
859 cmd.driver = info->isdn_driver;
860 cmd.command = ISDN_CMD_SETEAZ;
861 isdn_command(&cmd);
862 cmd.driver = info->isdn_driver;
863 cmd.command = ISDN_CMD_SETL2;
864 info->last_l2 = l2;
865 cmd.arg = info->isdn_channel + (l2 << 8);
866 isdn_command(&cmd);
867 cmd.driver = info->isdn_driver;
868 cmd.command = ISDN_CMD_SETL3;
869 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
870 isdn_command(&cmd);
871 cmd.driver = info->isdn_driver;
872 cmd.arg = info->isdn_channel;
Joe Perches475be4d2012-02-19 19:52:38 -0800873 cmd.parm.cmsg.Length = l + 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 cmd.parm.cmsg.Command = CAPI_FACILITY;
875 cmd.parm.cmsg.Subcommand = CAPI_REQ;
876 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
877 cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
878 cmd.parm.cmsg.para[1] = 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800879 cmd.parm.cmsg.para[2] = l + 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 cmd.parm.cmsg.para[3] = 5; /* 16 bit 0x0005 Resume */
881 cmd.parm.cmsg.para[4] = 0;
882 cmd.parm.cmsg.para[5] = l;
883 strncpy(&cmd.parm.cmsg.para[6], id, l);
Joe Perches475be4d2012-02-19 19:52:38 -0800884 cmd.command = CAPI_PUT_MESSAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 info->dialing = 1;
886// strcpy(dev->num[i], n);
887 isdn_info_update();
888 isdn_command(&cmd);
889 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
890 }
891}
892
893/* isdn_tty_send_msg() sends a message to a HL driver
894 * This is used for hybrid modem cards to send AT commands to it
895 */
896
897static void
Joe Perches475be4d2012-02-19 19:52:38 -0800898isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 int usg = ISDN_USAGE_MODEM;
901 int si = 7;
902 int l2 = m->mdmreg[REG_L2PROT];
903 isdn_ctrl cmd;
904 ulong flags;
905 int i;
906 int j;
907 int l;
908
909 l = strlen(msg);
910 if (!l) {
911 isdn_tty_modem_result(RESULT_ERROR, info);
912 return;
913 }
914 for (j = 7; j >= 0; j--)
915 if (m->mdmreg[REG_SI1] & (1 << j)) {
916 si = bit2si[j];
917 break;
918 }
919 usg = isdn_calc_usage(si, l2);
920#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -0800921 if ((si == 1) &&
922 (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800924 && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925#endif
926 ) {
927 l2 = ISDN_PROTO_L2_TRANS;
928 usg = ISDN_USAGE_VOICE;
929 }
930#endif
931 m->mdmreg[REG_SI1I] = si2bit[si];
932 spin_lock_irqsave(&dev->lock, flags);
933 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
934 if (i < 0) {
935 spin_unlock_irqrestore(&dev->lock, flags);
936 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
937 } else {
938 info->isdn_driver = dev->drvmap[i];
939 info->isdn_channel = dev->chanmap[i];
940 info->drv_index = i;
941 dev->m_idx[i] = info->line;
942 dev->usage[i] |= ISDN_USAGE_OUTGOING;
943 info->last_dir = 1;
944 isdn_info_update();
945 spin_unlock_irqrestore(&dev->lock, flags);
946 cmd.driver = info->isdn_driver;
947 cmd.arg = info->isdn_channel;
948 cmd.command = ISDN_CMD_CLREAZ;
949 isdn_command(&cmd);
950 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
951 cmd.driver = info->isdn_driver;
952 cmd.command = ISDN_CMD_SETEAZ;
953 isdn_command(&cmd);
954 cmd.driver = info->isdn_driver;
955 cmd.command = ISDN_CMD_SETL2;
956 info->last_l2 = l2;
957 cmd.arg = info->isdn_channel + (l2 << 8);
958 isdn_command(&cmd);
959 cmd.driver = info->isdn_driver;
960 cmd.command = ISDN_CMD_SETL3;
961 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
962 isdn_command(&cmd);
963 cmd.driver = info->isdn_driver;
964 cmd.arg = info->isdn_channel;
Joe Perches475be4d2012-02-19 19:52:38 -0800965 cmd.parm.cmsg.Length = l + 14;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
967 cmd.parm.cmsg.Subcommand = CAPI_REQ;
968 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800969 cmd.parm.cmsg.para[0] = l + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 strncpy(&cmd.parm.cmsg.para[1], msg, l);
Joe Perches475be4d2012-02-19 19:52:38 -0800971 cmd.parm.cmsg.para[l + 1] = 0xd;
972 cmd.command = CAPI_PUT_MESSAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/* info->dialing = 1;
974 strcpy(dev->num[i], n);
975 isdn_info_update();
976*/
977 isdn_command(&cmd);
978 }
979}
980
981static inline int
982isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
983{
984#ifdef MODEM_PARANOIA_CHECK
985 if (!info) {
986 printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800987 name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 1;
989 }
990 if (info->magic != ISDN_ASYNC_MAGIC) {
991 printk(KERN_WARNING "isdn_tty: bad magic for modem struct %s in %s\n",
992 name, routine);
993 return 1;
994 }
995#endif
996 return 0;
997}
998
999/*
1000 * This routine is called to set the UART divisor registers to match
1001 * the specified baud rate for a serial port.
1002 */
1003static void
Joe Perches475be4d2012-02-19 19:52:38 -08001004isdn_tty_change_speed(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 uint cflag,
Joe Perches475be4d2012-02-19 19:52:38 -08001007 cval,
1008 quot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int i;
1010
1011 if (!info->tty || !info->tty->termios)
1012 return;
1013 cflag = info->tty->termios->c_cflag;
1014
1015 quot = i = cflag & CBAUD;
1016 if (i & CBAUDEX) {
1017 i &= ~CBAUDEX;
1018 if (i < 1 || i > 2)
1019 info->tty->termios->c_cflag &= ~CBAUDEX;
1020 else
1021 i += 15;
1022 }
1023 if (quot) {
1024 info->mcr |= UART_MCR_DTR;
1025 isdn_tty_modem_ncarrier(info);
1026 } else {
1027 info->mcr &= ~UART_MCR_DTR;
1028 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1029#ifdef ISDN_DEBUG_MODEM_HUP
1030 printk(KERN_DEBUG "Mhup in changespeed\n");
1031#endif
1032 if (info->online)
1033 info->ncarrier = 1;
1034 isdn_tty_modem_reset_regs(info, 0);
1035 isdn_tty_modem_hup(info, 1);
1036 }
1037 return;
1038 }
1039 /* byte size and parity */
1040 cval = cflag & (CSIZE | CSTOPB);
1041 cval >>= 4;
1042 if (cflag & PARENB)
1043 cval |= UART_LCR_PARITY;
1044 if (!(cflag & PARODD))
1045 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* CTS flow control flag and modem status interrupts */
1048 if (cflag & CRTSCTS) {
Jiri Slaby48decc12012-04-02 13:53:54 +02001049 info->port.flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 } else
Jiri Slaby48decc12012-04-02 13:53:54 +02001051 info->port.flags &= ~ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (cflag & CLOCAL)
Jiri Slaby48decc12012-04-02 13:53:54 +02001053 info->port.flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 else {
Jiri Slaby48decc12012-04-02 13:53:54 +02001055 info->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057}
1058
1059static int
Joe Perches475be4d2012-02-19 19:52:38 -08001060isdn_tty_startup(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Jiri Slaby48decc12012-04-02 13:53:54 +02001062 if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064 isdn_lock_drivers();
1065#ifdef ISDN_DEBUG_MODEM_OPEN
1066 printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
1067#endif
1068 /*
1069 * Now, initialize the UART
1070 */
1071 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
1072 if (info->tty)
1073 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1074 /*
1075 * and set the speed of the serial port
1076 */
1077 isdn_tty_change_speed(info);
1078
Jiri Slaby48decc12012-04-02 13:53:54 +02001079 info->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
1081 info->send_outstanding = 0;
1082 return 0;
1083}
1084
1085/*
1086 * This routine will shutdown a serial port; interrupts are disabled, and
1087 * DTR is dropped if the hangup on close termio flag is on.
1088 */
1089static void
Joe Perches475be4d2012-02-19 19:52:38 -08001090isdn_tty_shutdown(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Jiri Slaby48decc12012-04-02 13:53:54 +02001092 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return;
1094#ifdef ISDN_DEBUG_MODEM_OPEN
1095 printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
1096#endif
1097 isdn_unlock_drivers();
1098 info->msr &= ~UART_MSR_RI;
1099 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1100 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
1101 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1102 isdn_tty_modem_reset_regs(info, 0);
1103#ifdef ISDN_DEBUG_MODEM_HUP
1104 printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
1105#endif
1106 isdn_tty_modem_hup(info, 1);
1107 }
1108 }
1109 if (info->tty)
1110 set_bit(TTY_IO_ERROR, &info->tty->flags);
1111
Jiri Slaby48decc12012-04-02 13:53:54 +02001112 info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115/* isdn_tty_write() is the main send-routine. It is called from the upper
1116 * levels within the kernel to perform sending data. Depending on the
1117 * online-flag it either directs output to the at-command-interpreter or
1118 * to the lower level. Additional tasks done here:
1119 * - If online, check for escape-sequence (+++)
1120 * - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
1121 * - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
1122 * - If dialing, abort dial.
1123 */
1124static int
Joe Perches475be4d2012-02-19 19:52:38 -08001125isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 int c;
1128 int total = 0;
1129 modem_info *info = (modem_info *) tty->driver_data;
1130 atemu *m = &info->emu;
1131
1132 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write"))
1133 return 0;
1134 /* See isdn_tty_senddown() */
1135 atomic_inc(&info->xmit_lock);
1136 while (1) {
1137 c = count;
1138 if (c > info->xmit_size - info->xmit_count)
1139 c = info->xmit_size - info->xmit_count;
1140 if (info->isdn_driver >= 0 && c > dev->drv[info->isdn_driver]->maxbufsize)
1141 c = dev->drv[info->isdn_driver]->maxbufsize;
1142 if (c <= 0)
1143 break;
1144 if ((info->online > 1)
1145#ifdef CONFIG_ISDN_AUDIO
1146 || (info->vonline & 3)
1147#endif
1148 ) {
1149#ifdef CONFIG_ISDN_AUDIO
1150 if (!info->vonline)
1151#endif
1152 isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
1153 &(m->pluscount),
1154 &(m->lastplus));
1155 memcpy(&(info->xmit_buf[info->xmit_count]), buf, c);
1156#ifdef CONFIG_ISDN_AUDIO
1157 if (info->vonline) {
1158 int cc = isdn_tty_handleDLEdown(info, m, c);
1159 if (info->vonline & 2) {
1160 if (!cc) {
1161 /* If DLE decoding results in zero-transmit, but
1162 * c originally was non-zero, do a wakeup.
1163 */
1164 tty_wakeup(tty);
1165 info->msr |= UART_MSR_CTS;
1166 info->lsr |= UART_LSR_TEMT;
1167 }
1168 info->xmit_count += cc;
1169 }
1170 if ((info->vonline & 3) == 1) {
1171 /* Do NOT handle Ctrl-Q or Ctrl-S
1172 * when in full-duplex audio mode.
1173 */
1174 if (isdn_tty_end_vrx(buf, c)) {
1175 info->vonline &= ~1;
1176#ifdef ISDN_DEBUG_MODEM_VOICE
1177 printk(KERN_DEBUG
1178 "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
1179 info->line);
1180#endif
1181 isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
1182 }
1183 }
1184 } else
Joe Perches475be4d2012-02-19 19:52:38 -08001185 if (TTY_IS_FCLASS1(info)) {
1186 int cc = isdn_tty_handleDLEdown(info, m, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Joe Perches475be4d2012-02-19 19:52:38 -08001188 if (info->vonline & 4) { /* ETX seen */
1189 isdn_ctrl c;
1190
1191 c.command = ISDN_CMD_FAXCMD;
1192 c.driver = info->isdn_driver;
1193 c.arg = info->isdn_channel;
1194 c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
1195 c.parm.aux.subcmd = ETX;
1196 isdn_command(&c);
1197 }
1198 info->vonline = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#ifdef ISDN_DEBUG_MODEM_VOICE
Joe Perches475be4d2012-02-19 19:52:38 -08001200 printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001202 info->xmit_count += cc;
1203 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001205 info->xmit_count += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 } else {
1207 info->msr |= UART_MSR_CTS;
1208 info->lsr |= UART_LSR_TEMT;
1209 if (info->dialing) {
1210 info->dialing = 0;
1211#ifdef ISDN_DEBUG_MODEM_HUP
1212 printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
1213#endif
1214 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
1215 isdn_tty_modem_hup(info, 1);
1216 } else
1217 c = isdn_tty_edit_at(buf, c, info);
1218 }
1219 buf += c;
1220 count -= c;
1221 total += c;
1222 }
1223 atomic_dec(&info->xmit_lock);
David S. Millerb03efcf2005-07-08 14:57:23 -07001224 if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
1226 isdn_tty_senddown(info);
1227 isdn_tty_tint(info);
1228 }
1229 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1230 }
1231 return total;
1232}
1233
1234static int
1235isdn_tty_write_room(struct tty_struct *tty)
1236{
1237 modem_info *info = (modem_info *) tty->driver_data;
1238 int ret;
1239
1240 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write_room"))
1241 return 0;
1242 if (!info->online)
1243 return info->xmit_size;
1244 ret = info->xmit_size - info->xmit_count;
1245 return (ret < 0) ? 0 : ret;
1246}
1247
1248static int
1249isdn_tty_chars_in_buffer(struct tty_struct *tty)
1250{
1251 modem_info *info = (modem_info *) tty->driver_data;
1252
1253 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_chars_in_buffer"))
1254 return 0;
1255 if (!info->online)
1256 return 0;
1257 return (info->xmit_count);
1258}
1259
1260static void
1261isdn_tty_flush_buffer(struct tty_struct *tty)
1262{
1263 modem_info *info;
1264
1265 if (!tty) {
1266 return;
1267 }
1268 info = (modem_info *) tty->driver_data;
1269 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
1270 return;
1271 }
1272 isdn_tty_cleanup_xmit(info);
1273 info->xmit_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 tty_wakeup(tty);
1275}
1276
1277static void
1278isdn_tty_flush_chars(struct tty_struct *tty)
1279{
1280 modem_info *info = (modem_info *) tty->driver_data;
1281
1282 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
1283 return;
David S. Millerb03efcf2005-07-08 14:57:23 -07001284 if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1286}
1287
1288/*
1289 * ------------------------------------------------------------
1290 * isdn_tty_throttle()
1291 *
1292 * This routine is called by the upper-layer tty layer to signal that
1293 * incoming characters should be throttled.
1294 * ------------------------------------------------------------
1295 */
1296static void
1297isdn_tty_throttle(struct tty_struct *tty)
1298{
1299 modem_info *info = (modem_info *) tty->driver_data;
1300
1301 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_throttle"))
1302 return;
1303 if (I_IXOFF(tty))
1304 info->x_char = STOP_CHAR(tty);
1305 info->mcr &= ~UART_MCR_RTS;
1306}
1307
1308static void
1309isdn_tty_unthrottle(struct tty_struct *tty)
1310{
1311 modem_info *info = (modem_info *) tty->driver_data;
1312
1313 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_unthrottle"))
1314 return;
1315 if (I_IXOFF(tty)) {
1316 if (info->x_char)
1317 info->x_char = 0;
1318 else
1319 info->x_char = START_CHAR(tty);
1320 }
1321 info->mcr |= UART_MCR_RTS;
1322}
1323
1324/*
1325 * ------------------------------------------------------------
1326 * isdn_tty_ioctl() and friends
1327 * ------------------------------------------------------------
1328 */
1329
1330/*
1331 * isdn_tty_get_lsr_info - get line status register info
1332 *
1333 * Purpose: Let user call ioctl() to get info when the UART physically
1334 * is emptied. On bus types like RS485, the transmitter must
1335 * release the bus after transmitting. This must be done when
1336 * the transmit shift register is empty, not be done when the
1337 * transmit holding register is empty. This functionality
1338 * allows RS485 driver to be written in user space.
1339 */
1340static int
Joe Perches475be4d2012-02-19 19:52:38 -08001341isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 u_char status;
1344 uint result;
1345
1346 status = info->lsr;
1347 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1348 return put_user(result, value);
1349}
1350
1351
1352static int
Alan Cox60b33c12011-02-14 16:26:14 +00001353isdn_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 modem_info *info = (modem_info *) tty->driver_data;
1356 u_char control, status;
1357
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001358 if (isdn_tty_paranoia_check(info, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -ENODEV;
1360 if (tty->flags & (1 << TTY_IO_ERROR))
1361 return -EIO;
1362
Arnd Bergmann72250d42010-09-14 09:35:04 +00001363 mutex_lock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364#ifdef ISDN_DEBUG_MODEM_IOCTL
1365 printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
1366#endif
1367
1368 control = info->mcr;
1369 status = info->msr;
Arnd Bergmann72250d42010-09-14 09:35:04 +00001370 mutex_unlock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
Joe Perches475be4d2012-02-19 19:52:38 -08001372 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1373 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1374 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1375 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1376 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
1379static int
Alan Cox20b9d172011-02-14 16:26:50 +00001380isdn_tty_tiocmset(struct tty_struct *tty,
Joe Perches475be4d2012-02-19 19:52:38 -08001381 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 modem_info *info = (modem_info *) tty->driver_data;
1384
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001385 if (isdn_tty_paranoia_check(info, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return -ENODEV;
1387 if (tty->flags & (1 << TTY_IO_ERROR))
1388 return -EIO;
1389
1390#ifdef ISDN_DEBUG_MODEM_IOCTL
1391 printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
1392#endif
1393
Arnd Bergmann72250d42010-09-14 09:35:04 +00001394 mutex_lock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (set & TIOCM_RTS)
1396 info->mcr |= UART_MCR_RTS;
1397 if (set & TIOCM_DTR) {
1398 info->mcr |= UART_MCR_DTR;
1399 isdn_tty_modem_ncarrier(info);
1400 }
1401
1402 if (clear & TIOCM_RTS)
1403 info->mcr &= ~UART_MCR_RTS;
1404 if (clear & TIOCM_DTR) {
1405 info->mcr &= ~UART_MCR_DTR;
1406 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1407 isdn_tty_modem_reset_regs(info, 0);
1408#ifdef ISDN_DEBUG_MODEM_HUP
1409 printk(KERN_DEBUG "Mhup in TIOCMSET\n");
1410#endif
1411 if (info->online)
1412 info->ncarrier = 1;
1413 isdn_tty_modem_hup(info, 1);
1414 }
1415 }
Arnd Bergmann72250d42010-09-14 09:35:04 +00001416 mutex_unlock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
1418}
1419
1420static int
Alan Cox6caa76b2011-02-14 16:27:22 +00001421isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 modem_info *info = (modem_info *) tty->driver_data;
1424 int retval;
1425
1426 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
1427 return -ENODEV;
1428 if (tty->flags & (1 << TTY_IO_ERROR))
1429 return -EIO;
1430 switch (cmd) {
Joe Perches475be4d2012-02-19 19:52:38 -08001431 case TCSBRK: /* SVID version: non-zero arg --> no break */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001433 printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001435 retval = tty_check_change(tty);
1436 if (retval)
1437 return retval;
1438 tty_wait_until_sent(tty, 0);
1439 return 0;
1440 case TCSBRKP: /* support for POSIX tcsendbreak() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001442 printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001444 retval = tty_check_change(tty);
1445 if (retval)
1446 return retval;
1447 tty_wait_until_sent(tty, 0);
1448 return 0;
1449 case TIOCSERGETLSR: /* Get line status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001451 printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001453 return isdn_tty_get_lsr_info(info, (uint __user *) arg);
1454 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001456 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001458 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460 return 0;
1461}
1462
1463static void
Alan Cox606d0992006-12-08 02:38:45 -08001464isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 modem_info *info = (modem_info *) tty->driver_data;
1467
1468 if (!old_termios)
1469 isdn_tty_change_speed(info);
1470 else {
Alan Cox6e4d3762008-04-30 00:53:27 -07001471 if (tty->termios->c_cflag == old_termios->c_cflag &&
1472 tty->termios->c_ispeed == old_termios->c_ispeed &&
1473 tty->termios->c_ospeed == old_termios->c_ospeed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return;
1475 isdn_tty_change_speed(info);
1476 if ((old_termios->c_cflag & CRTSCTS) &&
Alan Cox6e4d3762008-04-30 00:53:27 -07001477 !(tty->termios->c_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480}
1481
1482/*
1483 * ------------------------------------------------------------
1484 * isdn_tty_open() and friends
1485 * ------------------------------------------------------------
1486 */
1487static int
Joe Perches475be4d2012-02-19 19:52:38 -08001488isdn_tty_block_til_ready(struct tty_struct *tty, struct file *filp, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 DECLARE_WAITQUEUE(wait, NULL);
1491 int do_clocal = 0;
1492 int retval;
1493
1494 /*
1495 * If the device is in the middle of being closed, then block
1496 * until it's done, and then try again.
1497 */
1498 if (tty_hung_up_p(filp) ||
Jiri Slaby48decc12012-04-02 13:53:54 +02001499 (info->port.flags & ASYNC_CLOSING)) {
1500 if (info->port.flags & ASYNC_CLOSING)
Jiri Slabyc6e92b62012-04-02 13:53:55 +02001501 interruptible_sleep_on(&info->port.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502#ifdef MODEM_DO_RESTART
Jiri Slaby48decc12012-04-02 13:53:54 +02001503 if (info->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return -EAGAIN;
1505 else
1506 return -ERESTARTSYS;
1507#else
1508 return -EAGAIN;
1509#endif
1510 }
1511 /*
1512 * If non-blocking mode is set, then make the check up front
1513 * and then exit.
1514 */
1515 if ((filp->f_flags & O_NONBLOCK) ||
1516 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby48decc12012-04-02 13:53:54 +02001517 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return 0;
1519 }
Jiri Slaby05eb48b2012-04-02 13:53:49 +02001520 if (tty->termios->c_cflag & CLOCAL)
1521 do_clocal = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /*
1523 * Block waiting for the carrier detect and the line to become
1524 * free (i.e., not in use by the callout). While we are in
1525 * this loop, info->count is dropped by one, so that
1526 * isdn_tty_close() knows when to free things. We restore it upon
1527 * exit, either normal or abnormal.
1528 */
1529 retval = 0;
Jiri Slabyc6e92b62012-04-02 13:53:55 +02001530 add_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531#ifdef ISDN_DEBUG_MODEM_OPEN
1532 printk(KERN_DEBUG "isdn_tty_block_til_ready before block: ttyi%d, count = %d\n",
1533 info->line, info->count);
1534#endif
1535 if (!(tty_hung_up_p(filp)))
Jiri Slaby1b05f032012-04-02 13:53:56 +02001536 info->port.count--;
1537 info->port.blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 while (1) {
1539 set_current_state(TASK_INTERRUPTIBLE);
1540 if (tty_hung_up_p(filp) ||
Jiri Slaby48decc12012-04-02 13:53:54 +02001541 !(info->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542#ifdef MODEM_DO_RESTART
Jiri Slaby48decc12012-04-02 13:53:54 +02001543 if (info->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 retval = -EAGAIN;
1545 else
1546 retval = -ERESTARTSYS;
1547#else
1548 retval = -EAGAIN;
1549#endif
1550 break;
1551 }
Jiri Slaby48decc12012-04-02 13:53:54 +02001552 if (!(info->port.flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 (do_clocal || (info->msr & UART_MSR_DCD))) {
1554 break;
1555 }
1556 if (signal_pending(current)) {
1557 retval = -ERESTARTSYS;
1558 break;
1559 }
1560#ifdef ISDN_DEBUG_MODEM_OPEN
1561 printk(KERN_DEBUG "isdn_tty_block_til_ready blocking: ttyi%d, count = %d\n",
Jiri Slaby1b05f032012-04-02 13:53:56 +02001562 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563#endif
1564 schedule();
1565 }
1566 current->state = TASK_RUNNING;
Jiri Slabyc6e92b62012-04-02 13:53:55 +02001567 remove_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (!tty_hung_up_p(filp))
Jiri Slaby1b05f032012-04-02 13:53:56 +02001569 info->port.count++;
1570 info->port.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#ifdef ISDN_DEBUG_MODEM_OPEN
1572 printk(KERN_DEBUG "isdn_tty_block_til_ready after blocking: ttyi%d, count = %d\n",
Jiri Slaby1b05f032012-04-02 13:53:56 +02001573 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574#endif
1575 if (retval)
1576 return retval;
Jiri Slaby48decc12012-04-02 13:53:54 +02001577 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579}
1580
1581/*
1582 * This routine is called whenever a serial port is opened. It
1583 * enables interrupts for a serial port, linking in its async structure into
1584 * the IRQ chain. It also performs the serial-specific
1585 * initialization for the tty structure.
1586 */
1587static int
1588isdn_tty_open(struct tty_struct *tty, struct file *filp)
1589{
1590 modem_info *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001591 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Jiri Slaby410235f2012-03-05 14:52:01 +01001593 info = &dev->mdm.info[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_open"))
1595 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596#ifdef ISDN_DEBUG_MODEM_OPEN
Joe Perches475be4d2012-02-19 19:52:38 -08001597 printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
Jiri Slaby1b05f032012-04-02 13:53:56 +02001598 info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599#endif
Jiri Slaby1b05f032012-04-02 13:53:56 +02001600 info->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 tty->driver_data = info;
1602 info->tty = tty;
Jiri Slaby48decc12012-04-02 13:53:54 +02001603 tty->port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /*
1605 * Start up serial port
1606 */
1607 retval = isdn_tty_startup(info);
1608 if (retval) {
1609#ifdef ISDN_DEBUG_MODEM_OPEN
1610 printk(KERN_DEBUG "isdn_tty_open return after startup\n");
1611#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return retval;
1613 }
1614 retval = isdn_tty_block_til_ready(tty, filp, info);
1615 if (retval) {
1616#ifdef ISDN_DEBUG_MODEM_OPEN
1617 printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
1618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return retval;
1620 }
1621#ifdef ISDN_DEBUG_MODEM_OPEN
1622 printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
1623#endif
1624 dev->modempoll++;
1625#ifdef ISDN_DEBUG_MODEM_OPEN
1626 printk(KERN_DEBUG "isdn_tty_open normal exit\n");
1627#endif
1628 return 0;
1629}
1630
1631static void
1632isdn_tty_close(struct tty_struct *tty, struct file *filp)
1633{
1634 modem_info *info = (modem_info *) tty->driver_data;
1635 ulong timeout;
1636
1637 if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
1638 return;
1639 if (tty_hung_up_p(filp)) {
1640#ifdef ISDN_DEBUG_MODEM_OPEN
1641 printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
1642#endif
1643 return;
1644 }
Jiri Slaby1b05f032012-04-02 13:53:56 +02001645 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /*
1647 * Uh, oh. tty->count is 1, which means that the tty
1648 * structure will be freed. Info->count should always
1649 * be one in these conditions. If it's greater than
1650 * one, we've got real problems, since it means the
1651 * serial port won't be shutdown.
1652 */
1653 printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
Jiri Slaby1b05f032012-04-02 13:53:56 +02001654 "info->count is %d\n", info->port.count);
1655 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Jiri Slaby1b05f032012-04-02 13:53:56 +02001657 if (--info->port.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
Jiri Slaby1b05f032012-04-02 13:53:56 +02001659 info->line, info->port.count);
1660 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Jiri Slaby1b05f032012-04-02 13:53:56 +02001662 if (info->port.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663#ifdef ISDN_DEBUG_MODEM_OPEN
1664 printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
1665#endif
1666 return;
1667 }
Jiri Slaby48decc12012-04-02 13:53:54 +02001668 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 tty->closing = 1;
1671 /*
1672 * At this point we stop accepting input. To do this, we
1673 * disable the receive line status interrupts, and tell the
1674 * interrupt driver to stop checking the data ready bit in the
1675 * line status register.
1676 */
Jiri Slaby48decc12012-04-02 13:53:54 +02001677 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby0b058352011-08-25 15:12:08 +02001678 tty_wait_until_sent_from_close(tty, 3000); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /*
1680 * Before we drop DTR, make sure the UART transmitter
1681 * has completely drained; this is especially
1682 * important if there is a transmit FIFO!
1683 */
1684 timeout = jiffies + HZ;
1685 while (!(info->lsr & UART_LSR_TEMT)) {
Nishanth Aravamudan24763c42005-11-07 01:01:16 -08001686 schedule_timeout_interruptible(20);
Joe Perches475be4d2012-02-19 19:52:38 -08001687 if (time_after(jiffies, timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689 }
1690 }
1691 dev->modempoll--;
1692 isdn_tty_shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001693 isdn_tty_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 tty_ldisc_flush(tty);
1695 info->tty = NULL;
1696 info->ncarrier = 0;
1697 tty->closing = 0;
Jiri Slaby1b05f032012-04-02 13:53:56 +02001698 if (info->port.blocked_open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 msleep_interruptible(500);
Jiri Slabyc6e92b62012-04-02 13:53:55 +02001700 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Jiri Slaby48decc12012-04-02 13:53:54 +02001702 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slabyc6e92b62012-04-02 13:53:55 +02001703 wake_up_interruptible(&info->port.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704#ifdef ISDN_DEBUG_MODEM_OPEN
1705 printk(KERN_DEBUG "isdn_tty_close normal exit\n");
1706#endif
1707}
1708
1709/*
1710 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1711 */
1712static void
1713isdn_tty_hangup(struct tty_struct *tty)
1714{
1715 modem_info *info = (modem_info *) tty->driver_data;
1716
1717 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
1718 return;
1719 isdn_tty_shutdown(info);
Jiri Slaby1b05f032012-04-02 13:53:56 +02001720 info->port.count = 0;
Jiri Slaby48decc12012-04-02 13:53:54 +02001721 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 info->tty = NULL;
Jiri Slabyc6e92b62012-04-02 13:53:55 +02001723 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726/* This routine initializes all emulator-data.
1727 */
1728static void
Joe Perches475be4d2012-02-19 19:52:38 -08001729isdn_tty_reset_profile(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 m->profile[0] = 0;
1732 m->profile[1] = 0;
1733 m->profile[2] = 43;
1734 m->profile[3] = 13;
1735 m->profile[4] = 10;
1736 m->profile[5] = 8;
1737 m->profile[6] = 3;
1738 m->profile[7] = 60;
1739 m->profile[8] = 2;
1740 m->profile[9] = 6;
1741 m->profile[10] = 7;
1742 m->profile[11] = 70;
1743 m->profile[12] = 0x45;
1744 m->profile[13] = 4;
1745 m->profile[14] = ISDN_PROTO_L2_X75I;
1746 m->profile[15] = ISDN_PROTO_L3_TRANS;
1747 m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
1748 m->profile[17] = ISDN_MODEM_WINSIZE;
1749 m->profile[18] = 4;
1750 m->profile[19] = 0;
1751 m->profile[20] = 0;
1752 m->profile[23] = 0;
1753 m->pmsn[0] = '\0';
1754 m->plmsn[0] = '\0';
1755}
1756
1757#ifdef CONFIG_ISDN_AUDIO
1758static void
Joe Perches475be4d2012-02-19 19:52:38 -08001759isdn_tty_modem_reset_vpar(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 m->vpar[0] = 2; /* Voice-device (2 = phone line) */
1762 m->vpar[1] = 0; /* Silence detection level (0 = none ) */
1763 m->vpar[2] = 70; /* Silence interval (7 sec. ) */
1764 m->vpar[3] = 2; /* Compression type (1 = ADPCM-2 ) */
1765 m->vpar[4] = 0; /* DTMF detection level (0 = softcode ) */
1766 m->vpar[5] = 8; /* DTMF interval (8 * 5 ms. ) */
1767}
1768#endif
1769
1770#ifdef CONFIG_ISDN_TTY_FAX
1771static void
Joe Perches475be4d2012-02-19 19:52:38 -08001772isdn_tty_modem_reset_faxpar(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 T30_s *f = info->fax;
1775
1776 f->code = 0;
1777 f->phase = ISDN_FAX_PHASE_IDLE;
1778 f->direction = 0;
1779 f->resolution = 1; /* fine */
1780 f->rate = 5; /* 14400 bit/s */
1781 f->width = 0;
1782 f->length = 0;
1783 f->compression = 0;
1784 f->ecm = 0;
1785 f->binary = 0;
1786 f->scantime = 0;
1787 memset(&f->id[0], 32, FAXIDLEN - 1);
1788 f->id[FAXIDLEN - 1] = 0;
1789 f->badlin = 0;
1790 f->badmul = 0;
1791 f->bor = 0;
1792 f->nbc = 0;
1793 f->cq = 0;
1794 f->cr = 0;
1795 f->ctcrty = 0;
1796 f->minsp = 0;
1797 f->phcto = 30;
1798 f->rel = 0;
1799 memset(&f->pollid[0], 32, FAXIDLEN - 1);
1800 f->pollid[FAXIDLEN - 1] = 0;
1801}
1802#endif
1803
1804static void
Joe Perches475be4d2012-02-19 19:52:38 -08001805isdn_tty_modem_reset_regs(modem_info *info, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 atemu *m = &info->emu;
1808 if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
1809 memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
1810 memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
1811 memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
1812 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
1813 }
1814#ifdef CONFIG_ISDN_AUDIO
1815 isdn_tty_modem_reset_vpar(m);
1816#endif
1817#ifdef CONFIG_ISDN_TTY_FAX
1818 isdn_tty_modem_reset_faxpar(info);
1819#endif
1820 m->mdmcmdl = 0;
1821}
1822
1823static void
Joe Perches475be4d2012-02-19 19:52:38 -08001824modem_write_profile(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
1827 memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
1828 memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
1829 if (dev->profd)
1830 send_sig(SIGIO, dev->profd, 1);
1831}
1832
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001833static const struct tty_operations modem_ops = {
Joe Perches475be4d2012-02-19 19:52:38 -08001834 .open = isdn_tty_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 .close = isdn_tty_close,
1836 .write = isdn_tty_write,
1837 .flush_chars = isdn_tty_flush_chars,
1838 .write_room = isdn_tty_write_room,
1839 .chars_in_buffer = isdn_tty_chars_in_buffer,
1840 .flush_buffer = isdn_tty_flush_buffer,
1841 .ioctl = isdn_tty_ioctl,
1842 .throttle = isdn_tty_throttle,
1843 .unthrottle = isdn_tty_unthrottle,
1844 .set_termios = isdn_tty_set_termios,
1845 .hangup = isdn_tty_hangup,
1846 .tiocmget = isdn_tty_tiocmget,
1847 .tiocmset = isdn_tty_tiocmset,
1848};
1849
1850int
1851isdn_tty_modem_init(void)
1852{
1853 isdn_modem_t *m;
1854 int i, retval;
1855 modem_info *info;
1856
1857 m = &dev->mdm;
1858 m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
1859 if (!m->tty_modem)
1860 return -ENOMEM;
1861 m->tty_modem->name = "ttyI";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 m->tty_modem->major = ISDN_TTY_MAJOR;
1863 m->tty_modem->minor_start = 0;
1864 m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
1865 m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
1866 m->tty_modem->init_termios = tty_std_termios;
1867 m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001868 m->tty_modem->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 m->tty_modem->driver_name = "isdn_tty";
1870 tty_set_operations(m->tty_modem, &modem_ops);
1871 retval = tty_register_driver(m->tty_modem);
1872 if (retval) {
1873 printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
1874 goto err;
1875 }
1876 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1877 info = &m->info[i];
1878#ifdef CONFIG_ISDN_TTY_FAX
1879 if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
1880 printk(KERN_ERR "Could not allocate fax t30-buffer\n");
1881 retval = -ENOMEM;
1882 goto err_unregister;
1883 }
1884#endif
Jiri Slaby48decc12012-04-02 13:53:54 +02001885 tty_port_init(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 spin_lock_init(&info->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 sprintf(info->last_cause, "0000");
1888 sprintf(info->last_num, "none");
1889 info->last_dir = 0;
1890 info->last_lhup = 1;
1891 info->last_l2 = -1;
1892 info->last_si = 0;
1893 isdn_tty_reset_profile(&info->emu);
1894 isdn_tty_modem_reset_regs(info, 1);
1895 info->magic = ISDN_ASYNC_MAGIC;
1896 info->line = i;
1897 info->tty = NULL;
1898 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 info->isdn_driver = -1;
1900 info->isdn_channel = -1;
1901 info->drv_index = -1;
1902 info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
1903 init_timer(&info->nc_timer);
1904 info->nc_timer.function = isdn_tty_modem_do_ncarrier;
1905 info->nc_timer.data = (unsigned long) info;
1906 skb_queue_head_init(&info->xmit_queue);
1907#ifdef CONFIG_ISDN_AUDIO
1908 skb_queue_head_init(&info->dtmf_queue);
1909#endif
1910 if (!(info->xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5, GFP_KERNEL))) {
1911 printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
1912 retval = -ENOMEM;
1913 goto err_unregister;
1914 }
1915 /* Make room for T.70 header */
1916 info->xmit_buf += 4;
1917 }
1918 return 0;
1919err_unregister:
1920 for (i--; i >= 0; i--) {
1921 info = &m->info[i];
1922#ifdef CONFIG_ISDN_TTY_FAX
1923 kfree(info->fax);
1924#endif
1925 kfree(info->xmit_buf - 4);
1926 }
1927 tty_unregister_driver(m->tty_modem);
Joe Perches475be4d2012-02-19 19:52:38 -08001928err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 put_tty_driver(m->tty_modem);
1930 m->tty_modem = NULL;
1931 return retval;
1932}
1933
1934void
1935isdn_tty_exit(void)
1936{
1937 modem_info *info;
1938 int i;
1939
1940 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1941 info = &dev->mdm.info[i];
1942 isdn_tty_cleanup_xmit(info);
1943#ifdef CONFIG_ISDN_TTY_FAX
1944 kfree(info->fax);
1945#endif
1946 kfree(info->xmit_buf - 4);
1947 }
1948 tty_unregister_driver(dev->mdm.tty_modem);
1949 put_tty_driver(dev->mdm.tty_modem);
1950 dev->mdm.tty_modem = NULL;
1951}
1952
1953
1954/*
1955 * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
1956 * match the MSN against the MSNs (glob patterns) defined for tty_emulator,
1957 * and return 0 for match, 1 for no match, 2 if MSN could match if longer.
1958 */
1959
1960static int
1961isdn_tty_match_icall(char *cid, atemu *emu, int di)
1962{
1963#ifdef ISDN_DEBUG_MODEM_ICALL
1964 printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
1965 emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
1966 emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
1967#endif
1968 if (strlen(emu->lmsn)) {
1969 char *p = emu->lmsn;
1970 char *q;
1971 int tmp;
1972 int ret = 0;
1973
1974 while (1) {
1975 if ((q = strchr(p, ';')))
1976 *q = '\0';
1977 if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
1978 ret = tmp;
1979#ifdef ISDN_DEBUG_MODEM_ICALL
1980 printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
1981 p, isdn_map_eaz2msn(emu->msn, di), tmp);
1982#endif
1983 if (q) {
1984 *q = ';';
1985 p = q;
1986 p++;
1987 }
1988 if (!tmp)
1989 return 0;
1990 if (!q)
1991 break;
1992 }
1993 return ret;
1994 } else {
1995 int tmp;
1996 tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
1997#ifdef ISDN_DEBUG_MODEM_ICALL
Joe Perches475be4d2012-02-19 19:52:38 -08001998 printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
1999 isdn_map_eaz2msn(emu->msn, di), tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000#endif
2001 return tmp;
2002 }
2003}
2004
2005/*
2006 * An incoming call-request has arrived.
2007 * Search the tty-devices for an appropriate device and bind
2008 * it to the ISDN-Channel.
2009 * Return:
2010 *
2011 * 0 = No matching device found.
2012 * 1 = A matching device found.
2013 * 3 = No match found, but eventually would match, if
2014 * CID is longer.
2015 */
2016int
2017isdn_tty_find_icall(int di, int ch, setup_parm *setup)
2018{
2019 char *eaz;
2020 int i;
2021 int wret;
2022 int idx;
2023 int si1;
2024 int si2;
2025 char *nr;
2026 ulong flags;
2027
2028 if (!setup->phone[0]) {
2029 nr = "0";
2030 printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
2031 } else
2032 nr = setup->phone;
2033 si1 = (int) setup->si1;
2034 si2 = (int) setup->si2;
2035 if (!setup->eazmsn[0]) {
2036 printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
2037 eaz = "0";
2038 } else
2039 eaz = setup->eazmsn;
2040#ifdef ISDN_DEBUG_MODEM_ICALL
2041 printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
2042#endif
2043 wret = 0;
2044 spin_lock_irqsave(&dev->lock, flags);
2045 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2046 modem_info *info = &dev->mdm.info[i];
2047
Jiri Slaby1b05f032012-04-02 13:53:56 +02002048 if (info->port.count == 0)
Joe Perches475be4d2012-02-19 19:52:38 -08002049 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) && /* SI1 is matching */
2051 (info->emu.mdmreg[REG_SI2] == si2)) { /* SI2 is matching */
2052 idx = isdn_dc2minor(di, ch);
2053#ifdef ISDN_DEBUG_MODEM_ICALL
2054 printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
2055 printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
Jiri Slaby48decc12012-04-02 13:53:54 +02002056 info->port.flags, info->isdn_driver,
2057 info->isdn_channel, dev->usage[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058#endif
2059 if (
2060#ifndef FIX_FILE_TRANSFER
Jiri Slaby48decc12012-04-02 13:53:54 +02002061 (info->port.flags & ASYNC_NORMAL_ACTIVE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062#endif
2063 (info->isdn_driver == -1) &&
2064 (info->isdn_channel == -1) &&
2065 (USG_NONE(dev->usage[idx]))) {
2066 int matchret;
2067
2068 if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
2069 wret = matchret;
2070 if (!matchret) { /* EAZ is matching */
2071 info->isdn_driver = di;
2072 info->isdn_channel = ch;
2073 info->drv_index = idx;
2074 dev->m_idx[idx] = info->line;
2075 dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
Joe Perches475be4d2012-02-19 19:52:38 -08002076 dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 strcpy(dev->num[idx], nr);
2078 strcpy(info->emu.cpn, eaz);
2079 info->emu.mdmreg[REG_SI1I] = si2bit[si1];
2080 info->emu.mdmreg[REG_PLAN] = setup->plan;
2081 info->emu.mdmreg[REG_SCREEN] = setup->screen;
2082 isdn_info_update();
2083 spin_unlock_irqrestore(&dev->lock, flags);
2084 printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2085 info->line);
2086 info->msr |= UART_MSR_RI;
2087 isdn_tty_modem_result(RESULT_RING, info);
2088 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2089 return 1;
2090 }
2091 }
2092 }
2093 }
2094 spin_unlock_irqrestore(&dev->lock, flags);
2095 printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
Joe Perches475be4d2012-02-19 19:52:38 -08002096 ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
2097 return (wret == 2) ? 3 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Jiri Slaby48decc12012-04-02 13:53:54 +02002100#define TTY_IS_ACTIVE(info) (info->port.flags & ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102int
2103isdn_tty_stat_callback(int i, isdn_ctrl *c)
2104{
2105 int mi;
2106 modem_info *info;
2107 char *e;
2108
2109 if (i < 0)
2110 return 0;
2111 if ((mi = dev->m_idx[i]) >= 0) {
2112 info = &dev->mdm.info[mi];
2113 switch (c->command) {
Joe Perches475be4d2012-02-19 19:52:38 -08002114 case ISDN_STAT_CINF:
2115 printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2116 info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2117 if (e == (char *)c->parm.num)
2118 info->emu.charge = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Joe Perches475be4d2012-02-19 19:52:38 -08002120 break;
2121 case ISDN_STAT_BSENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#ifdef ISDN_TTY_STAT_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08002123 printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002125 if ((info->isdn_driver == c->driver) &&
2126 (info->isdn_channel == c->arg)) {
2127 info->msr |= UART_MSR_CTS;
2128 if (info->send_outstanding)
2129 if (!(--info->send_outstanding))
2130 info->lsr |= UART_LSR_TEMT;
2131 isdn_tty_tint(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return 1;
Joe Perches475be4d2012-02-19 19:52:38 -08002133 }
2134 break;
2135 case ISDN_STAT_CAUSE:
2136#ifdef ISDN_TTY_STAT_DEBUG
2137 printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2138#endif
2139 /* Signal cause to tty-device */
2140 strncpy(info->last_cause, c->parm.num, 5);
2141 return 1;
2142 case ISDN_STAT_DISPLAY:
2143#ifdef ISDN_TTY_STAT_DEBUG
2144 printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2145#endif
2146 /* Signal display to tty-device */
2147 if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2148 !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2149 isdn_tty_at_cout("\r\n", info);
2150 isdn_tty_at_cout("DISPLAY: ", info);
2151 isdn_tty_at_cout(c->parm.display, info);
2152 isdn_tty_at_cout("\r\n", info);
2153 }
2154 return 1;
2155 case ISDN_STAT_DCONN:
2156#ifdef ISDN_TTY_STAT_DEBUG
2157 printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2158#endif
2159 if (TTY_IS_ACTIVE(info)) {
2160 if (info->dialing == 1) {
2161 info->dialing = 2;
2162 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Joe Perches475be4d2012-02-19 19:52:38 -08002164 }
2165 break;
2166 case ISDN_STAT_DHUP:
2167#ifdef ISDN_TTY_STAT_DEBUG
2168 printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2169#endif
2170 if (TTY_IS_ACTIVE(info)) {
2171 if (info->dialing == 1)
2172 isdn_tty_modem_result(RESULT_BUSY, info);
2173 if (info->dialing > 1)
2174 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2175 info->dialing = 0;
2176#ifdef ISDN_DEBUG_MODEM_HUP
2177 printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2178#endif
2179 isdn_tty_modem_hup(info, 0);
2180 return 1;
2181 }
2182 break;
2183 case ISDN_STAT_BCONN:
2184#ifdef ISDN_TTY_STAT_DEBUG
2185 printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2186#endif
2187 /* Wake up any processes waiting
2188 * for incoming call of this device when
2189 * DCD follow the state of incoming carrier
2190 */
Jiri Slaby1b05f032012-04-02 13:53:56 +02002191 if (info->port.blocked_open &&
Joe Perches475be4d2012-02-19 19:52:38 -08002192 (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
Jiri Slabyc6e92b62012-04-02 13:53:55 +02002193 wake_up_interruptible(&info->port.open_wait);
Joe Perches475be4d2012-02-19 19:52:38 -08002194 }
2195
2196 /* Schedule CONNECT-Message to any tty
2197 * waiting for it and
2198 * set DCD-bit of its modem-status.
2199 */
2200 if (TTY_IS_ACTIVE(info) ||
Jiri Slaby1b05f032012-04-02 13:53:56 +02002201 (info->port.blocked_open &&
2202 (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
Joe Perches475be4d2012-02-19 19:52:38 -08002203 info->msr |= UART_MSR_DCD;
2204 info->emu.charge = 0;
2205 if (info->dialing & 0xf)
2206 info->last_dir = 1;
2207 else
2208 info->last_dir = 0;
2209 info->dialing = 0;
2210 info->rcvsched = 1;
2211 if (USG_MODEM(dev->usage[i])) {
2212 if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2213 strcpy(info->emu.connmsg, c->parm.num);
2214 isdn_tty_modem_result(RESULT_CONNECT, info);
2215 } else
2216 isdn_tty_modem_result(RESULT_CONNECT64000, info);
2217 }
2218 if (USG_VOICE(dev->usage[i]))
2219 isdn_tty_modem_result(RESULT_VCON, info);
2220 return 1;
2221 }
2222 break;
2223 case ISDN_STAT_BHUP:
2224#ifdef ISDN_TTY_STAT_DEBUG
2225 printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2226#endif
2227 if (TTY_IS_ACTIVE(info)) {
2228#ifdef ISDN_DEBUG_MODEM_HUP
2229 printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2230#endif
2231 isdn_tty_modem_hup(info, 0);
2232 return 1;
2233 }
2234 break;
2235 case ISDN_STAT_NODCH:
2236#ifdef ISDN_TTY_STAT_DEBUG
2237 printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2238#endif
2239 if (TTY_IS_ACTIVE(info)) {
2240 if (info->dialing) {
2241 info->dialing = 0;
2242 info->last_l2 = -1;
2243 info->last_si = 0;
2244 sprintf(info->last_cause, "0000");
2245 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2246 }
2247 isdn_tty_modem_hup(info, 0);
2248 return 1;
2249 }
2250 break;
2251 case ISDN_STAT_UNLOAD:
2252#ifdef ISDN_TTY_STAT_DEBUG
2253 printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2254#endif
2255 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2256 info = &dev->mdm.info[i];
2257 if (info->isdn_driver == c->driver) {
2258 if (info->online)
2259 isdn_tty_modem_hup(info, 1);
2260 }
2261 }
2262 return 1;
2263#ifdef CONFIG_ISDN_TTY_FAX
2264 case ISDN_STAT_FAXIND:
2265 if (TTY_IS_ACTIVE(info)) {
2266 isdn_tty_fax_command(info, c);
2267 }
2268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269#endif
2270#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002271 case ISDN_STAT_AUDIO:
2272 if (TTY_IS_ACTIVE(info)) {
2273 switch (c->parm.num[0]) {
2274 case ISDN_AUDIO_DTMF:
2275 if (info->vonline) {
2276 isdn_audio_put_dle_code(info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 c->parm.num[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Joe Perches475be4d2012-02-19 19:52:38 -08002279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
Joe Perches475be4d2012-02-19 19:52:38 -08002281 }
2282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283#endif
2284 }
2285 }
2286 return 0;
2287}
2288
2289/*********************************************************************
2290 Modem-Emulator-Routines
Joe Perches475be4d2012-02-19 19:52:38 -08002291*********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Joe Perches475be4d2012-02-19 19:52:38 -08002293#define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295/*
2296 * Put a message from the AT-emulator into receive-buffer of tty,
2297 * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2298 */
2299void
Joe Perches475be4d2012-02-19 19:52:38 -08002300isdn_tty_at_cout(char *msg, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
2302 struct tty_struct *tty;
2303 atemu *m = &info->emu;
2304 char *p;
2305 char c;
2306 u_long flags;
2307 struct sk_buff *skb = NULL;
2308 char *sp = NULL;
Adrian Bunk1f4d4a82006-03-25 03:08:06 -08002309 int l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 if (!msg) {
2312 printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2313 return;
2314 }
Adrian Bunk1f4d4a82006-03-25 03:08:06 -08002315
2316 l = strlen(msg);
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 spin_lock_irqsave(&info->readlock, flags);
2319 tty = info->tty;
Jiri Slaby48decc12012-04-02 13:53:54 +02002320 if ((info->port.flags & ASYNC_CLOSING) || (!tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 spin_unlock_irqrestore(&info->readlock, flags);
2322 return;
2323 }
2324
Alan Cox33f0f882006-01-09 20:54:13 -08002325 /* use queue instead of direct, if online and */
2326 /* data is in queue or buffer is full */
Karsten Keil61b9a26a2006-02-14 13:53:06 -08002327 if (info->online && ((tty_buffer_request_room(tty, l) < l) ||
Joe Perches475be4d2012-02-19 19:52:38 -08002328 !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
Alan Cox33f0f882006-01-09 20:54:13 -08002329 skb = alloc_skb(l, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!skb) {
2331 spin_unlock_irqrestore(&info->readlock, flags);
2332 return;
2333 }
Alan Cox33f0f882006-01-09 20:54:13 -08002334 sp = skb_put(skb, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335#ifdef CONFIG_ISDN_AUDIO
2336 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2337 ISDN_AUDIO_SKB_LOCK(skb) = 0;
2338#endif
2339 }
2340
2341 for (p = msg; *p; p++) {
2342 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -08002343 case '\r':
2344 c = m->mdmreg[REG_CR];
2345 break;
2346 case '\n':
2347 c = m->mdmreg[REG_LF];
2348 break;
2349 case '\b':
2350 c = m->mdmreg[REG_BS];
2351 break;
2352 default:
2353 c = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355 if (skb) {
2356 *sp++ = c;
2357 } else {
Joe Perches475be4d2012-02-19 19:52:38 -08002358 if (tty_insert_flip_char(tty, c, TTY_NORMAL) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 }
2362 if (skb) {
2363 __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2364 dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2365 spin_unlock_irqrestore(&info->readlock, flags);
2366 /* Schedule dequeuing */
Alan Cox33f0f882006-01-09 20:54:13 -08002367 if (dev->modempoll && info->rcvsched)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2369
2370 } else {
2371 spin_unlock_irqrestore(&info->readlock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -08002372 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374}
2375
2376/*
2377 * Perform ATH Hangup
2378 */
2379static void
Joe Perches475be4d2012-02-19 19:52:38 -08002380isdn_tty_on_hook(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 if (info->isdn_channel >= 0) {
2383#ifdef ISDN_DEBUG_MODEM_HUP
2384 printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2385#endif
2386 isdn_tty_modem_hup(info, 1);
2387 }
2388}
2389
2390static void
2391isdn_tty_off_hook(void)
2392{
2393 printk(KERN_DEBUG "isdn_tty_off_hook\n");
2394}
2395
Joe Perches475be4d2012-02-19 19:52:38 -08002396#define PLUSWAIT1 (HZ / 2) /* 0.5 sec. */
2397#define PLUSWAIT2 (HZ * 3 / 2) /* 1.5 sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399/*
2400 * Check Buffer for Modem-escape-sequence, activate timer-callback to
2401 * isdn_tty_modem_escape() if sequence found.
2402 *
2403 * Parameters:
2404 * p pointer to databuffer
2405 * plus escape-character
2406 * count length of buffer
2407 * pluscount count of valid escape-characters so far
2408 * lastplus timestamp of last character
2409 */
2410static void
Joe Perches475be4d2012-02-19 19:52:38 -08002411isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 u_long *lastplus)
2413{
2414 if (plus > 127)
2415 return;
2416 if (count > 3) {
2417 p += count - 3;
2418 count = 3;
2419 *pluscount = 0;
2420 }
2421 while (count > 0) {
2422 if (*(p++) == plus) {
2423 if ((*pluscount)++) {
2424 /* Time since last '+' > 0.5 sec. ? */
2425 if (time_after(jiffies, *lastplus + PLUSWAIT1))
2426 *pluscount = 1;
2427 } else {
2428 /* Time since last non-'+' < 1.5 sec. ? */
2429 if (time_before(jiffies, *lastplus + PLUSWAIT2))
2430 *pluscount = 0;
2431 }
2432 if ((*pluscount == 3) && (count == 1))
2433 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2434 if (*pluscount > 3)
2435 *pluscount = 1;
2436 } else
2437 *pluscount = 0;
2438 *lastplus = jiffies;
2439 count--;
2440 }
2441}
2442
2443/*
2444 * Return result of AT-emulator to tty-receive-buffer, depending on
2445 * modem-register 12, bit 0 and 1.
2446 * For CONNECT-messages also switch to online-mode.
2447 * For RING-message handle auto-ATA if register 0 != 0
2448 */
2449
2450static void
Joe Perches475be4d2012-02-19 19:52:38 -08002451isdn_tty_modem_result(int code, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
2453 atemu *m = &info->emu;
2454 static char *msg[] =
Joe Perches475be4d2012-02-19 19:52:38 -08002455 {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2456 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2457 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2458 char s[ISDN_MSNLEN + 10];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 switch (code) {
Joe Perches475be4d2012-02-19 19:52:38 -08002461 case RESULT_RING:
2462 m->mdmreg[REG_RINGCNT]++;
2463 if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2464 /* Automatically accept incoming call */
2465 isdn_tty_cmd_ATA(info);
2466 break;
2467 case RESULT_NO_CARRIER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468#ifdef ISDN_DEBUG_MODEM_HUP
Joe Perches475be4d2012-02-19 19:52:38 -08002469 printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
Jiri Slaby48decc12012-04-02 13:53:54 +02002470 (info->port.flags & ASYNC_CLOSING),
Joe Perches475be4d2012-02-19 19:52:38 -08002471 (!info->tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002473 m->mdmreg[REG_RINGCNT] = 0;
2474 del_timer(&info->nc_timer);
2475 info->ncarrier = 0;
Jiri Slaby48decc12012-04-02 13:53:54 +02002476 if ((info->port.flags & ASYNC_CLOSING) || (!info->tty))
Joe Perches475be4d2012-02-19 19:52:38 -08002477 return;
Jiri Slaby6776a2f2012-04-02 13:53:50 +02002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002480 if (info->vonline & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481#ifdef ISDN_DEBUG_MODEM_VOICE
Joe Perches475be4d2012-02-19 19:52:38 -08002482 printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 info->line);
2484#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002485 /* voice-recording, add DLE-ETX */
2486 isdn_tty_at_cout("\020\003", info);
2487 }
2488 if (info->vonline & 2) {
2489#ifdef ISDN_DEBUG_MODEM_VOICE
2490 printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
2491 info->line);
2492#endif
2493 /* voice-playing, add DLE-DC4 */
2494 isdn_tty_at_cout("\020\024", info);
2495 }
2496#endif
2497 break;
2498 case RESULT_CONNECT:
2499 case RESULT_CONNECT64000:
2500 sprintf(info->last_cause, "0000");
2501 if (!info->online)
2502 info->online = 2;
2503 break;
2504 case RESULT_VCON:
2505#ifdef ISDN_DEBUG_MODEM_VOICE
2506 printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
2507 info->line);
2508#endif
2509 sprintf(info->last_cause, "0000");
2510 if (!info->online)
2511 info->online = 1;
2512 break;
2513 } /* switch (code) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 if (m->mdmreg[REG_RESP] & BIT_RESP) {
2516 /* Show results */
2517 if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
2518 /* Show numeric results only */
2519 sprintf(s, "\r\n%d\r\n", code);
2520 isdn_tty_at_cout(s, info);
2521 } else {
2522 if (code == RESULT_RING) {
Joe Perches475be4d2012-02-19 19:52:38 -08002523 /* return if "show RUNG" and ringcounter>1 */
2524 if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 (m->mdmreg[REG_RINGCNT] > 1))
Joe Perches475be4d2012-02-19 19:52:38 -08002526 return;
2527 /* print CID, _before_ _every_ ring */
2528 if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
2529 isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
2530 isdn_tty_at_cout(dev->num[info->drv_index], info);
2531 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2532 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2533 isdn_tty_at_cout(info->emu.cpn, info);
2534 }
2535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537 isdn_tty_at_cout("\r\n", info);
2538 isdn_tty_at_cout(msg[code], info);
2539 switch (code) {
Joe Perches475be4d2012-02-19 19:52:38 -08002540 case RESULT_CONNECT:
2541 switch (m->mdmreg[REG_L2PROT]) {
2542 case ISDN_PROTO_L2_MODEM:
2543 isdn_tty_at_cout(" ", info);
2544 isdn_tty_at_cout(m->connmsg, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002546 }
2547 break;
2548 case RESULT_RING:
2549 /* Append CPN, if enabled */
2550 if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
2551 sprintf(s, "/%s", m->cpn);
2552 isdn_tty_at_cout(s, info);
2553 }
2554 /* Print CID only once, _after_ 1st RING */
2555 if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
2556 (m->mdmreg[REG_RINGCNT] == 1)) {
2557 isdn_tty_at_cout("\r\n", info);
2558 isdn_tty_at_cout("CALLER NUMBER: ", info);
2559 isdn_tty_at_cout(dev->num[info->drv_index], info);
2560 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2561 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2562 isdn_tty_at_cout(info->emu.cpn, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
Joe Perches475be4d2012-02-19 19:52:38 -08002564 }
2565 break;
2566 case RESULT_NO_CARRIER:
2567 case RESULT_NO_DIALTONE:
2568 case RESULT_BUSY:
2569 case RESULT_NO_ANSWER:
2570 m->mdmreg[REG_RINGCNT] = 0;
2571 /* Append Cause-Message if enabled */
2572 if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
2573 sprintf(s, "/%s", info->last_cause);
2574 isdn_tty_at_cout(s, info);
2575 }
2576 break;
2577 case RESULT_CONNECT64000:
2578 /* Append Protocol to CONNECT message */
2579 switch (m->mdmreg[REG_L2PROT]) {
2580 case ISDN_PROTO_L2_X75I:
2581 case ISDN_PROTO_L2_X75UI:
2582 case ISDN_PROTO_L2_X75BUI:
2583 isdn_tty_at_cout("/X.75", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002585 case ISDN_PROTO_L2_HDLC:
2586 isdn_tty_at_cout("/HDLC", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002588 case ISDN_PROTO_L2_V11096:
2589 isdn_tty_at_cout("/V110/9600", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002591 case ISDN_PROTO_L2_V11019:
2592 isdn_tty_at_cout("/V110/19200", info);
2593 break;
2594 case ISDN_PROTO_L2_V11038:
2595 isdn_tty_at_cout("/V110/38400", info);
2596 break;
2597 }
2598 if (m->mdmreg[REG_T70] & BIT_T70) {
2599 isdn_tty_at_cout("/T.70", info);
2600 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2601 isdn_tty_at_cout("+", info);
2602 }
2603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605 isdn_tty_at_cout("\r\n", info);
2606 }
2607 }
2608 if (code == RESULT_NO_CARRIER) {
Jiri Slaby48decc12012-04-02 13:53:54 +02002609 if ((info->port.flags & ASYNC_CLOSING) || (!info->tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return;
Jiri Slaby6776a2f2012-04-02 13:53:50 +02002611
Jiri Slaby48decc12012-04-02 13:53:54 +02002612 if (info->port.flags & ASYNC_CHECK_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 tty_hangup(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615}
2616
2617
2618/*
2619 * Display a modem-register-value.
2620 */
2621static void
Joe Perches475be4d2012-02-19 19:52:38 -08002622isdn_tty_show_profile(int ridx, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
2624 char v[6];
2625
2626 sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
2627 isdn_tty_at_cout(v, info);
2628}
2629
2630/*
2631 * Get MSN-string from char-pointer, set pointer to end of number
2632 */
2633static void
2634isdn_tty_get_msnstr(char *n, char **p)
2635{
2636 int limit = ISDN_MSNLEN - 1;
2637
2638 while (((*p[0] >= '0' && *p[0] <= '9') ||
2639 /* Why a comma ??? */
2640 (*p[0] == ',') || (*p[0] == ':')) &&
Joe Perches475be4d2012-02-19 19:52:38 -08002641 (limit--))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 *n++ = *p[0]++;
2643 *n = '\0';
2644}
2645
2646/*
2647 * Get phone-number from modem-commandbuffer
2648 */
2649static void
Joe Perches475be4d2012-02-19 19:52:38 -08002650isdn_tty_getdial(char *p, char *q, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 int first = 1;
2653 int limit = ISDN_MSNLEN - 1; /* MUST match the size of interface var to avoid
Joe Perches475be4d2012-02-19 19:52:38 -08002654 buffer overflow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Joe Perches475be4d2012-02-19 19:52:38 -08002656 while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
Karsten Keil924ad152007-06-01 00:46:55 -07002658 ((*p == 'R') && first) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 (*p == '*') || (*p == '#')) {
2660 *q++ = *p;
2661 limit--;
2662 }
Joe Perches475be4d2012-02-19 19:52:38 -08002663 if (!limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 break;
2665 p++;
2666 first = 0;
2667 }
2668 *q = 0;
2669}
2670
2671#define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
2672#define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
2673
2674static void
Joe Perches475be4d2012-02-19 19:52:38 -08002675isdn_tty_report(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
2677 atemu *m = &info->emu;
2678 char s[80];
2679
2680 isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
2681 sprintf(s, " Remote Number: %s\r\n", info->last_num);
2682 isdn_tty_at_cout(s, info);
2683 sprintf(s, " Direction: %s\r\n", info->last_dir ? "outgoing" : "incoming");
2684 isdn_tty_at_cout(s, info);
2685 isdn_tty_at_cout(" Layer-2 Protocol: ", info);
2686 switch (info->last_l2) {
Joe Perches475be4d2012-02-19 19:52:38 -08002687 case ISDN_PROTO_L2_X75I:
2688 isdn_tty_at_cout("X.75i", info);
2689 break;
2690 case ISDN_PROTO_L2_X75UI:
2691 isdn_tty_at_cout("X.75ui", info);
2692 break;
2693 case ISDN_PROTO_L2_X75BUI:
2694 isdn_tty_at_cout("X.75bui", info);
2695 break;
2696 case ISDN_PROTO_L2_HDLC:
2697 isdn_tty_at_cout("HDLC", info);
2698 break;
2699 case ISDN_PROTO_L2_V11096:
2700 isdn_tty_at_cout("V.110 9600 Baud", info);
2701 break;
2702 case ISDN_PROTO_L2_V11019:
2703 isdn_tty_at_cout("V.110 19200 Baud", info);
2704 break;
2705 case ISDN_PROTO_L2_V11038:
2706 isdn_tty_at_cout("V.110 38400 Baud", info);
2707 break;
2708 case ISDN_PROTO_L2_TRANS:
2709 isdn_tty_at_cout("transparent", info);
2710 break;
2711 case ISDN_PROTO_L2_MODEM:
2712 isdn_tty_at_cout("modem", info);
2713 break;
2714 case ISDN_PROTO_L2_FAX:
2715 isdn_tty_at_cout("fax", info);
2716 break;
2717 default:
2718 isdn_tty_at_cout("unknown", info);
2719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721 if (m->mdmreg[REG_T70] & BIT_T70) {
2722 isdn_tty_at_cout("/T.70", info);
2723 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2724 isdn_tty_at_cout("+", info);
2725 }
2726 isdn_tty_at_cout("\r\n", info);
2727 isdn_tty_at_cout(" Service: ", info);
2728 switch (info->last_si) {
Joe Perches475be4d2012-02-19 19:52:38 -08002729 case 1:
2730 isdn_tty_at_cout("audio\r\n", info);
2731 break;
2732 case 5:
2733 isdn_tty_at_cout("btx\r\n", info);
2734 break;
2735 case 7:
2736 isdn_tty_at_cout("data\r\n", info);
2737 break;
2738 default:
2739 sprintf(s, "%d\r\n", info->last_si);
2740 isdn_tty_at_cout(s, info);
2741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743 sprintf(s, " Hangup location: %s\r\n", info->last_lhup ? "local" : "remote");
2744 isdn_tty_at_cout(s, info);
2745 sprintf(s, " Last cause: %s\r\n", info->last_cause);
2746 isdn_tty_at_cout(s, info);
2747}
2748
2749/*
2750 * Parse AT&.. commands.
2751 */
2752static int
Joe Perches475be4d2012-02-19 19:52:38 -08002753isdn_tty_cmd_ATand(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 atemu *m = &info->emu;
2756 int i;
2757 char rb[100];
2758
2759#define MAXRB (sizeof(rb) - 1)
2760
2761 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08002762 case 'B':
2763 /* &B - Set Buffersize */
2764 p[0]++;
2765 i = isdn_getnum(p);
2766 if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
2767 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002769 if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
2770 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002772 m->mdmreg[REG_PSIZE] = i / 16;
2773 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2774 switch (m->mdmreg[REG_L2PROT]) {
2775 case ISDN_PROTO_L2_V11096:
2776 case ISDN_PROTO_L2_V11019:
2777 case ISDN_PROTO_L2_V11038:
2778 info->xmit_size /= 10;
2779 }
2780 break;
2781 case 'C':
2782 /* &C - DCD Status */
2783 p[0]++;
2784 switch (isdn_getnum(p)) {
2785 case 0:
2786 m->mdmreg[REG_DCD] &= ~BIT_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002788 case 1:
2789 m->mdmreg[REG_DCD] |= BIT_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002791 default:
2792 PARSE_ERROR1
2793 }
2794 break;
2795 case 'D':
2796 /* &D - Set DTR-Low-behavior */
2797 p[0]++;
2798 switch (isdn_getnum(p)) {
2799 case 0:
2800 m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
2801 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002803 case 2:
2804 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2805 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002807 case 3:
2808 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2809 m->mdmreg[REG_DTRR] |= BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002811 default:
2812 PARSE_ERROR1
2813 }
2814 break;
2815 case 'E':
2816 /* &E -Set EAZ/MSN */
2817 p[0]++;
2818 isdn_tty_get_msnstr(m->msn, p);
2819 break;
2820 case 'F':
2821 /* &F -Set Factory-Defaults */
2822 p[0]++;
2823 if (info->msr & UART_MSR_DCD)
2824 PARSE_ERROR1;
2825 isdn_tty_reset_profile(m);
2826 isdn_tty_modem_reset_regs(info, 1);
2827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828#ifdef DUMMY_HAYES_AT
Joe Perches475be4d2012-02-19 19:52:38 -08002829 case 'K':
2830 /* only for be compilant with common scripts */
2831 /* &K Flowcontrol - no function */
2832 p[0]++;
2833 isdn_getnum(p);
2834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002836 case 'L':
2837 /* &L -Set Numbers to listen on */
2838 p[0]++;
2839 i = 0;
2840 while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
2841 (i < ISDN_LMSNLEN - 1))
2842 m->lmsn[i++] = *p[0]++;
2843 m->lmsn[i] = '\0';
2844 break;
2845 case 'R':
2846 /* &R - Set V.110 bitrate adaption */
2847 p[0]++;
2848 i = isdn_getnum(p);
2849 switch (i) {
2850 case 0:
2851 /* Switch off V.110, back to X.75 */
2852 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2853 m->mdmreg[REG_SI2] = 0;
2854 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002856 case 9600:
2857 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
2858 m->mdmreg[REG_SI2] = 197;
2859 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002861 case 19200:
2862 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
2863 m->mdmreg[REG_SI2] = 199;
2864 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002866 case 38400:
2867 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
2868 m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
2869 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 break;
2871 default:
2872 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08002873 }
2874 /* Switch off T.70 */
2875 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2876 /* Set Service 7 */
2877 m->mdmreg[REG_SI1] |= 4;
2878 break;
2879 case 'S':
2880 /* &S - Set Windowsize */
2881 p[0]++;
2882 i = isdn_getnum(p);
2883 if ((i > 0) && (i < 9))
2884 m->mdmreg[REG_WSIZE] = i;
2885 else
2886 PARSE_ERROR1;
2887 break;
2888 case 'V':
2889 /* &V - Show registers */
2890 p[0]++;
2891 isdn_tty_at_cout("\r\n", info);
2892 for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
2893 sprintf(rb, "S%02d=%03d%s", i,
2894 m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
2895 isdn_tty_at_cout(rb, info);
2896 }
2897 sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
2898 strlen(m->msn) ? m->msn : "None");
2899 isdn_tty_at_cout(rb, info);
2900 if (strlen(m->lmsn)) {
2901 isdn_tty_at_cout("\r\nListen: ", info);
2902 isdn_tty_at_cout(m->lmsn, info);
2903 isdn_tty_at_cout("\r\n", info);
2904 }
2905 break;
2906 case 'W':
2907 /* &W - Write Profile */
2908 p[0]++;
2909 switch (*p[0]) {
2910 case '0':
2911 p[0]++;
2912 modem_write_profile(m);
2913 break;
2914 default:
2915 PARSE_ERROR1;
2916 }
2917 break;
2918 case 'X':
2919 /* &X - Switch to BTX-Mode and T.70 */
2920 p[0]++;
2921 switch (isdn_getnum(p)) {
2922 case 0:
2923 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2924 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2925 break;
2926 case 1:
2927 m->mdmreg[REG_T70] |= BIT_T70;
2928 m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
2929 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2930 info->xmit_size = 112;
2931 m->mdmreg[REG_SI1] = 4;
2932 m->mdmreg[REG_SI2] = 0;
2933 break;
2934 case 2:
2935 m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
2936 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2937 info->xmit_size = 112;
2938 m->mdmreg[REG_SI1] = 4;
2939 m->mdmreg[REG_SI2] = 0;
2940 break;
2941 default:
2942 PARSE_ERROR1;
2943 }
2944 break;
2945 default:
2946 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948 return 0;
2949}
2950
2951static int
Joe Perches475be4d2012-02-19 19:52:38 -08002952isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 /* Some plausibility checks */
2955 switch (mreg) {
Joe Perches475be4d2012-02-19 19:52:38 -08002956 case REG_L2PROT:
2957 if (mval > ISDN_PROTO_L2_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return 1;
Joe Perches475be4d2012-02-19 19:52:38 -08002959 break;
2960 case REG_PSIZE:
2961 if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
2962 return 1;
2963#ifdef CONFIG_ISDN_AUDIO
2964 if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
2965 return 1;
2966#endif
2967 info->xmit_size = mval * 16;
2968 switch (m->mdmreg[REG_L2PROT]) {
2969 case ISDN_PROTO_L2_V11096:
2970 case ISDN_PROTO_L2_V11019:
2971 case ISDN_PROTO_L2_V11038:
2972 info->xmit_size /= 10;
2973 }
2974 break;
2975 case REG_SI1I:
2976 case REG_PLAN:
2977 case REG_SCREEN:
2978 /* readonly registers */
2979 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981 return 0;
2982}
2983
2984/*
2985 * Perform ATS command
2986 */
2987static int
Joe Perches475be4d2012-02-19 19:52:38 -08002988isdn_tty_cmd_ATS(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
2990 atemu *m = &info->emu;
2991 int bitpos;
2992 int mreg;
2993 int mval;
2994 int bval;
2995
2996 mreg = isdn_getnum(p);
2997 if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
2998 PARSE_ERROR1;
2999 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003000 case '=':
3001 p[0]++;
3002 mval = isdn_getnum(p);
3003 if (mval < 0 || mval > 255)
3004 PARSE_ERROR1;
3005 if (isdn_tty_check_ats(mreg, mval, info, m))
3006 PARSE_ERROR1;
3007 m->mdmreg[mreg] = mval;
3008 break;
3009 case '.':
3010 /* Set/Clear a single bit */
3011 p[0]++;
3012 bitpos = isdn_getnum(p);
3013 if ((bitpos < 0) || (bitpos > 7))
3014 PARSE_ERROR1;
3015 switch (*p[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 case '=':
3017 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08003018 bval = isdn_getnum(p);
3019 if (bval < 0 || bval > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003021 if (bval)
3022 mval = m->mdmreg[mreg] | (1 << bitpos);
3023 else
3024 mval = m->mdmreg[mreg] & ~(1 << bitpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (isdn_tty_check_ats(mreg, mval, info, m))
3026 PARSE_ERROR1;
3027 m->mdmreg[mreg] = mval;
3028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 case '?':
3030 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08003031 isdn_tty_at_cout("\r\n", info);
3032 isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
3033 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 break;
3035 default:
3036 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003037 }
3038 break;
3039 case '?':
3040 p[0]++;
3041 isdn_tty_show_profile(mreg, info);
3042 break;
3043 default:
3044 PARSE_ERROR1;
3045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047 return 0;
3048}
3049
3050/*
3051 * Perform ATA command
3052 */
3053static void
Joe Perches475be4d2012-02-19 19:52:38 -08003054isdn_tty_cmd_ATA(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 atemu *m = &info->emu;
3057 isdn_ctrl cmd;
3058 int l2;
3059
3060 if (info->msr & UART_MSR_RI) {
3061 /* Accept incoming call */
3062 info->last_dir = 0;
3063 strcpy(info->last_num, dev->num[info->drv_index]);
3064 m->mdmreg[REG_RINGCNT] = 0;
3065 info->msr &= ~UART_MSR_RI;
3066 l2 = m->mdmreg[REG_L2PROT];
3067#ifdef CONFIG_ISDN_AUDIO
3068 /* If more than one bit set in reg18, autoselect Layer2 */
3069 if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
3070 if (m->mdmreg[REG_SI1I] == 1) {
3071 if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
3072 l2 = ISDN_PROTO_L2_TRANS;
3073 } else
3074 l2 = ISDN_PROTO_L2_X75I;
3075 }
3076#endif
3077 cmd.driver = info->isdn_driver;
3078 cmd.command = ISDN_CMD_SETL2;
3079 cmd.arg = info->isdn_channel + (l2 << 8);
3080 info->last_l2 = l2;
3081 isdn_command(&cmd);
3082 cmd.driver = info->isdn_driver;
3083 cmd.command = ISDN_CMD_SETL3;
3084 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
3085#ifdef CONFIG_ISDN_TTY_FAX
3086 if (l2 == ISDN_PROTO_L2_FAX) {
3087 cmd.parm.fax = info->fax;
3088 info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3089 }
3090#endif
3091 isdn_command(&cmd);
3092 cmd.driver = info->isdn_driver;
3093 cmd.arg = info->isdn_channel;
3094 cmd.command = ISDN_CMD_ACCEPTD;
3095 info->dialing = 16;
3096 info->emu.carrierwait = 0;
3097 isdn_command(&cmd);
3098 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3099 } else
3100 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3101}
3102
3103#ifdef CONFIG_ISDN_AUDIO
3104/*
3105 * Parse AT+F.. commands
3106 */
3107static int
Joe Perches475be4d2012-02-19 19:52:38 -08003108isdn_tty_cmd_PLUSF(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 atemu *m = &info->emu;
3111 char rs[20];
3112
3113 if (!strncmp(p[0], "CLASS", 5)) {
3114 p[0] += 5;
3115 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003116 case '?':
3117 p[0]++;
3118 sprintf(rs, "\r\n%d",
3119 (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3120#ifdef CONFIG_ISDN_TTY_FAX
3121 if (TTY_IS_FCLASS2(info))
3122 sprintf(rs, "\r\n2");
3123 else if (TTY_IS_FCLASS1(info))
3124 sprintf(rs, "\r\n1");
3125#endif
3126 isdn_tty_at_cout(rs, info);
3127 break;
3128 case '=':
3129 p[0]++;
3130 switch (*p[0]) {
3131 case '0':
3132 p[0]++;
3133 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3134 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3135 m->mdmreg[REG_SI1] = 4;
3136 info->xmit_size =
3137 m->mdmreg[REG_PSIZE] * 16;
3138 break;
3139#ifdef CONFIG_ISDN_TTY_FAX
3140 case '1':
3141 p[0]++;
3142 if (!(dev->global_features &
3143 ISDN_FEATURE_L3_FCLASS1))
3144 PARSE_ERROR1;
3145 m->mdmreg[REG_SI1] = 1;
3146 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3147 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3148 info->xmit_size =
3149 m->mdmreg[REG_PSIZE] * 16;
3150 break;
3151 case '2':
3152 p[0]++;
3153 if (!(dev->global_features &
3154 ISDN_FEATURE_L3_FCLASS2))
3155 PARSE_ERROR1;
3156 m->mdmreg[REG_SI1] = 1;
3157 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3158 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3159 info->xmit_size =
3160 m->mdmreg[REG_PSIZE] * 16;
3161 break;
3162#endif
3163 case '8':
3164 p[0]++;
3165 /* L2 will change on dialout with si=1 */
3166 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3167 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3168 m->mdmreg[REG_SI1] = 5;
3169 info->xmit_size = VBUF;
3170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 case '?':
3172 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08003173 strcpy(rs, "\r\n0,");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -08003175 if (dev->global_features &
3176 ISDN_FEATURE_L3_FCLASS1)
3177 strcat(rs, "1,");
3178 if (dev->global_features &
3179 ISDN_FEATURE_L3_FCLASS2)
3180 strcat(rs, "2,");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181#endif
Joe Perches475be4d2012-02-19 19:52:38 -08003182 strcat(rs, "8");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 isdn_tty_at_cout(rs, info);
3184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 default:
3186 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003187 }
3188 break;
3189 default:
3190 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192 return 0;
3193 }
3194#ifdef CONFIG_ISDN_TTY_FAX
3195 return (isdn_tty_cmd_PLUSF_FAX(p, info));
3196#else
3197 PARSE_ERROR1;
3198#endif
3199}
3200
3201/*
3202 * Parse AT+V.. commands
3203 */
3204static int
Joe Perches475be4d2012-02-19 19:52:38 -08003205isdn_tty_cmd_PLUSV(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 atemu *m = &info->emu;
3208 isdn_ctrl cmd;
3209 static char *vcmd[] =
Joe Perches475be4d2012-02-19 19:52:38 -08003210 {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 int i;
3212 int par1;
3213 int par2;
3214 char rs[20];
3215
3216 i = 0;
3217 while (vcmd[i]) {
3218 if (!strncmp(vcmd[i], p[0], 2)) {
3219 p[0] += 2;
3220 break;
3221 }
3222 i++;
3223 }
3224 switch (i) {
Joe Perches475be4d2012-02-19 19:52:38 -08003225 case 0:
3226 /* AT+VNH - Auto hangup feature */
3227 switch (*p[0]) {
3228 case '?':
3229 p[0]++;
3230 isdn_tty_at_cout("\r\n1", info);
3231 break;
3232 case '=':
3233 p[0]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003235 case '1':
3236 p[0]++;
3237 break;
3238 case '?':
3239 p[0]++;
3240 isdn_tty_at_cout("\r\n1", info);
3241 break;
3242 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
3245 break;
3246 default:
3247 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003248 }
3249 break;
3250 case 1:
3251 /* AT+VIP - Reset all voice parameters */
3252 isdn_tty_modem_reset_vpar(m);
3253 break;
3254 case 2:
3255 /* AT+VLS - Select device, accept incoming call */
3256 switch (*p[0]) {
3257 case '?':
3258 p[0]++;
3259 sprintf(rs, "\r\n%d", m->vpar[0]);
3260 isdn_tty_at_cout(rs, info);
3261 break;
3262 case '=':
3263 p[0]++;
3264 switch (*p[0]) {
3265 case '0':
3266 p[0]++;
3267 m->vpar[0] = 0;
3268 break;
3269 case '2':
3270 p[0]++;
3271 m->vpar[0] = 2;
3272 break;
3273 case '?':
3274 p[0]++;
3275 isdn_tty_at_cout("\r\n0,2", info);
3276 break;
3277 default:
3278 PARSE_ERROR1;
3279 }
3280 break;
3281 default:
3282 PARSE_ERROR1;
3283 }
3284 break;
3285 case 3:
3286 /* AT+VRX - Start recording */
3287 if (!m->vpar[0])
3288 PARSE_ERROR1;
3289 if (info->online != 1) {
3290 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3291 return 1;
3292 }
3293 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3294 if (!info->dtmf_state) {
3295 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3296 PARSE_ERROR1;
3297 }
3298 info->silence_state = isdn_audio_silence_init(info->silence_state);
3299 if (!info->silence_state) {
3300 printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3301 PARSE_ERROR1;
3302 }
3303 if (m->vpar[3] < 5) {
3304 info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3305 if (!info->adpcmr) {
3306 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3307 PARSE_ERROR1;
3308 }
3309 }
3310#ifdef ISDN_DEBUG_AT
3311 printk(KERN_DEBUG "AT: +VRX\n");
3312#endif
3313 info->vonline |= 1;
3314 isdn_tty_modem_result(RESULT_CONNECT, info);
3315 return 0;
3316 break;
3317 case 4:
3318 /* AT+VSD - Silence detection */
3319 switch (*p[0]) {
3320 case '?':
3321 p[0]++;
3322 sprintf(rs, "\r\n<%d>,<%d>",
3323 m->vpar[1],
3324 m->vpar[2]);
3325 isdn_tty_at_cout(rs, info);
3326 break;
3327 case '=':
3328 p[0]++;
3329 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3330 par1 = isdn_getnum(p);
3331 if ((par1 < 0) || (par1 > 31))
3332 PARSE_ERROR1;
3333 if (*p[0] != ',')
3334 PARSE_ERROR1;
3335 p[0]++;
3336 par2 = isdn_getnum(p);
3337 if ((par2 < 0) || (par2 > 255))
3338 PARSE_ERROR1;
3339 m->vpar[1] = par1;
3340 m->vpar[2] = par2;
3341 break;
3342 } else
3343 if (*p[0] == '?') {
3344 p[0]++;
3345 isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3346 info);
3347 break;
3348 } else
3349 PARSE_ERROR1;
3350 break;
3351 default:
3352 PARSE_ERROR1;
3353 }
3354 break;
3355 case 5:
3356 /* AT+VSM - Select compression */
3357 switch (*p[0]) {
3358 case '?':
3359 p[0]++;
3360 sprintf(rs, "\r\n<%d>,<%d><8000>",
3361 m->vpar[3],
3362 m->vpar[1]);
3363 isdn_tty_at_cout(rs, info);
3364 break;
3365 case '=':
3366 p[0]++;
3367 switch (*p[0]) {
3368 case '2':
3369 case '3':
3370 case '4':
3371 case '5':
3372 case '6':
3373 par1 = isdn_getnum(p);
3374 if ((par1 < 2) || (par1 > 6))
3375 PARSE_ERROR1;
3376 m->vpar[3] = par1;
3377 break;
3378 case '?':
3379 p[0]++;
3380 isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3381 info);
3382 isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3383 info);
3384 isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3385 info);
3386 isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3387 info);
3388 isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3389 info);
3390 break;
3391 default:
3392 PARSE_ERROR1;
3393 }
3394 break;
3395 default:
3396 PARSE_ERROR1;
3397 }
3398 break;
3399 case 6:
3400 /* AT+VTX - Start sending */
3401 if (!m->vpar[0])
3402 PARSE_ERROR1;
3403 if (info->online != 1) {
3404 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3405 return 1;
3406 }
3407 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3408 if (!info->dtmf_state) {
3409 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3410 PARSE_ERROR1;
3411 }
3412 if (m->vpar[3] < 5) {
3413 info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3414 if (!info->adpcms) {
3415 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3416 PARSE_ERROR1;
3417 }
3418 }
3419#ifdef ISDN_DEBUG_AT
3420 printk(KERN_DEBUG "AT: +VTX\n");
3421#endif
3422 m->lastDLE = 0;
3423 info->vonline |= 2;
3424 isdn_tty_modem_result(RESULT_CONNECT, info);
3425 return 0;
3426 break;
3427 case 7:
3428 /* AT+VDD - DTMF detection */
3429 switch (*p[0]) {
3430 case '?':
3431 p[0]++;
3432 sprintf(rs, "\r\n<%d>,<%d>",
3433 m->vpar[4],
3434 m->vpar[5]);
3435 isdn_tty_at_cout(rs, info);
3436 break;
3437 case '=':
3438 p[0]++;
3439 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3440 if (info->online != 1)
3441 PARSE_ERROR1;
3442 par1 = isdn_getnum(p);
3443 if ((par1 < 0) || (par1 > 15))
3444 PARSE_ERROR1;
3445 if (*p[0] != ',')
3446 PARSE_ERROR1;
3447 p[0]++;
3448 par2 = isdn_getnum(p);
3449 if ((par2 < 0) || (par2 > 255))
3450 PARSE_ERROR1;
3451 m->vpar[4] = par1;
3452 m->vpar[5] = par2;
3453 cmd.driver = info->isdn_driver;
3454 cmd.command = ISDN_CMD_AUDIO;
3455 cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3456 cmd.parm.num[0] = par1;
3457 cmd.parm.num[1] = par2;
3458 isdn_command(&cmd);
3459 break;
3460 } else
3461 if (*p[0] == '?') {
3462 p[0]++;
3463 isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3464 info);
3465 break;
3466 } else
3467 PARSE_ERROR1;
3468 break;
3469 default:
3470 PARSE_ERROR1;
3471 }
3472 break;
3473 default:
3474 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
3476 return 0;
3477}
3478#endif /* CONFIG_ISDN_AUDIO */
3479
3480/*
3481 * Parse and perform an AT-command-line.
3482 */
3483static void
Joe Perches475be4d2012-02-19 19:52:38 -08003484isdn_tty_parse_at(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
3486 atemu *m = &info->emu;
3487 char *p;
Dan Carpenterf417f5e2010-09-04 08:33:03 +00003488 char ds[ISDN_MSNLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490#ifdef ISDN_DEBUG_AT
3491 printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
3492#endif
3493 for (p = &m->mdmcmd[2]; *p;) {
3494 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -08003495 case ' ':
3496 p++;
3497 break;
3498 case 'A':
3499 /* A - Accept incoming call */
3500 p++;
3501 isdn_tty_cmd_ATA(info);
3502 return;
3503 break;
3504 case 'D':
3505 /* D - Dial */
3506 if (info->msr & UART_MSR_DCD)
3507 PARSE_ERROR;
3508 if (info->msr & UART_MSR_RI) {
3509 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return;
Joe Perches475be4d2012-02-19 19:52:38 -08003511 }
3512 isdn_tty_getdial(++p, ds, sizeof ds);
3513 p += strlen(p);
3514 if (!strlen(m->msn))
3515 isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
3516 else if (strlen(ds))
3517 isdn_tty_dial(ds, info, m);
3518 else
3519 PARSE_ERROR;
3520 return;
3521 case 'E':
3522 /* E - Turn Echo on/off */
3523 p++;
3524 switch (isdn_getnum(&p)) {
3525 case 0:
3526 m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 break;
Joe Perches475be4d2012-02-19 19:52:38 -08003528 case 1:
3529 m->mdmreg[REG_ECHO] |= BIT_ECHO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 break;
3531 default:
3532 PARSE_ERROR;
Joe Perches475be4d2012-02-19 19:52:38 -08003533 }
3534 break;
3535 case 'H':
3536 /* H - On/Off-hook */
3537 p++;
3538 switch (*p) {
3539 case '0':
3540 p++;
3541 isdn_tty_on_hook(info);
3542 break;
3543 case '1':
3544 p++;
3545 isdn_tty_off_hook();
3546 break;
3547 default:
3548 isdn_tty_on_hook(info);
3549 break;
3550 }
3551 break;
3552 case 'I':
3553 /* I - Information */
3554 p++;
3555 isdn_tty_at_cout("\r\nLinux ISDN", info);
3556 switch (*p) {
3557 case '0':
3558 case '1':
3559 p++;
3560 break;
3561 case '2':
3562 p++;
3563 isdn_tty_report(info);
3564 break;
3565 case '3':
3566 p++;
3567 snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
3568 isdn_tty_at_cout(ds, info);
3569 break;
3570 default:;
3571 }
3572 break;
3573#ifdef DUMMY_HAYES_AT
3574 case 'L':
3575 case 'M':
3576 /* only for be compilant with common scripts */
3577 /* no function */
3578 p++;
3579 isdn_getnum(&p);
3580 break;
3581#endif
3582 case 'O':
3583 /* O - Go online */
3584 p++;
3585 if (info->msr & UART_MSR_DCD)
3586 /* if B-Channel is up */
3587 isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
3588 else
3589 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3590 return;
3591 case 'Q':
3592 /* Q - Turn Emulator messages on/off */
3593 p++;
3594 switch (isdn_getnum(&p)) {
3595 case 0:
3596 m->mdmreg[REG_RESP] |= BIT_RESP;
3597 break;
3598 case 1:
3599 m->mdmreg[REG_RESP] &= ~BIT_RESP;
3600 break;
3601 default:
3602 PARSE_ERROR;
3603 }
3604 break;
3605 case 'S':
3606 /* S - Set/Get Register */
3607 p++;
3608 if (isdn_tty_cmd_ATS(&p, info))
3609 return;
3610 break;
3611 case 'V':
3612 /* V - Numeric or ASCII Emulator-messages */
3613 p++;
3614 switch (isdn_getnum(&p)) {
3615 case 0:
3616 m->mdmreg[REG_RESP] |= BIT_RESPNUM;
3617 break;
3618 case 1:
3619 m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
3620 break;
3621 default:
3622 PARSE_ERROR;
3623 }
3624 break;
3625 case 'Z':
3626 /* Z - Load Registers from Profile */
3627 p++;
3628 if (info->msr & UART_MSR_DCD) {
3629 info->online = 0;
3630 isdn_tty_on_hook(info);
3631 }
3632 isdn_tty_modem_reset_regs(info, 1);
3633 break;
3634 case '+':
3635 p++;
3636 switch (*p) {
3637#ifdef CONFIG_ISDN_AUDIO
3638 case 'F':
3639 p++;
3640 if (isdn_tty_cmd_PLUSF(&p, info))
3641 return;
3642 break;
3643 case 'V':
3644 if ((!(m->mdmreg[REG_SI1] & 1)) ||
3645 (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
3646 PARSE_ERROR;
3647 p++;
3648 if (isdn_tty_cmd_PLUSV(&p, info))
3649 return;
3650 break;
3651#endif /* CONFIG_ISDN_AUDIO */
3652 case 'S': /* SUSPEND */
3653 p++;
3654 isdn_tty_get_msnstr(ds, &p);
3655 isdn_tty_suspend(ds, info, m);
3656 break;
3657 case 'R': /* RESUME */
3658 p++;
3659 isdn_tty_get_msnstr(ds, &p);
3660 isdn_tty_resume(ds, info, m);
3661 break;
3662 case 'M': /* MESSAGE */
3663 p++;
3664 isdn_tty_send_msg(info, m, p);
3665 break;
3666 default:
3667 PARSE_ERROR;
3668 }
3669 break;
3670 case '&':
3671 p++;
3672 if (isdn_tty_cmd_ATand(&p, info))
3673 return;
3674 break;
3675 default:
3676 PARSE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
3678 }
3679#ifdef CONFIG_ISDN_AUDIO
3680 if (!info->vonline)
3681#endif
3682 isdn_tty_modem_result(RESULT_OK, info);
3683}
3684
3685/* Need own toupper() because standard-toupper is not available
3686 * within modules.
3687 */
Joe Perches475be4d2012-02-19 19:52:38 -08003688#define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
3690/*
3691 * Perform line-editing of AT-commands
3692 *
3693 * Parameters:
3694 * p inputbuffer
3695 * count length of buffer
3696 * channel index to line (minor-device)
3697 */
3698static int
Joe Perches475be4d2012-02-19 19:52:38 -08003699isdn_tty_edit_at(const char *p, int count, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
3701 atemu *m = &info->emu;
3702 int total = 0;
3703 u_char c;
3704 char eb[2];
3705 int cnt;
3706
3707 for (cnt = count; cnt > 0; p++, cnt--) {
3708 c = *p;
3709 total++;
3710 if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
3711 /* Separator (CR or LF) */
3712 m->mdmcmd[m->mdmcmdl] = 0;
3713 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3714 eb[0] = c;
3715 eb[1] = 0;
3716 isdn_tty_at_cout(eb, info);
3717 }
3718 if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
3719 isdn_tty_parse_at(info);
3720 m->mdmcmdl = 0;
3721 continue;
3722 }
3723 if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
3724 /* Backspace-Function */
3725 if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
3726 if (m->mdmcmdl)
3727 m->mdmcmdl--;
3728 if (m->mdmreg[REG_ECHO] & BIT_ECHO)
3729 isdn_tty_at_cout("\b", info);
3730 }
3731 continue;
3732 }
3733 if (cmdchar(c)) {
3734 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3735 eb[0] = c;
3736 eb[1] = 0;
3737 isdn_tty_at_cout(eb, info);
3738 }
3739 if (m->mdmcmdl < 255) {
3740 c = my_toupper(c);
3741 switch (m->mdmcmdl) {
Joe Perches475be4d2012-02-19 19:52:38 -08003742 case 1:
3743 if (c == 'T') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 m->mdmcmd[m->mdmcmdl] = c;
3745 m->mdmcmd[++m->mdmcmdl] = 0;
Joe Perches475be4d2012-02-19 19:52:38 -08003746 break;
3747 } else
3748 m->mdmcmdl = 0;
3749 /* Fall through, check for 'A' */
3750 case 0:
3751 if (c == 'A') {
3752 m->mdmcmd[m->mdmcmdl] = c;
3753 m->mdmcmd[++m->mdmcmdl] = 0;
3754 }
3755 break;
3756 default:
3757 m->mdmcmd[m->mdmcmdl] = c;
3758 m->mdmcmd[++m->mdmcmdl] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 }
3760 }
3761 }
3762 }
3763 return total;
3764}
3765
3766/*
3767 * Switch all modem-channels who are online and got a valid
3768 * escape-sequence 1.5 seconds ago, to command-mode.
3769 * This function is called every second via timer-interrupt from within
3770 * timer-dispatcher isdn_timer_function()
3771 */
3772void
3773isdn_tty_modem_escape(void)
3774{
3775 int ton = 0;
3776 int i;
3777 int midx;
3778
3779 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
Jiri Slaby37630f42012-04-02 13:53:52 +02003780 if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
3781 modem_info *info = &dev->mdm.info[midx];
3782 if (info->online) {
3783 ton = 1;
3784 if ((info->emu.pluscount == 3) &&
3785 time_after(jiffies,
3786 info->emu.lastplus + PLUSWAIT2)) {
3787 info->emu.pluscount = 0;
3788 info->online = 0;
3789 isdn_tty_modem_result(RESULT_OK, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 }
3791 }
Jiri Slaby37630f42012-04-02 13:53:52 +02003792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
3794}
3795
3796/*
3797 * Put a RING-message to all modem-channels who have the RI-bit set.
3798 * This function is called every second via timer-interrupt from within
3799 * timer-dispatcher isdn_timer_function()
3800 */
3801void
3802isdn_tty_modem_ring(void)
3803{
3804 int ton = 0;
3805 int i;
3806
3807 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3808 modem_info *info = &dev->mdm.info[i];
3809 if (info->msr & UART_MSR_RI) {
3810 ton = 1;
3811 isdn_tty_modem_result(RESULT_RING, info);
3812 }
3813 }
3814 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
3815}
3816
3817/*
3818 * For all online tty's, try sending data to
3819 * the lower levels.
3820 */
3821void
3822isdn_tty_modem_xmit(void)
3823{
3824 int ton = 1;
3825 int i;
3826
3827 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3828 modem_info *info = &dev->mdm.info[i];
3829 if (info->online) {
3830 ton = 1;
3831 isdn_tty_senddown(info);
3832 isdn_tty_tint(info);
3833 }
3834 }
3835 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
3836}
3837
3838/*
3839 * Check all channels if we have a 'no carrier' timeout.
3840 * Timeout value is set by Register S7.
3841 */
3842void
3843isdn_tty_carrier_timeout(void)
3844{
3845 int ton = 0;
3846 int i;
3847
3848 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3849 modem_info *info = &dev->mdm.info[i];
Jiri Slaby37630f42012-04-02 13:53:52 +02003850 if (!info->dialing)
3851 continue;
3852 if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
3853 info->dialing = 0;
3854 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3855 isdn_tty_modem_hup(info, 1);
3856 } else
3857 ton = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 }
3859 isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
3860}