blob: d8468f3529a7c9fca2b1b0dd5cfd09e4915a11fc [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{
Jiri Slaby227434f2013-01-03 15:53:01 +010063 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int c;
65 int len;
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 if (!(info->mcr & UART_MCR_RTS))
72 return 0;
73
74 len = skb->len
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifdef CONFIG_ISDN_AUDIO
Jiri Slaby37630f42012-04-02 13:53:52 +020076 + ISDN_AUDIO_SKB_DLECOUNT(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
Jiri Slaby37630f42012-04-02 13:53:52 +020078 ;
79
Jiri Slaby227434f2013-01-03 15:53:01 +010080 c = tty_buffer_request_room(port, len);
Jiri Slaby37630f42012-04-02 13:53:52 +020081 if (c < len)
82 return 0;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#ifdef CONFIG_ISDN_AUDIO
Jiri Slaby37630f42012-04-02 13:53:52 +020085 if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
86 int l = skb->len;
87 unsigned char *dp = skb->data;
88 while (--l) {
89 if (*dp == DLE)
Jiri Slaby92a19f92013-01-03 15:53:03 +010090 tty_insert_flip_char(port, DLE, 0);
91 tty_insert_flip_char(port, *dp++, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Jiri Slaby37630f42012-04-02 13:53:52 +020093 if (*dp == DLE)
Jiri Slaby92a19f92013-01-03 15:53:03 +010094 tty_insert_flip_char(port, DLE, 0);
Jiri Slaby37630f42012-04-02 13:53:52 +020095 last = *dp;
96 } else {
97#endif
98 if (len > 1)
Jiri Slaby05c7cd32013-01-03 15:53:04 +010099 tty_insert_flip_string(port, skb->data, len - 1);
Jiri Slaby37630f42012-04-02 13:53:52 +0200100 last = skb->data[len - 1];
101#ifdef CONFIG_ISDN_AUDIO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Jiri Slaby37630f42012-04-02 13:53:52 +0200103#endif
104 if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100105 tty_insert_flip_char(port, last, 0xFF);
Jiri Slaby37630f42012-04-02 13:53:52 +0200106 else
Jiri Slaby92a19f92013-01-03 15:53:03 +0100107 tty_insert_flip_char(port, last, TTY_NORMAL);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100108 tty_flip_buffer_push(port);
Jiri Slaby37630f42012-04-02 13:53:52 +0200109 kfree_skb(skb);
110
111 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/* isdn_tty_readmodem() is called periodically from within timer-interrupt.
115 * It tries getting received data from the receive queue an stuff it into
116 * the tty's flip-buffer.
117 */
118void
119isdn_tty_readmodem(void)
120{
121 int resched = 0;
122 int midx;
123 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 modem_info *info;
126
127 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
Jiri Slaby37630f42012-04-02 13:53:52 +0200128 midx = dev->m_idx[i];
129 if (midx < 0)
130 continue;
131
132 info = &dev->mdm.info[midx];
133 if (!info->online)
134 continue;
135
136 r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#ifdef CONFIG_ISDN_AUDIO
Jiri Slaby37630f42012-04-02 13:53:52 +0200138 isdn_audio_eval_dtmf(info);
139 if ((info->vonline & 1) && (info->emu.vpar[1]))
140 isdn_audio_eval_silence(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif
Jiri Slaby2e124b42013-01-03 15:53:06 +0100142 if (info->mcr & UART_MCR_RTS) {
143 /* CISCO AsyncPPP Hack */
144 if (!(info->emu.mdmreg[REG_CPPP] & BIT_CPPP))
145 r = isdn_readbchan_tty(info->isdn_driver,
146 info->isdn_channel,
147 &info->port, 0);
148 else
149 r = isdn_readbchan_tty(info->isdn_driver,
150 info->isdn_channel,
151 &info->port, 1);
152 if (r)
153 tty_flip_buffer_push(&info->port);
Jiri Slaby37630f42012-04-02 13:53:52 +0200154 } else
155 r = 1;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100156
Jiri Slaby37630f42012-04-02 13:53:52 +0200157 if (r) {
158 info->rcvsched = 0;
159 resched = 1;
160 } else
161 info->rcvsched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 if (!resched)
164 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
165}
166
167int
168isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
169{
170 ulong flags;
171 int midx;
172#ifdef CONFIG_ISDN_AUDIO
173 int ifmt;
174#endif
175 modem_info *info;
176
177 if ((midx = dev->m_idx[i]) < 0) {
178 /* if midx is invalid, packet is not for tty */
179 return 0;
180 }
181 info = &dev->mdm.info[midx];
182#ifdef CONFIG_ISDN_AUDIO
183 ifmt = 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if ((info->vonline) && (!info->emu.vpar[4]))
186 isdn_audio_calc_dtmf(info, skb->data, skb->len, ifmt);
187 if ((info->vonline & 1) && (info->emu.vpar[1]))
188 isdn_audio_calc_silence(info, skb->data, skb->len, ifmt);
189#endif
190 if ((info->online < 2)
191#ifdef CONFIG_ISDN_AUDIO
192 && (!(info->vonline & 1))
193#endif
194 ) {
195 /* If Modem not listening, drop data */
196 kfree_skb(skb);
197 return 1;
198 }
199 if (info->emu.mdmreg[REG_T70] & BIT_T70) {
200 if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT) {
201 /* T.70 decoding: throw away the T.70 header (2 or 4 bytes) */
202 if (skb->data[0] == 3) /* pure data packet -> 4 byte headers */
203 skb_pull(skb, 4);
204 else
205 if (skb->data[0] == 1) /* keepalive packet -> 2 byte hdr */
206 skb_pull(skb, 2);
207 } else
208 /* T.70 decoding: Simply throw away the T.70 header (4 bytes) */
209 if ((skb->data[0] == 1) && ((skb->data[1] == 0) || (skb->data[1] == 1)))
210 skb_pull(skb, 4);
211 }
212#ifdef CONFIG_ISDN_AUDIO
213 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
214 ISDN_AUDIO_SKB_LOCK(skb) = 0;
215 if (info->vonline & 1) {
216 /* voice conversion/compression */
217 switch (info->emu.vpar[3]) {
Joe Perches475be4d2012-02-19 19:52:38 -0800218 case 2:
219 case 3:
220 case 4:
221 /* adpcm
222 * Since compressed data takes less
223 * space, we can overwrite the buffer.
224 */
225 skb_trim(skb, isdn_audio_xlaw2adpcm(info->adpcmr,
226 ifmt,
227 skb->data,
228 skb->data,
229 skb->len));
230 break;
231 case 5:
232 /* a-law */
233 if (!ifmt)
234 isdn_audio_ulaw2alaw(skb->data, skb->len);
235 break;
236 case 6:
237 /* u-law */
238 if (ifmt)
239 isdn_audio_alaw2ulaw(skb->data, skb->len);
240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 ISDN_AUDIO_SKB_DLECOUNT(skb) =
243 isdn_tty_countDLE(skb->data, skb->len);
244 }
245#ifdef CONFIG_ISDN_TTY_FAX
246 else {
247 if (info->faxonline & 2) {
248 isdn_tty_fax_bitorder(info, skb);
249 ISDN_AUDIO_SKB_DLECOUNT(skb) =
250 isdn_tty_countDLE(skb->data, skb->len);
251 }
252 }
253#endif
254#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800255 /* Try to deliver directly via tty-buf if queue is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 spin_lock_irqsave(&info->readlock, flags);
257 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
258 if (isdn_tty_try_read(info, skb)) {
259 spin_unlock_irqrestore(&info->readlock, flags);
260 return 1;
261 }
262 /* Direct deliver failed or queue wasn't empty.
263 * Queue up for later dequeueing via timer-irq.
264 */
265 __skb_queue_tail(&dev->drv[di]->rpqueue[channel], skb);
266 dev->drv[di]->rcvcount[channel] +=
267 (skb->len
268#ifdef CONFIG_ISDN_AUDIO
269 + ISDN_AUDIO_SKB_DLECOUNT(skb)
270#endif
271 );
272 spin_unlock_irqrestore(&info->readlock, flags);
273 /* Schedule dequeuing */
274 if ((dev->modempoll) && (info->rcvsched))
275 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
276 return 1;
277}
278
Adrian Bunk3e206b02005-06-25 14:58:35 -0700279static void
Joe Perches475be4d2012-02-19 19:52:38 -0800280isdn_tty_cleanup_xmit(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 skb_queue_purge(&info->xmit_queue);
283#ifdef CONFIG_ISDN_AUDIO
284 skb_queue_purge(&info->dtmf_queue);
285#endif
286}
287
288static void
Joe Perches475be4d2012-02-19 19:52:38 -0800289isdn_tty_tint(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
292 int len, slen;
293
294 if (!skb)
295 return;
296 len = skb->len;
297 if ((slen = isdn_writebuf_skb_stub(info->isdn_driver,
298 info->isdn_channel, 1, skb)) == len) {
Jiri Slabyba432942012-04-02 13:53:57 +0200299 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 info->send_outstanding++;
301 info->msr &= ~UART_MSR_CTS;
302 info->lsr &= ~UART_LSR_TEMT;
303 tty_wakeup(tty);
304 return;
305 }
306 if (slen < 0) {
307 /* Error: no channel, already shutdown, or wrong parameter */
308 dev_kfree_skb(skb);
309 return;
310 }
311 skb_queue_head(&info->xmit_queue, skb);
312}
313
314#ifdef CONFIG_ISDN_AUDIO
315static int
316isdn_tty_countDLE(unsigned char *buf, int len)
317{
318 int count = 0;
319
320 while (len--)
321 if (*buf++ == DLE)
322 count++;
323 return count;
324}
325
326/* This routine is called from within isdn_tty_write() to perform
327 * DLE-decoding when sending audio-data.
328 */
329static int
Joe Perches475be4d2012-02-19 19:52:38 -0800330isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Jiri Slaby82e46b32012-04-02 13:53:58 +0200332 unsigned char *p = &info->port.xmit_buf[info->xmit_count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int count = 0;
334
335 while (len > 0) {
336 if (m->lastDLE) {
337 m->lastDLE = 0;
338 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -0800339 case DLE:
340 /* Escape code */
341 if (len > 1)
342 memmove(p, p + 1, len - 1);
343 p--;
344 count++;
345 break;
346 case ETX:
347 /* End of data */
348 info->vonline |= 4;
349 return count;
350 case DC4:
351 /* Abort RX */
352 info->vonline &= ~1;
353#ifdef ISDN_DEBUG_MODEM_VOICE
354 printk(KERN_DEBUG
355 "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
356 info->line);
357#endif
358 isdn_tty_at_cout("\020\003", info);
359 if (!info->vonline) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#ifdef ISDN_DEBUG_MODEM_VOICE
361 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800362 "DLEdown: send VCON on ttyI%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 info->line);
364#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800365 isdn_tty_at_cout("\r\nVCON\r\n", info);
366 }
367 /* Fall through */
368 case 'q':
369 case 's':
370 /* Silence */
371 if (len > 1)
372 memmove(p, p + 1, len - 1);
373 p--;
374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 } else {
377 if (*p == DLE)
378 m->lastDLE = 1;
379 else
380 count++;
381 }
382 p++;
383 len--;
384 }
385 if (len < 0) {
386 printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
387 return 0;
388 }
389 return count;
390}
391
392/* This routine is called from within isdn_tty_write() when receiving
393 * audio-data. It interrupts receiving, if an character other than
394 * ^S or ^Q is sent.
395 */
396static int
397isdn_tty_end_vrx(const char *buf, int c)
398{
399 char ch;
400
401 while (c--) {
402 ch = *buf;
403 if ((ch != 0x11) && (ch != 0x13))
404 return 1;
405 buf++;
406 }
407 return 0;
408}
409
410static int voice_cf[7] =
411{0, 0, 4, 3, 2, 0, 0};
412
413#endif /* CONFIG_ISDN_AUDIO */
414
415/* isdn_tty_senddown() is called either directly from within isdn_tty_write()
416 * or via timer-interrupt from within isdn_tty_modem_xmit(). It pulls
417 * outgoing data from the tty's xmit-buffer, handles voice-decompression or
418 * T.70 if necessary, and finally queues it up for sending via isdn_tty_tint.
419 */
420static void
Joe Perches475be4d2012-02-19 19:52:38 -0800421isdn_tty_senddown(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 int buflen;
424 int skb_res;
425#ifdef CONFIG_ISDN_AUDIO
426 int audio_len;
427#endif
428 struct sk_buff *skb;
429
430#ifdef CONFIG_ISDN_AUDIO
431 if (info->vonline & 4) {
432 info->vonline &= ~6;
433 if (!info->vonline) {
434#ifdef ISDN_DEBUG_MODEM_VOICE
435 printk(KERN_DEBUG
436 "senddown: send VCON on ttyI%d\n",
437 info->line);
438#endif
439 isdn_tty_at_cout("\r\nVCON\r\n", info);
440 }
441 }
442#endif
443 if (!(buflen = info->xmit_count))
444 return;
Joe Perches475be4d2012-02-19 19:52:38 -0800445 if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 info->msr &= ~UART_MSR_CTS;
Joe Perches475be4d2012-02-19 19:52:38 -0800447 info->lsr &= ~UART_LSR_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* info->xmit_count is modified here and in isdn_tty_write().
449 * So we return here if isdn_tty_write() is in the
450 * critical section.
451 */
452 atomic_inc(&info->xmit_lock);
453 if (!(atomic_dec_and_test(&info->xmit_lock)))
454 return;
455 if (info->isdn_driver < 0) {
456 info->xmit_count = 0;
457 return;
458 }
459 skb_res = dev->drv[info->isdn_driver]->interface->hl_hdrlen + 4;
460#ifdef CONFIG_ISDN_AUDIO
461 if (info->vonline & 2)
462 audio_len = buflen * voice_cf[info->emu.vpar[3]];
463 else
464 audio_len = 0;
465 skb = dev_alloc_skb(skb_res + buflen + audio_len);
466#else
467 skb = dev_alloc_skb(skb_res + buflen);
468#endif
469 if (!skb) {
470 printk(KERN_WARNING
471 "isdn_tty: Out of memory in ttyI%d senddown\n",
472 info->line);
473 return;
474 }
475 skb_reserve(skb, skb_res);
Jiri Slaby82e46b32012-04-02 13:53:58 +0200476 memcpy(skb_put(skb, buflen), info->port.xmit_buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 info->xmit_count = 0;
478#ifdef CONFIG_ISDN_AUDIO
479 if (info->vonline & 2) {
480 /* For now, ifmt is fixed to 1 (alaw), since this
481 * is used with ISDN everywhere in the world, except
482 * US, Canada and Japan.
483 * Later, when US-ISDN protocols are implemented,
484 * this setting will depend on the D-channel protocol.
485 */
486 int ifmt = 1;
487
488 /* voice conversion/decompression */
489 switch (info->emu.vpar[3]) {
Joe Perches475be4d2012-02-19 19:52:38 -0800490 case 2:
491 case 3:
492 case 4:
493 /* adpcm, compatible to ZyXel 1496 modem
494 * with ROM revision 6.01
495 */
496 audio_len = isdn_audio_adpcm2xlaw(info->adpcms,
497 ifmt,
498 skb->data,
499 skb_put(skb, audio_len),
500 buflen);
501 skb_pull(skb, buflen);
502 skb_trim(skb, audio_len);
503 break;
504 case 5:
505 /* a-law */
506 if (!ifmt)
507 isdn_audio_alaw2ulaw(skb->data,
508 buflen);
509 break;
510 case 6:
511 /* u-law */
512 if (ifmt)
513 isdn_audio_ulaw2alaw(skb->data,
514 buflen);
515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 }
518#endif /* CONFIG_ISDN_AUDIO */
519 if (info->emu.mdmreg[REG_T70] & BIT_T70) {
520 /* Add T.70 simplified header */
521 if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT)
522 memcpy(skb_push(skb, 2), "\1\0", 2);
523 else
524 memcpy(skb_push(skb, 4), "\1\0\1\0", 4);
525 }
526 skb_queue_tail(&info->xmit_queue, skb);
527}
528
529/************************************************************
530 *
531 * Modem-functions
532 *
533 * mostly "stolen" from original Linux-serial.c and friends.
534 *
535 ************************************************************/
536
537/* The next routine is called once from within timer-interrupt
538 * triggered within isdn_tty_modem_ncarrier(). It calls
539 * isdn_tty_modem_result() to stuff a "NO CARRIER" Message
Alan Cox33f0f882006-01-09 20:54:13 -0800540 * into the tty's buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542static void
543isdn_tty_modem_do_ncarrier(unsigned long data)
544{
545 modem_info *info = (modem_info *) data;
546 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
547}
548
549/* Next routine is called, whenever the DTR-signal is raised.
550 * It checks the ncarrier-flag, and triggers the above routine
551 * when necessary. The ncarrier-flag is set, whenever DTR goes
552 * low.
553 */
554static void
Joe Perches475be4d2012-02-19 19:52:38 -0800555isdn_tty_modem_ncarrier(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 if (info->ncarrier) {
558 info->nc_timer.expires = jiffies + HZ;
559 add_timer(&info->nc_timer);
560 }
561}
562
563/*
564 * return the usage calculated by si and layer 2 protocol
565 */
Adrian Bunk3e206b02005-06-25 14:58:35 -0700566static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567isdn_calc_usage(int si, int l2)
568{
569 int usg = ISDN_USAGE_MODEM;
570
571#ifdef CONFIG_ISDN_AUDIO
572 if (si == 1) {
Joe Perches475be4d2012-02-19 19:52:38 -0800573 switch (l2) {
574 case ISDN_PROTO_L2_MODEM:
575 usg = ISDN_USAGE_MODEM;
576 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800578 case ISDN_PROTO_L2_FAX:
579 usg = ISDN_USAGE_FAX;
580 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800582 case ISDN_PROTO_L2_TRANS:
583 default:
584 usg = ISDN_USAGE_VOICE;
585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 }
588#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800589 return (usg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/* isdn_tty_dial() performs dialing of a tty an the necessary
593 * setup of the lower levels before that.
594 */
595static void
Joe Perches475be4d2012-02-19 19:52:38 -0800596isdn_tty_dial(char *n, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 int usg = ISDN_USAGE_MODEM;
599 int si = 7;
600 int l2 = m->mdmreg[REG_L2PROT];
601 u_long flags;
602 isdn_ctrl cmd;
603 int i;
604 int j;
605
606 for (j = 7; j >= 0; j--)
607 if (m->mdmreg[REG_SI1] & (1 << j)) {
608 si = bit2si[j];
609 break;
610 }
611 usg = isdn_calc_usage(si, l2);
612#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -0800613 if ((si == 1) &&
614 (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800616 && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#endif
618 ) {
619 l2 = ISDN_PROTO_L2_TRANS;
620 usg = ISDN_USAGE_VOICE;
621 }
622#endif
623 m->mdmreg[REG_SI1I] = si2bit[si];
624 spin_lock_irqsave(&dev->lock, flags);
625 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
626 if (i < 0) {
627 spin_unlock_irqrestore(&dev->lock, flags);
628 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
629 } else {
630 info->isdn_driver = dev->drvmap[i];
631 info->isdn_channel = dev->chanmap[i];
632 info->drv_index = i;
633 dev->m_idx[i] = info->line;
634 dev->usage[i] |= ISDN_USAGE_OUTGOING;
635 info->last_dir = 1;
636 strcpy(info->last_num, n);
637 isdn_info_update();
638 spin_unlock_irqrestore(&dev->lock, flags);
639 cmd.driver = info->isdn_driver;
640 cmd.arg = info->isdn_channel;
641 cmd.command = ISDN_CMD_CLREAZ;
642 isdn_command(&cmd);
643 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
644 cmd.driver = info->isdn_driver;
645 cmd.command = ISDN_CMD_SETEAZ;
646 isdn_command(&cmd);
647 cmd.driver = info->isdn_driver;
648 cmd.command = ISDN_CMD_SETL2;
649 info->last_l2 = l2;
650 cmd.arg = info->isdn_channel + (l2 << 8);
651 isdn_command(&cmd);
652 cmd.driver = info->isdn_driver;
653 cmd.command = ISDN_CMD_SETL3;
654 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
655#ifdef CONFIG_ISDN_TTY_FAX
656 if (l2 == ISDN_PROTO_L2_FAX) {
657 cmd.parm.fax = info->fax;
658 info->fax->direction = ISDN_TTY_FAX_CONN_OUT;
659 }
660#endif
661 isdn_command(&cmd);
662 cmd.driver = info->isdn_driver;
663 cmd.arg = info->isdn_channel;
664 sprintf(cmd.parm.setup.phone, "%s", n);
665 sprintf(cmd.parm.setup.eazmsn, "%s",
666 isdn_map_eaz2msn(m->msn, info->isdn_driver));
667 cmd.parm.setup.si1 = si;
668 cmd.parm.setup.si2 = m->mdmreg[REG_SI2];
669 cmd.command = ISDN_CMD_DIAL;
670 info->dialing = 1;
671 info->emu.carrierwait = 0;
672 strcpy(dev->num[i], n);
673 isdn_info_update();
674 isdn_command(&cmd);
675 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
676 }
677}
678
679/* isdn_tty_hangup() disassociates a tty from the real
680 * ISDN-line (hangup). The usage-status is cleared
681 * and some cleanup is done also.
682 */
683void
Joe Perches475be4d2012-02-19 19:52:38 -0800684isdn_tty_modem_hup(modem_info *info, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 isdn_ctrl cmd;
687 int di, ch;
688
689 if (!info)
690 return;
691
692 di = info->isdn_driver;
693 ch = info->isdn_channel;
694 if (di < 0 || ch < 0)
695 return;
696
697 info->isdn_driver = -1;
698 info->isdn_channel = -1;
699
700#ifdef ISDN_DEBUG_MODEM_HUP
701 printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
702#endif
703 info->rcvsched = 0;
Jiri Slabyba432942012-04-02 13:53:57 +0200704 isdn_tty_flush_buffer(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (info->online) {
706 info->last_lhup = local;
707 info->online = 0;
708 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
709 }
710#ifdef CONFIG_ISDN_AUDIO
711 info->vonline = 0;
712#ifdef CONFIG_ISDN_TTY_FAX
713 info->faxonline = 0;
714 info->fax->phase = ISDN_FAX_PHASE_IDLE;
715#endif
716 info->emu.vpar[4] = 0;
717 info->emu.vpar[5] = 8;
Jesper Juhl3c7208f2005-11-07 01:01:29 -0800718 kfree(info->dtmf_state);
719 info->dtmf_state = NULL;
720 kfree(info->silence_state);
721 info->silence_state = NULL;
722 kfree(info->adpcms);
723 info->adpcms = NULL;
724 kfree(info->adpcmr);
725 info->adpcmr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#endif
727 if ((info->msr & UART_MSR_RI) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800728 (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 isdn_tty_modem_result(RESULT_RUNG, info);
730 info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
731 info->lsr |= UART_LSR_TEMT;
732
733 if (local) {
734 cmd.driver = di;
735 cmd.command = ISDN_CMD_HANGUP;
736 cmd.arg = ch;
737 isdn_command(&cmd);
738 }
739
740 isdn_all_eaz(di, ch);
741 info->emu.mdmreg[REG_RINGCNT] = 0;
742 isdn_free_channel(di, ch, 0);
743
744 if (info->drv_index >= 0) {
745 dev->m_idx[info->drv_index] = -1;
746 info->drv_index = -1;
747 }
748}
749
750/*
Joe Perches475be4d2012-02-19 19:52:38 -0800751 * Begin of a CAPI like interface, currently used only for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * supplementary service (CAPI 2.0 part III)
753 */
754#include <linux/isdn/capicmd.h>
Paul Gortmaker07a97fe2011-08-30 12:08:51 -0400755#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757int
758isdn_tty_capi_facility(capi_msg *cm) {
Joe Perches475be4d2012-02-19 19:52:38 -0800759 return (-1); /* dummy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/* isdn_tty_suspend() tries to suspend the current tty connection
763 */
764static void
Joe Perches475be4d2012-02-19 19:52:38 -0800765isdn_tty_suspend(char *id, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 isdn_ctrl cmd;
Joe Perches475be4d2012-02-19 19:52:38 -0800768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int l;
770
771 if (!info)
772 return;
773
774#ifdef ISDN_DEBUG_MODEM_SERVICES
775 printk(KERN_DEBUG "Msusp ttyI%d\n", info->line);
776#endif
777 l = strlen(id);
778 if ((info->isdn_driver >= 0)) {
Joe Perches475be4d2012-02-19 19:52:38 -0800779 cmd.parm.cmsg.Length = l + 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 cmd.parm.cmsg.Command = CAPI_FACILITY;
781 cmd.parm.cmsg.Subcommand = CAPI_REQ;
782 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
783 cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
784 cmd.parm.cmsg.para[1] = 0;
785 cmd.parm.cmsg.para[2] = l + 3;
786 cmd.parm.cmsg.para[3] = 4; /* 16 bit 0x0004 Suspend */
787 cmd.parm.cmsg.para[4] = 0;
788 cmd.parm.cmsg.para[5] = l;
789 strncpy(&cmd.parm.cmsg.para[6], id, l);
790 cmd.command = CAPI_PUT_MESSAGE;
791 cmd.driver = info->isdn_driver;
792 cmd.arg = info->isdn_channel;
793 isdn_command(&cmd);
794 }
795}
796
797/* isdn_tty_resume() tries to resume a suspended call
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300798 * setup of the lower levels before that. unfortunately here is no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * checking for compatibility of used protocols implemented by Q931
800 * It does the same things like isdn_tty_dial, the last command
801 * is different, may be we can merge it.
802 */
803
804static void
Joe Perches475be4d2012-02-19 19:52:38 -0800805isdn_tty_resume(char *id, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 int usg = ISDN_USAGE_MODEM;
808 int si = 7;
809 int l2 = m->mdmreg[REG_L2PROT];
810 isdn_ctrl cmd;
811 ulong flags;
812 int i;
813 int j;
814 int l;
815
816 l = strlen(id);
817 for (j = 7; j >= 0; j--)
818 if (m->mdmreg[REG_SI1] & (1 << j)) {
819 si = bit2si[j];
820 break;
821 }
822 usg = isdn_calc_usage(si, l2);
823#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -0800824 if ((si == 1) &&
825 (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800827 && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#endif
829 ) {
830 l2 = ISDN_PROTO_L2_TRANS;
831 usg = ISDN_USAGE_VOICE;
832 }
833#endif
834 m->mdmreg[REG_SI1I] = si2bit[si];
835 spin_lock_irqsave(&dev->lock, flags);
836 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
837 if (i < 0) {
838 spin_unlock_irqrestore(&dev->lock, flags);
839 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
840 } else {
841 info->isdn_driver = dev->drvmap[i];
842 info->isdn_channel = dev->chanmap[i];
843 info->drv_index = i;
844 dev->m_idx[i] = info->line;
845 dev->usage[i] |= ISDN_USAGE_OUTGOING;
846 info->last_dir = 1;
847// strcpy(info->last_num, n);
848 isdn_info_update();
849 spin_unlock_irqrestore(&dev->lock, flags);
850 cmd.driver = info->isdn_driver;
851 cmd.arg = info->isdn_channel;
852 cmd.command = ISDN_CMD_CLREAZ;
853 isdn_command(&cmd);
854 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
855 cmd.driver = info->isdn_driver;
856 cmd.command = ISDN_CMD_SETEAZ;
857 isdn_command(&cmd);
858 cmd.driver = info->isdn_driver;
859 cmd.command = ISDN_CMD_SETL2;
860 info->last_l2 = l2;
861 cmd.arg = info->isdn_channel + (l2 << 8);
862 isdn_command(&cmd);
863 cmd.driver = info->isdn_driver;
864 cmd.command = ISDN_CMD_SETL3;
865 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
866 isdn_command(&cmd);
867 cmd.driver = info->isdn_driver;
868 cmd.arg = info->isdn_channel;
Joe Perches475be4d2012-02-19 19:52:38 -0800869 cmd.parm.cmsg.Length = l + 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 cmd.parm.cmsg.Command = CAPI_FACILITY;
871 cmd.parm.cmsg.Subcommand = CAPI_REQ;
872 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
873 cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
874 cmd.parm.cmsg.para[1] = 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800875 cmd.parm.cmsg.para[2] = l + 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 cmd.parm.cmsg.para[3] = 5; /* 16 bit 0x0005 Resume */
877 cmd.parm.cmsg.para[4] = 0;
878 cmd.parm.cmsg.para[5] = l;
879 strncpy(&cmd.parm.cmsg.para[6], id, l);
Joe Perches475be4d2012-02-19 19:52:38 -0800880 cmd.command = CAPI_PUT_MESSAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 info->dialing = 1;
882// strcpy(dev->num[i], n);
883 isdn_info_update();
884 isdn_command(&cmd);
885 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
886 }
887}
888
889/* isdn_tty_send_msg() sends a message to a HL driver
890 * This is used for hybrid modem cards to send AT commands to it
891 */
892
893static void
Joe Perches475be4d2012-02-19 19:52:38 -0800894isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 int usg = ISDN_USAGE_MODEM;
897 int si = 7;
898 int l2 = m->mdmreg[REG_L2PROT];
899 isdn_ctrl cmd;
900 ulong flags;
901 int i;
902 int j;
903 int l;
904
Chen Gangf3947932013-03-07 18:25:41 +0000905 l = min(strlen(msg), sizeof(cmd.parm) - sizeof(cmd.parm.cmsg)
906 + sizeof(cmd.parm.cmsg.para) - 2);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (!l) {
909 isdn_tty_modem_result(RESULT_ERROR, info);
910 return;
911 }
912 for (j = 7; j >= 0; j--)
913 if (m->mdmreg[REG_SI1] & (1 << j)) {
914 si = bit2si[j];
915 break;
916 }
917 usg = isdn_calc_usage(si, l2);
918#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -0800919 if ((si == 1) &&
920 (l2 != ISDN_PROTO_L2_MODEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800922 && (l2 != ISDN_PROTO_L2_FAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923#endif
924 ) {
925 l2 = ISDN_PROTO_L2_TRANS;
926 usg = ISDN_USAGE_VOICE;
927 }
928#endif
929 m->mdmreg[REG_SI1I] = si2bit[si];
930 spin_lock_irqsave(&dev->lock, flags);
931 i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
932 if (i < 0) {
933 spin_unlock_irqrestore(&dev->lock, flags);
934 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
935 } else {
936 info->isdn_driver = dev->drvmap[i];
937 info->isdn_channel = dev->chanmap[i];
938 info->drv_index = i;
939 dev->m_idx[i] = info->line;
940 dev->usage[i] |= ISDN_USAGE_OUTGOING;
941 info->last_dir = 1;
942 isdn_info_update();
943 spin_unlock_irqrestore(&dev->lock, flags);
944 cmd.driver = info->isdn_driver;
945 cmd.arg = info->isdn_channel;
946 cmd.command = ISDN_CMD_CLREAZ;
947 isdn_command(&cmd);
948 strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
949 cmd.driver = info->isdn_driver;
950 cmd.command = ISDN_CMD_SETEAZ;
951 isdn_command(&cmd);
952 cmd.driver = info->isdn_driver;
953 cmd.command = ISDN_CMD_SETL2;
954 info->last_l2 = l2;
955 cmd.arg = info->isdn_channel + (l2 << 8);
956 isdn_command(&cmd);
957 cmd.driver = info->isdn_driver;
958 cmd.command = ISDN_CMD_SETL3;
959 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
960 isdn_command(&cmd);
961 cmd.driver = info->isdn_driver;
962 cmd.arg = info->isdn_channel;
Joe Perches475be4d2012-02-19 19:52:38 -0800963 cmd.parm.cmsg.Length = l + 14;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
965 cmd.parm.cmsg.Subcommand = CAPI_REQ;
966 cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800967 cmd.parm.cmsg.para[0] = l + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 strncpy(&cmd.parm.cmsg.para[1], msg, l);
Joe Perches475be4d2012-02-19 19:52:38 -0800969 cmd.parm.cmsg.para[l + 1] = 0xd;
970 cmd.command = CAPI_PUT_MESSAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/* info->dialing = 1;
972 strcpy(dev->num[i], n);
973 isdn_info_update();
974*/
975 isdn_command(&cmd);
976 }
977}
978
979static inline int
980isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
981{
982#ifdef MODEM_PARANOIA_CHECK
983 if (!info) {
984 printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800985 name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 1;
987 }
988 if (info->magic != ISDN_ASYNC_MAGIC) {
989 printk(KERN_WARNING "isdn_tty: bad magic for modem struct %s in %s\n",
990 name, routine);
991 return 1;
992 }
993#endif
994 return 0;
995}
996
997/*
998 * This routine is called to set the UART divisor registers to match
999 * the specified baud rate for a serial port.
1000 */
1001static void
Joe Perches475be4d2012-02-19 19:52:38 -08001002isdn_tty_change_speed(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Jiri Slaby265d6f02012-04-02 13:53:59 +02001004 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 uint cflag,
Joe Perches475be4d2012-02-19 19:52:38 -08001006 cval,
1007 quot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int i;
1009
Alan Coxadc8d742012-07-14 15:31:47 +01001010 if (!port->tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return;
Alan Coxadc8d742012-07-14 15:31:47 +01001012 cflag = port->tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 quot = i = cflag & CBAUD;
1015 if (i & CBAUDEX) {
1016 i &= ~CBAUDEX;
1017 if (i < 1 || i > 2)
Alan Coxadc8d742012-07-14 15:31:47 +01001018 port->tty->termios.c_cflag &= ~CBAUDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 else
1020 i += 15;
1021 }
1022 if (quot) {
1023 info->mcr |= UART_MCR_DTR;
1024 isdn_tty_modem_ncarrier(info);
1025 } else {
1026 info->mcr &= ~UART_MCR_DTR;
1027 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1028#ifdef ISDN_DEBUG_MODEM_HUP
1029 printk(KERN_DEBUG "Mhup in changespeed\n");
1030#endif
1031 if (info->online)
1032 info->ncarrier = 1;
1033 isdn_tty_modem_reset_regs(info, 0);
1034 isdn_tty_modem_hup(info, 1);
1035 }
1036 return;
1037 }
1038 /* byte size and parity */
1039 cval = cflag & (CSIZE | CSTOPB);
1040 cval >>= 4;
1041 if (cflag & PARENB)
1042 cval |= UART_LCR_PARITY;
1043 if (!(cflag & PARODD))
1044 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (cflag & CLOCAL)
Jiri Slaby265d6f02012-04-02 13:53:59 +02001047 port->flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 else {
Jiri Slaby265d6f02012-04-02 13:53:59 +02001049 port->flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051}
1052
1053static int
Joe Perches475be4d2012-02-19 19:52:38 -08001054isdn_tty_startup(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Jiri Slaby48decc12012-04-02 13:53:54 +02001056 if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return 0;
1058 isdn_lock_drivers();
1059#ifdef ISDN_DEBUG_MODEM_OPEN
1060 printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
1061#endif
1062 /*
1063 * Now, initialize the UART
1064 */
1065 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
Jiri Slabyba432942012-04-02 13:53:57 +02001066 if (info->port.tty)
1067 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * and set the speed of the serial port
1070 */
1071 isdn_tty_change_speed(info);
1072
Jiri Slaby48decc12012-04-02 13:53:54 +02001073 info->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
1075 info->send_outstanding = 0;
1076 return 0;
1077}
1078
1079/*
1080 * This routine will shutdown a serial port; interrupts are disabled, and
1081 * DTR is dropped if the hangup on close termio flag is on.
1082 */
1083static void
Joe Perches475be4d2012-02-19 19:52:38 -08001084isdn_tty_shutdown(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Jiri Slaby48decc12012-04-02 13:53:54 +02001086 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return;
1088#ifdef ISDN_DEBUG_MODEM_OPEN
1089 printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
1090#endif
1091 isdn_unlock_drivers();
1092 info->msr &= ~UART_MSR_RI;
Alan Coxadc8d742012-07-14 15:31:47 +01001093 if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
1095 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1096 isdn_tty_modem_reset_regs(info, 0);
1097#ifdef ISDN_DEBUG_MODEM_HUP
1098 printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
1099#endif
1100 isdn_tty_modem_hup(info, 1);
1101 }
1102 }
Jiri Slabyba432942012-04-02 13:53:57 +02001103 if (info->port.tty)
1104 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Jiri Slaby48decc12012-04-02 13:53:54 +02001106 info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109/* isdn_tty_write() is the main send-routine. It is called from the upper
1110 * levels within the kernel to perform sending data. Depending on the
1111 * online-flag it either directs output to the at-command-interpreter or
1112 * to the lower level. Additional tasks done here:
1113 * - If online, check for escape-sequence (+++)
1114 * - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
1115 * - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
1116 * - If dialing, abort dial.
1117 */
1118static int
Joe Perches475be4d2012-02-19 19:52:38 -08001119isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 int c;
1122 int total = 0;
1123 modem_info *info = (modem_info *) tty->driver_data;
1124 atemu *m = &info->emu;
1125
1126 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write"))
1127 return 0;
1128 /* See isdn_tty_senddown() */
1129 atomic_inc(&info->xmit_lock);
1130 while (1) {
1131 c = count;
1132 if (c > info->xmit_size - info->xmit_count)
1133 c = info->xmit_size - info->xmit_count;
1134 if (info->isdn_driver >= 0 && c > dev->drv[info->isdn_driver]->maxbufsize)
1135 c = dev->drv[info->isdn_driver]->maxbufsize;
1136 if (c <= 0)
1137 break;
1138 if ((info->online > 1)
1139#ifdef CONFIG_ISDN_AUDIO
1140 || (info->vonline & 3)
1141#endif
1142 ) {
1143#ifdef CONFIG_ISDN_AUDIO
1144 if (!info->vonline)
1145#endif
1146 isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
1147 &(m->pluscount),
1148 &(m->lastplus));
Jiri Slaby82e46b32012-04-02 13:53:58 +02001149 memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150#ifdef CONFIG_ISDN_AUDIO
1151 if (info->vonline) {
1152 int cc = isdn_tty_handleDLEdown(info, m, c);
1153 if (info->vonline & 2) {
1154 if (!cc) {
1155 /* If DLE decoding results in zero-transmit, but
1156 * c originally was non-zero, do a wakeup.
1157 */
1158 tty_wakeup(tty);
1159 info->msr |= UART_MSR_CTS;
1160 info->lsr |= UART_LSR_TEMT;
1161 }
1162 info->xmit_count += cc;
1163 }
1164 if ((info->vonline & 3) == 1) {
1165 /* Do NOT handle Ctrl-Q or Ctrl-S
1166 * when in full-duplex audio mode.
1167 */
1168 if (isdn_tty_end_vrx(buf, c)) {
1169 info->vonline &= ~1;
1170#ifdef ISDN_DEBUG_MODEM_VOICE
1171 printk(KERN_DEBUG
1172 "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
1173 info->line);
1174#endif
1175 isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
1176 }
1177 }
1178 } else
Joe Perches475be4d2012-02-19 19:52:38 -08001179 if (TTY_IS_FCLASS1(info)) {
1180 int cc = isdn_tty_handleDLEdown(info, m, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Joe Perches475be4d2012-02-19 19:52:38 -08001182 if (info->vonline & 4) { /* ETX seen */
1183 isdn_ctrl c;
1184
1185 c.command = ISDN_CMD_FAXCMD;
1186 c.driver = info->isdn_driver;
1187 c.arg = info->isdn_channel;
1188 c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
1189 c.parm.aux.subcmd = ETX;
1190 isdn_command(&c);
1191 }
1192 info->vonline = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193#ifdef ISDN_DEBUG_MODEM_VOICE
Joe Perches475be4d2012-02-19 19:52:38 -08001194 printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001196 info->xmit_count += cc;
1197 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001199 info->xmit_count += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 } else {
1201 info->msr |= UART_MSR_CTS;
1202 info->lsr |= UART_LSR_TEMT;
1203 if (info->dialing) {
1204 info->dialing = 0;
1205#ifdef ISDN_DEBUG_MODEM_HUP
1206 printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
1207#endif
1208 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
1209 isdn_tty_modem_hup(info, 1);
1210 } else
1211 c = isdn_tty_edit_at(buf, c, info);
1212 }
1213 buf += c;
1214 count -= c;
1215 total += c;
1216 }
1217 atomic_dec(&info->xmit_lock);
David S. Millerb03efcf2005-07-08 14:57:23 -07001218 if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
1220 isdn_tty_senddown(info);
1221 isdn_tty_tint(info);
1222 }
1223 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1224 }
1225 return total;
1226}
1227
1228static int
1229isdn_tty_write_room(struct tty_struct *tty)
1230{
1231 modem_info *info = (modem_info *) tty->driver_data;
1232 int ret;
1233
1234 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write_room"))
1235 return 0;
1236 if (!info->online)
1237 return info->xmit_size;
1238 ret = info->xmit_size - info->xmit_count;
1239 return (ret < 0) ? 0 : ret;
1240}
1241
1242static int
1243isdn_tty_chars_in_buffer(struct tty_struct *tty)
1244{
1245 modem_info *info = (modem_info *) tty->driver_data;
1246
1247 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_chars_in_buffer"))
1248 return 0;
1249 if (!info->online)
1250 return 0;
1251 return (info->xmit_count);
1252}
1253
1254static void
1255isdn_tty_flush_buffer(struct tty_struct *tty)
1256{
1257 modem_info *info;
1258
1259 if (!tty) {
1260 return;
1261 }
1262 info = (modem_info *) tty->driver_data;
1263 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
1264 return;
1265 }
1266 isdn_tty_cleanup_xmit(info);
1267 info->xmit_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 tty_wakeup(tty);
1269}
1270
1271static void
1272isdn_tty_flush_chars(struct tty_struct *tty)
1273{
1274 modem_info *info = (modem_info *) tty->driver_data;
1275
1276 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
1277 return;
David S. Millerb03efcf2005-07-08 14:57:23 -07001278 if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
1280}
1281
1282/*
1283 * ------------------------------------------------------------
1284 * isdn_tty_throttle()
1285 *
1286 * This routine is called by the upper-layer tty layer to signal that
1287 * incoming characters should be throttled.
1288 * ------------------------------------------------------------
1289 */
1290static void
1291isdn_tty_throttle(struct tty_struct *tty)
1292{
1293 modem_info *info = (modem_info *) tty->driver_data;
1294
1295 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_throttle"))
1296 return;
1297 if (I_IXOFF(tty))
1298 info->x_char = STOP_CHAR(tty);
1299 info->mcr &= ~UART_MCR_RTS;
1300}
1301
1302static void
1303isdn_tty_unthrottle(struct tty_struct *tty)
1304{
1305 modem_info *info = (modem_info *) tty->driver_data;
1306
1307 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_unthrottle"))
1308 return;
1309 if (I_IXOFF(tty)) {
1310 if (info->x_char)
1311 info->x_char = 0;
1312 else
1313 info->x_char = START_CHAR(tty);
1314 }
1315 info->mcr |= UART_MCR_RTS;
1316}
1317
1318/*
1319 * ------------------------------------------------------------
1320 * isdn_tty_ioctl() and friends
1321 * ------------------------------------------------------------
1322 */
1323
1324/*
1325 * isdn_tty_get_lsr_info - get line status register info
1326 *
1327 * Purpose: Let user call ioctl() to get info when the UART physically
1328 * is emptied. On bus types like RS485, the transmitter must
1329 * release the bus after transmitting. This must be done when
1330 * the transmit shift register is empty, not be done when the
1331 * transmit holding register is empty. This functionality
1332 * allows RS485 driver to be written in user space.
1333 */
1334static int
Joe Perches475be4d2012-02-19 19:52:38 -08001335isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 u_char status;
1338 uint result;
1339
1340 status = info->lsr;
1341 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1342 return put_user(result, value);
1343}
1344
1345
1346static int
Alan Cox60b33c12011-02-14 16:26:14 +00001347isdn_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 modem_info *info = (modem_info *) tty->driver_data;
1350 u_char control, status;
1351
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001352 if (isdn_tty_paranoia_check(info, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -ENODEV;
Peter Hurley18900ca2016-04-09 17:06:48 -07001354 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EIO;
1356
Arnd Bergmann72250d42010-09-14 09:35:04 +00001357 mutex_lock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358#ifdef ISDN_DEBUG_MODEM_IOCTL
1359 printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
1360#endif
1361
1362 control = info->mcr;
1363 status = info->msr;
Arnd Bergmann72250d42010-09-14 09:35:04 +00001364 mutex_unlock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
Joe Perches475be4d2012-02-19 19:52:38 -08001366 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1367 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1368 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1369 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1370 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373static int
Alan Cox20b9d172011-02-14 16:26:50 +00001374isdn_tty_tiocmset(struct tty_struct *tty,
Joe Perches475be4d2012-02-19 19:52:38 -08001375 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 modem_info *info = (modem_info *) tty->driver_data;
1378
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001379 if (isdn_tty_paranoia_check(info, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return -ENODEV;
Peter Hurley18900ca2016-04-09 17:06:48 -07001381 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return -EIO;
1383
1384#ifdef ISDN_DEBUG_MODEM_IOCTL
1385 printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
1386#endif
1387
Arnd Bergmann72250d42010-09-14 09:35:04 +00001388 mutex_lock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (set & TIOCM_RTS)
1390 info->mcr |= UART_MCR_RTS;
1391 if (set & TIOCM_DTR) {
1392 info->mcr |= UART_MCR_DTR;
1393 isdn_tty_modem_ncarrier(info);
1394 }
1395
1396 if (clear & TIOCM_RTS)
1397 info->mcr &= ~UART_MCR_RTS;
1398 if (clear & TIOCM_DTR) {
1399 info->mcr &= ~UART_MCR_DTR;
1400 if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
1401 isdn_tty_modem_reset_regs(info, 0);
1402#ifdef ISDN_DEBUG_MODEM_HUP
1403 printk(KERN_DEBUG "Mhup in TIOCMSET\n");
1404#endif
1405 if (info->online)
1406 info->ncarrier = 1;
1407 isdn_tty_modem_hup(info, 1);
1408 }
1409 }
Arnd Bergmann72250d42010-09-14 09:35:04 +00001410 mutex_unlock(&modem_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return 0;
1412}
1413
1414static int
Alan Cox6caa76b2011-02-14 16:27:22 +00001415isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 modem_info *info = (modem_info *) tty->driver_data;
1418 int retval;
1419
1420 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
1421 return -ENODEV;
Peter Hurley18900ca2016-04-09 17:06:48 -07001422 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EIO;
1424 switch (cmd) {
Joe Perches475be4d2012-02-19 19:52:38 -08001425 case TCSBRK: /* SVID version: non-zero arg --> no break */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001427 printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001429 retval = tty_check_change(tty);
1430 if (retval)
1431 return retval;
1432 tty_wait_until_sent(tty, 0);
1433 return 0;
1434 case TCSBRKP: /* support for POSIX tcsendbreak() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001436 printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001438 retval = tty_check_change(tty);
1439 if (retval)
1440 return retval;
1441 tty_wait_until_sent(tty, 0);
1442 return 0;
1443 case TIOCSERGETLSR: /* Get line status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001445 printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001447 return isdn_tty_get_lsr_info(info, (uint __user *) arg);
1448 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449#ifdef ISDN_DEBUG_MODEM_IOCTL
Joe Perches475be4d2012-02-19 19:52:38 -08001450 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001452 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 return 0;
1455}
1456
1457static void
Alan Cox606d0992006-12-08 02:38:45 -08001458isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 modem_info *info = (modem_info *) tty->driver_data;
1461
1462 if (!old_termios)
1463 isdn_tty_change_speed(info);
1464 else {
Alan Coxadc8d742012-07-14 15:31:47 +01001465 if (tty->termios.c_cflag == old_termios->c_cflag &&
1466 tty->termios.c_ispeed == old_termios->c_ispeed &&
1467 tty->termios.c_ospeed == old_termios->c_ospeed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return;
1469 isdn_tty_change_speed(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471}
1472
1473/*
1474 * ------------------------------------------------------------
1475 * isdn_tty_open() and friends
1476 * ------------------------------------------------------------
1477 */
Jiri Slaby042b9e72012-04-02 13:54:01 +02001478
Jiri Slabyf7e04052012-08-07 21:47:59 +02001479static int isdn_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1480{
1481 modem_info *info = &dev->mdm.info[tty->index];
1482
1483 if (isdn_tty_paranoia_check(info, tty->name, __func__))
1484 return -ENODEV;
1485
1486 tty->driver_data = info;
1487
1488 return tty_port_install(&info->port, driver, tty);
1489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491/*
1492 * This routine is called whenever a serial port is opened. It
1493 * enables interrupts for a serial port, linking in its async structure into
1494 * the IRQ chain. It also performs the serial-specific
1495 * initialization for the tty structure.
1496 */
1497static int
1498isdn_tty_open(struct tty_struct *tty, struct file *filp)
1499{
Jiri Slabyf7e04052012-08-07 21:47:59 +02001500 modem_info *info = tty->driver_data;
1501 struct tty_port *port = &info->port;
Jiri Slaby410235f2012-03-05 14:52:01 +01001502 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504#ifdef ISDN_DEBUG_MODEM_OPEN
Joe Perches475be4d2012-02-19 19:52:38 -08001505 printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
Jiri Slaby265d6f02012-04-02 13:53:59 +02001506 port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507#endif
Jiri Slaby265d6f02012-04-02 13:53:59 +02001508 port->count++;
Jiri Slaby265d6f02012-04-02 13:53:59 +02001509 port->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * Start up serial port
1512 */
1513 retval = isdn_tty_startup(info);
1514 if (retval) {
1515#ifdef ISDN_DEBUG_MODEM_OPEN
1516 printk(KERN_DEBUG "isdn_tty_open return after startup\n");
1517#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return retval;
1519 }
Jiri Slaby875d54a2012-04-02 13:54:02 +02001520 retval = tty_port_block_til_ready(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (retval) {
1522#ifdef ISDN_DEBUG_MODEM_OPEN
1523 printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
1524#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return retval;
1526 }
1527#ifdef ISDN_DEBUG_MODEM_OPEN
1528 printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
1529#endif
1530 dev->modempoll++;
1531#ifdef ISDN_DEBUG_MODEM_OPEN
1532 printk(KERN_DEBUG "isdn_tty_open normal exit\n");
1533#endif
1534 return 0;
1535}
1536
1537static void
1538isdn_tty_close(struct tty_struct *tty, struct file *filp)
1539{
1540 modem_info *info = (modem_info *) tty->driver_data;
Jiri Slaby265d6f02012-04-02 13:53:59 +02001541 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 ulong timeout;
1543
1544 if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
1545 return;
1546 if (tty_hung_up_p(filp)) {
1547#ifdef ISDN_DEBUG_MODEM_OPEN
1548 printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
1549#endif
1550 return;
1551 }
Jiri Slaby265d6f02012-04-02 13:53:59 +02001552 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /*
1554 * Uh, oh. tty->count is 1, which means that the tty
1555 * structure will be freed. Info->count should always
1556 * be one in these conditions. If it's greater than
1557 * one, we've got real problems, since it means the
1558 * serial port won't be shutdown.
1559 */
1560 printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
Jiri Slaby265d6f02012-04-02 13:53:59 +02001561 "info->count is %d\n", port->count);
1562 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Jiri Slaby265d6f02012-04-02 13:53:59 +02001564 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
Jiri Slaby265d6f02012-04-02 13:53:59 +02001566 info->line, port->count);
1567 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Jiri Slaby265d6f02012-04-02 13:53:59 +02001569 if (port->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570#ifdef ISDN_DEBUG_MODEM_OPEN
1571 printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
1572#endif
1573 return;
1574 }
Peter Hurleya657eec2016-01-10 14:51:39 -08001575 info->closing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 tty->closing = 1;
1578 /*
1579 * At this point we stop accepting input. To do this, we
1580 * disable the receive line status interrupts, and tell the
1581 * interrupt driver to stop checking the data ready bit in the
1582 * line status register.
1583 */
Jiri Slaby265d6f02012-04-02 13:53:59 +02001584 if (port->flags & ASYNC_INITIALIZED) {
Peter Hurley79c1faa2015-10-10 16:00:51 -04001585 tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*
1587 * Before we drop DTR, make sure the UART transmitter
1588 * has completely drained; this is especially
1589 * important if there is a transmit FIFO!
1590 */
1591 timeout = jiffies + HZ;
1592 while (!(info->lsr & UART_LSR_TEMT)) {
Nishanth Aravamudan24763c42005-11-07 01:01:16 -08001593 schedule_timeout_interruptible(20);
Joe Perches475be4d2012-02-19 19:52:38 -08001594 if (time_after(jiffies, timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 break;
1596 }
1597 }
1598 dev->modempoll--;
1599 isdn_tty_shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001600 isdn_tty_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 tty_ldisc_flush(tty);
Jiri Slaby265d6f02012-04-02 13:53:59 +02001602 port->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 info->ncarrier = 0;
Jiri Slaby4330d662012-04-02 13:54:00 +02001604
1605 tty_port_close_end(port, tty);
Peter Hurleya657eec2016-01-10 14:51:39 -08001606 info->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607#ifdef ISDN_DEBUG_MODEM_OPEN
1608 printk(KERN_DEBUG "isdn_tty_close normal exit\n");
1609#endif
1610}
1611
1612/*
1613 * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1614 */
1615static void
1616isdn_tty_hangup(struct tty_struct *tty)
1617{
1618 modem_info *info = (modem_info *) tty->driver_data;
Jiri Slaby265d6f02012-04-02 13:53:59 +02001619 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
1622 return;
1623 isdn_tty_shutdown(info);
Jiri Slaby265d6f02012-04-02 13:53:59 +02001624 port->count = 0;
Peter Hurley807c8d812016-04-09 17:53:22 -07001625 tty_port_set_active(port, 0);
Jiri Slaby265d6f02012-04-02 13:53:59 +02001626 port->tty = NULL;
1627 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
1630/* This routine initializes all emulator-data.
1631 */
1632static void
Joe Perches475be4d2012-02-19 19:52:38 -08001633isdn_tty_reset_profile(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 m->profile[0] = 0;
1636 m->profile[1] = 0;
1637 m->profile[2] = 43;
1638 m->profile[3] = 13;
1639 m->profile[4] = 10;
1640 m->profile[5] = 8;
1641 m->profile[6] = 3;
1642 m->profile[7] = 60;
1643 m->profile[8] = 2;
1644 m->profile[9] = 6;
1645 m->profile[10] = 7;
1646 m->profile[11] = 70;
1647 m->profile[12] = 0x45;
1648 m->profile[13] = 4;
1649 m->profile[14] = ISDN_PROTO_L2_X75I;
1650 m->profile[15] = ISDN_PROTO_L3_TRANS;
1651 m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
1652 m->profile[17] = ISDN_MODEM_WINSIZE;
1653 m->profile[18] = 4;
1654 m->profile[19] = 0;
1655 m->profile[20] = 0;
1656 m->profile[23] = 0;
1657 m->pmsn[0] = '\0';
1658 m->plmsn[0] = '\0';
1659}
1660
1661#ifdef CONFIG_ISDN_AUDIO
1662static void
Joe Perches475be4d2012-02-19 19:52:38 -08001663isdn_tty_modem_reset_vpar(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 m->vpar[0] = 2; /* Voice-device (2 = phone line) */
1666 m->vpar[1] = 0; /* Silence detection level (0 = none ) */
1667 m->vpar[2] = 70; /* Silence interval (7 sec. ) */
1668 m->vpar[3] = 2; /* Compression type (1 = ADPCM-2 ) */
1669 m->vpar[4] = 0; /* DTMF detection level (0 = softcode ) */
1670 m->vpar[5] = 8; /* DTMF interval (8 * 5 ms. ) */
1671}
1672#endif
1673
1674#ifdef CONFIG_ISDN_TTY_FAX
1675static void
Joe Perches475be4d2012-02-19 19:52:38 -08001676isdn_tty_modem_reset_faxpar(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 T30_s *f = info->fax;
1679
1680 f->code = 0;
1681 f->phase = ISDN_FAX_PHASE_IDLE;
1682 f->direction = 0;
1683 f->resolution = 1; /* fine */
1684 f->rate = 5; /* 14400 bit/s */
1685 f->width = 0;
1686 f->length = 0;
1687 f->compression = 0;
1688 f->ecm = 0;
1689 f->binary = 0;
1690 f->scantime = 0;
1691 memset(&f->id[0], 32, FAXIDLEN - 1);
1692 f->id[FAXIDLEN - 1] = 0;
1693 f->badlin = 0;
1694 f->badmul = 0;
1695 f->bor = 0;
1696 f->nbc = 0;
1697 f->cq = 0;
1698 f->cr = 0;
1699 f->ctcrty = 0;
1700 f->minsp = 0;
1701 f->phcto = 30;
1702 f->rel = 0;
1703 memset(&f->pollid[0], 32, FAXIDLEN - 1);
1704 f->pollid[FAXIDLEN - 1] = 0;
1705}
1706#endif
1707
1708static void
Joe Perches475be4d2012-02-19 19:52:38 -08001709isdn_tty_modem_reset_regs(modem_info *info, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 atemu *m = &info->emu;
1712 if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
1713 memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
1714 memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
1715 memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
1716 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
1717 }
1718#ifdef CONFIG_ISDN_AUDIO
1719 isdn_tty_modem_reset_vpar(m);
1720#endif
1721#ifdef CONFIG_ISDN_TTY_FAX
1722 isdn_tty_modem_reset_faxpar(info);
1723#endif
1724 m->mdmcmdl = 0;
1725}
1726
1727static void
Joe Perches475be4d2012-02-19 19:52:38 -08001728modem_write_profile(atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
1731 memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
1732 memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
1733 if (dev->profd)
1734 send_sig(SIGIO, dev->profd, 1);
1735}
1736
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001737static const struct tty_operations modem_ops = {
Jiri Slabyf7e04052012-08-07 21:47:59 +02001738 .install = isdn_tty_install,
Joe Perches475be4d2012-02-19 19:52:38 -08001739 .open = isdn_tty_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 .close = isdn_tty_close,
1741 .write = isdn_tty_write,
1742 .flush_chars = isdn_tty_flush_chars,
1743 .write_room = isdn_tty_write_room,
1744 .chars_in_buffer = isdn_tty_chars_in_buffer,
1745 .flush_buffer = isdn_tty_flush_buffer,
1746 .ioctl = isdn_tty_ioctl,
1747 .throttle = isdn_tty_throttle,
1748 .unthrottle = isdn_tty_unthrottle,
1749 .set_termios = isdn_tty_set_termios,
1750 .hangup = isdn_tty_hangup,
1751 .tiocmget = isdn_tty_tiocmget,
1752 .tiocmset = isdn_tty_tiocmset,
1753};
1754
Jiri Slaby042b9e72012-04-02 13:54:01 +02001755static int isdn_tty_carrier_raised(struct tty_port *port)
1756{
1757 modem_info *info = container_of(port, modem_info, port);
1758 return info->msr & UART_MSR_DCD;
1759}
1760
1761static const struct tty_port_operations isdn_tty_port_ops = {
1762 .carrier_raised = isdn_tty_carrier_raised,
1763};
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765int
1766isdn_tty_modem_init(void)
1767{
1768 isdn_modem_t *m;
1769 int i, retval;
1770 modem_info *info;
1771
1772 m = &dev->mdm;
1773 m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
1774 if (!m->tty_modem)
1775 return -ENOMEM;
1776 m->tty_modem->name = "ttyI";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 m->tty_modem->major = ISDN_TTY_MAJOR;
1778 m->tty_modem->minor_start = 0;
1779 m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
1780 m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
1781 m->tty_modem->init_termios = tty_std_termios;
1782 m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Jiri Slabyc4d6ebe2012-08-07 21:47:48 +02001783 m->tty_modem->flags = TTY_DRIVER_REAL_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 m->tty_modem->driver_name = "isdn_tty";
1785 tty_set_operations(m->tty_modem, &modem_ops);
1786 retval = tty_register_driver(m->tty_modem);
1787 if (retval) {
1788 printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
1789 goto err;
1790 }
1791 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1792 info = &m->info[i];
1793#ifdef CONFIG_ISDN_TTY_FAX
1794 if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
1795 printk(KERN_ERR "Could not allocate fax t30-buffer\n");
1796 retval = -ENOMEM;
1797 goto err_unregister;
1798 }
1799#endif
Jiri Slaby48decc12012-04-02 13:53:54 +02001800 tty_port_init(&info->port);
Jiri Slaby042b9e72012-04-02 13:54:01 +02001801 info->port.ops = &isdn_tty_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 spin_lock_init(&info->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 sprintf(info->last_cause, "0000");
1804 sprintf(info->last_num, "none");
1805 info->last_dir = 0;
1806 info->last_lhup = 1;
1807 info->last_l2 = -1;
1808 info->last_si = 0;
1809 isdn_tty_reset_profile(&info->emu);
1810 isdn_tty_modem_reset_regs(info, 1);
1811 info->magic = ISDN_ASYNC_MAGIC;
1812 info->line = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 info->isdn_driver = -1;
1815 info->isdn_channel = -1;
1816 info->drv_index = -1;
1817 info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
1818 init_timer(&info->nc_timer);
1819 info->nc_timer.function = isdn_tty_modem_do_ncarrier;
1820 info->nc_timer.data = (unsigned long) info;
1821 skb_queue_head_init(&info->xmit_queue);
1822#ifdef CONFIG_ISDN_AUDIO
1823 skb_queue_head_init(&info->dtmf_queue);
1824#endif
Jiri Slaby82e46b32012-04-02 13:53:58 +02001825 info->port.xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5,
1826 GFP_KERNEL);
1827 if (!info->port.xmit_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
1829 retval = -ENOMEM;
1830 goto err_unregister;
1831 }
1832 /* Make room for T.70 header */
Jiri Slaby82e46b32012-04-02 13:53:58 +02001833 info->port.xmit_buf += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835 return 0;
1836err_unregister:
1837 for (i--; i >= 0; i--) {
1838 info = &m->info[i];
1839#ifdef CONFIG_ISDN_TTY_FAX
1840 kfree(info->fax);
1841#endif
Jiri Slaby82e46b32012-04-02 13:53:58 +02001842 kfree(info->port.xmit_buf - 4);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001843 info->port.xmit_buf = NULL;
1844 tty_port_destroy(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846 tty_unregister_driver(m->tty_modem);
Joe Perches475be4d2012-02-19 19:52:38 -08001847err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 put_tty_driver(m->tty_modem);
1849 m->tty_modem = NULL;
1850 return retval;
1851}
1852
1853void
1854isdn_tty_exit(void)
1855{
1856 modem_info *info;
1857 int i;
1858
1859 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1860 info = &dev->mdm.info[i];
1861 isdn_tty_cleanup_xmit(info);
1862#ifdef CONFIG_ISDN_TTY_FAX
1863 kfree(info->fax);
1864#endif
Jiri Slaby82e46b32012-04-02 13:53:58 +02001865 kfree(info->port.xmit_buf - 4);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001866 info->port.xmit_buf = NULL;
1867 tty_port_destroy(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 tty_unregister_driver(dev->mdm.tty_modem);
1870 put_tty_driver(dev->mdm.tty_modem);
1871 dev->mdm.tty_modem = NULL;
1872}
1873
1874
1875/*
1876 * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
1877 * match the MSN against the MSNs (glob patterns) defined for tty_emulator,
1878 * and return 0 for match, 1 for no match, 2 if MSN could match if longer.
1879 */
1880
1881static int
1882isdn_tty_match_icall(char *cid, atemu *emu, int di)
1883{
1884#ifdef ISDN_DEBUG_MODEM_ICALL
1885 printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
1886 emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
1887 emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
1888#endif
1889 if (strlen(emu->lmsn)) {
1890 char *p = emu->lmsn;
1891 char *q;
1892 int tmp;
1893 int ret = 0;
1894
1895 while (1) {
1896 if ((q = strchr(p, ';')))
1897 *q = '\0';
1898 if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
1899 ret = tmp;
1900#ifdef ISDN_DEBUG_MODEM_ICALL
1901 printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
1902 p, isdn_map_eaz2msn(emu->msn, di), tmp);
1903#endif
1904 if (q) {
1905 *q = ';';
1906 p = q;
1907 p++;
1908 }
1909 if (!tmp)
1910 return 0;
1911 if (!q)
1912 break;
1913 }
1914 return ret;
1915 } else {
1916 int tmp;
1917 tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
1918#ifdef ISDN_DEBUG_MODEM_ICALL
Joe Perches475be4d2012-02-19 19:52:38 -08001919 printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
1920 isdn_map_eaz2msn(emu->msn, di), tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921#endif
1922 return tmp;
1923 }
1924}
1925
1926/*
1927 * An incoming call-request has arrived.
1928 * Search the tty-devices for an appropriate device and bind
1929 * it to the ISDN-Channel.
1930 * Return:
1931 *
1932 * 0 = No matching device found.
1933 * 1 = A matching device found.
1934 * 3 = No match found, but eventually would match, if
1935 * CID is longer.
1936 */
1937int
1938isdn_tty_find_icall(int di, int ch, setup_parm *setup)
1939{
1940 char *eaz;
1941 int i;
1942 int wret;
1943 int idx;
1944 int si1;
1945 int si2;
1946 char *nr;
1947 ulong flags;
1948
1949 if (!setup->phone[0]) {
1950 nr = "0";
1951 printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
1952 } else
1953 nr = setup->phone;
1954 si1 = (int) setup->si1;
1955 si2 = (int) setup->si2;
1956 if (!setup->eazmsn[0]) {
1957 printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
1958 eaz = "0";
1959 } else
1960 eaz = setup->eazmsn;
1961#ifdef ISDN_DEBUG_MODEM_ICALL
1962 printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
1963#endif
1964 wret = 0;
1965 spin_lock_irqsave(&dev->lock, flags);
1966 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1967 modem_info *info = &dev->mdm.info[i];
1968
Jiri Slaby1b05f032012-04-02 13:53:56 +02001969 if (info->port.count == 0)
Joe Perches475be4d2012-02-19 19:52:38 -08001970 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) && /* SI1 is matching */
1972 (info->emu.mdmreg[REG_SI2] == si2)) { /* SI2 is matching */
1973 idx = isdn_dc2minor(di, ch);
1974#ifdef ISDN_DEBUG_MODEM_ICALL
1975 printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
1976 printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
Jiri Slaby48decc12012-04-02 13:53:54 +02001977 info->port.flags, info->isdn_driver,
1978 info->isdn_channel, dev->usage[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979#endif
1980 if (
1981#ifndef FIX_FILE_TRANSFER
Peter Hurley807c8d812016-04-09 17:53:22 -07001982 tty_port_active(&info->port) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983#endif
1984 (info->isdn_driver == -1) &&
1985 (info->isdn_channel == -1) &&
1986 (USG_NONE(dev->usage[idx]))) {
1987 int matchret;
1988
1989 if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
1990 wret = matchret;
1991 if (!matchret) { /* EAZ is matching */
1992 info->isdn_driver = di;
1993 info->isdn_channel = ch;
1994 info->drv_index = idx;
1995 dev->m_idx[idx] = info->line;
1996 dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
Joe Perches475be4d2012-02-19 19:52:38 -08001997 dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 strcpy(dev->num[idx], nr);
1999 strcpy(info->emu.cpn, eaz);
2000 info->emu.mdmreg[REG_SI1I] = si2bit[si1];
2001 info->emu.mdmreg[REG_PLAN] = setup->plan;
2002 info->emu.mdmreg[REG_SCREEN] = setup->screen;
2003 isdn_info_update();
2004 spin_unlock_irqrestore(&dev->lock, flags);
2005 printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
2006 info->line);
2007 info->msr |= UART_MSR_RI;
2008 isdn_tty_modem_result(RESULT_RING, info);
2009 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
2010 return 1;
2011 }
2012 }
2013 }
2014 }
2015 spin_unlock_irqrestore(&dev->lock, flags);
2016 printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
Joe Perches475be4d2012-02-19 19:52:38 -08002017 ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
2018 return (wret == 2) ? 3 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021int
2022isdn_tty_stat_callback(int i, isdn_ctrl *c)
2023{
2024 int mi;
2025 modem_info *info;
2026 char *e;
2027
2028 if (i < 0)
2029 return 0;
2030 if ((mi = dev->m_idx[i]) >= 0) {
2031 info = &dev->mdm.info[mi];
2032 switch (c->command) {
Joe Perches475be4d2012-02-19 19:52:38 -08002033 case ISDN_STAT_CINF:
2034 printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
2035 info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
2036 if (e == (char *)c->parm.num)
2037 info->emu.charge = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Joe Perches475be4d2012-02-19 19:52:38 -08002039 break;
2040 case ISDN_STAT_BSENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041#ifdef ISDN_TTY_STAT_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08002042 printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002044 if ((info->isdn_driver == c->driver) &&
2045 (info->isdn_channel == c->arg)) {
2046 info->msr |= UART_MSR_CTS;
2047 if (info->send_outstanding)
2048 if (!(--info->send_outstanding))
2049 info->lsr |= UART_LSR_TEMT;
2050 isdn_tty_tint(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 1;
Joe Perches475be4d2012-02-19 19:52:38 -08002052 }
2053 break;
2054 case ISDN_STAT_CAUSE:
2055#ifdef ISDN_TTY_STAT_DEBUG
2056 printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
2057#endif
2058 /* Signal cause to tty-device */
2059 strncpy(info->last_cause, c->parm.num, 5);
2060 return 1;
2061 case ISDN_STAT_DISPLAY:
2062#ifdef ISDN_TTY_STAT_DEBUG
2063 printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
2064#endif
2065 /* Signal display to tty-device */
2066 if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
2067 !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
2068 isdn_tty_at_cout("\r\n", info);
2069 isdn_tty_at_cout("DISPLAY: ", info);
2070 isdn_tty_at_cout(c->parm.display, info);
2071 isdn_tty_at_cout("\r\n", info);
2072 }
2073 return 1;
2074 case ISDN_STAT_DCONN:
2075#ifdef ISDN_TTY_STAT_DEBUG
2076 printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
2077#endif
Peter Hurley807c8d812016-04-09 17:53:22 -07002078 if (tty_port_active(&info->port)) {
Joe Perches475be4d2012-02-19 19:52:38 -08002079 if (info->dialing == 1) {
2080 info->dialing = 2;
2081 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Joe Perches475be4d2012-02-19 19:52:38 -08002083 }
2084 break;
2085 case ISDN_STAT_DHUP:
2086#ifdef ISDN_TTY_STAT_DEBUG
2087 printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
2088#endif
Peter Hurley807c8d812016-04-09 17:53:22 -07002089 if (tty_port_active(&info->port)) {
Joe Perches475be4d2012-02-19 19:52:38 -08002090 if (info->dialing == 1)
2091 isdn_tty_modem_result(RESULT_BUSY, info);
2092 if (info->dialing > 1)
2093 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
2094 info->dialing = 0;
2095#ifdef ISDN_DEBUG_MODEM_HUP
2096 printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
2097#endif
2098 isdn_tty_modem_hup(info, 0);
2099 return 1;
2100 }
2101 break;
2102 case ISDN_STAT_BCONN:
2103#ifdef ISDN_TTY_STAT_DEBUG
2104 printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
2105#endif
2106 /* Wake up any processes waiting
2107 * for incoming call of this device when
2108 * DCD follow the state of incoming carrier
2109 */
Jiri Slaby1b05f032012-04-02 13:53:56 +02002110 if (info->port.blocked_open &&
Joe Perches475be4d2012-02-19 19:52:38 -08002111 (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
Jiri Slabyc6e92b62012-04-02 13:53:55 +02002112 wake_up_interruptible(&info->port.open_wait);
Joe Perches475be4d2012-02-19 19:52:38 -08002113 }
2114
2115 /* Schedule CONNECT-Message to any tty
2116 * waiting for it and
2117 * set DCD-bit of its modem-status.
2118 */
Peter Hurley807c8d812016-04-09 17:53:22 -07002119 if (tty_port_active(&info->port) ||
Jiri Slaby1b05f032012-04-02 13:53:56 +02002120 (info->port.blocked_open &&
2121 (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
Joe Perches475be4d2012-02-19 19:52:38 -08002122 info->msr |= UART_MSR_DCD;
2123 info->emu.charge = 0;
2124 if (info->dialing & 0xf)
2125 info->last_dir = 1;
2126 else
2127 info->last_dir = 0;
2128 info->dialing = 0;
2129 info->rcvsched = 1;
2130 if (USG_MODEM(dev->usage[i])) {
2131 if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
2132 strcpy(info->emu.connmsg, c->parm.num);
2133 isdn_tty_modem_result(RESULT_CONNECT, info);
2134 } else
2135 isdn_tty_modem_result(RESULT_CONNECT64000, info);
2136 }
2137 if (USG_VOICE(dev->usage[i]))
2138 isdn_tty_modem_result(RESULT_VCON, info);
2139 return 1;
2140 }
2141 break;
2142 case ISDN_STAT_BHUP:
2143#ifdef ISDN_TTY_STAT_DEBUG
2144 printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
2145#endif
Peter Hurley807c8d812016-04-09 17:53:22 -07002146 if (tty_port_active(&info->port)) {
Joe Perches475be4d2012-02-19 19:52:38 -08002147#ifdef ISDN_DEBUG_MODEM_HUP
2148 printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
2149#endif
2150 isdn_tty_modem_hup(info, 0);
2151 return 1;
2152 }
2153 break;
2154 case ISDN_STAT_NODCH:
2155#ifdef ISDN_TTY_STAT_DEBUG
2156 printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
2157#endif
Peter Hurley807c8d812016-04-09 17:53:22 -07002158 if (tty_port_active(&info->port)) {
Joe Perches475be4d2012-02-19 19:52:38 -08002159 if (info->dialing) {
2160 info->dialing = 0;
2161 info->last_l2 = -1;
2162 info->last_si = 0;
2163 sprintf(info->last_cause, "0000");
2164 isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
2165 }
2166 isdn_tty_modem_hup(info, 0);
2167 return 1;
2168 }
2169 break;
2170 case ISDN_STAT_UNLOAD:
2171#ifdef ISDN_TTY_STAT_DEBUG
2172 printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
2173#endif
2174 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2175 info = &dev->mdm.info[i];
2176 if (info->isdn_driver == c->driver) {
2177 if (info->online)
2178 isdn_tty_modem_hup(info, 1);
2179 }
2180 }
2181 return 1;
2182#ifdef CONFIG_ISDN_TTY_FAX
2183 case ISDN_STAT_FAXIND:
Peter Hurley807c8d812016-04-09 17:53:22 -07002184 if (tty_port_active(&info->port)) {
Joe Perches475be4d2012-02-19 19:52:38 -08002185 isdn_tty_fax_command(info, c);
2186 }
2187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#endif
2189#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002190 case ISDN_STAT_AUDIO:
Peter Hurley807c8d812016-04-09 17:53:22 -07002191 if (tty_port_active(&info->port)) {
Joe Perches475be4d2012-02-19 19:52:38 -08002192 switch (c->parm.num[0]) {
2193 case ISDN_AUDIO_DTMF:
2194 if (info->vonline) {
2195 isdn_audio_put_dle_code(info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 c->parm.num[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Joe Perches475be4d2012-02-19 19:52:38 -08002198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Joe Perches475be4d2012-02-19 19:52:38 -08002200 }
2201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202#endif
2203 }
2204 }
2205 return 0;
2206}
2207
2208/*********************************************************************
2209 Modem-Emulator-Routines
Joe Perches475be4d2012-02-19 19:52:38 -08002210*********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Joe Perches475be4d2012-02-19 19:52:38 -08002212#define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214/*
2215 * Put a message from the AT-emulator into receive-buffer of tty,
2216 * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
2217 */
2218void
Joe Perches475be4d2012-02-19 19:52:38 -08002219isdn_tty_at_cout(char *msg, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Jiri Slaby227434f2013-01-03 15:53:01 +01002221 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 atemu *m = &info->emu;
2223 char *p;
2224 char c;
2225 u_long flags;
2226 struct sk_buff *skb = NULL;
2227 char *sp = NULL;
Adrian Bunk1f4d4a82006-03-25 03:08:06 -08002228 int l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (!msg) {
2231 printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
2232 return;
2233 }
Adrian Bunk1f4d4a82006-03-25 03:08:06 -08002234
2235 l = strlen(msg);
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 spin_lock_irqsave(&info->readlock, flags);
Peter Hurleya657eec2016-01-10 14:51:39 -08002238 if (info->closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 spin_unlock_irqrestore(&info->readlock, flags);
2240 return;
2241 }
2242
Alan Cox33f0f882006-01-09 20:54:13 -08002243 /* use queue instead of direct, if online and */
2244 /* data is in queue or buffer is full */
Jiri Slaby227434f2013-01-03 15:53:01 +01002245 if (info->online && ((tty_buffer_request_room(port, l) < l) ||
Joe Perches475be4d2012-02-19 19:52:38 -08002246 !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
Alan Cox33f0f882006-01-09 20:54:13 -08002247 skb = alloc_skb(l, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (!skb) {
2249 spin_unlock_irqrestore(&info->readlock, flags);
2250 return;
2251 }
Alan Cox33f0f882006-01-09 20:54:13 -08002252 sp = skb_put(skb, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253#ifdef CONFIG_ISDN_AUDIO
2254 ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
2255 ISDN_AUDIO_SKB_LOCK(skb) = 0;
2256#endif
2257 }
2258
2259 for (p = msg; *p; p++) {
2260 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -08002261 case '\r':
2262 c = m->mdmreg[REG_CR];
2263 break;
2264 case '\n':
2265 c = m->mdmreg[REG_LF];
2266 break;
2267 case '\b':
2268 c = m->mdmreg[REG_BS];
2269 break;
2270 default:
2271 c = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273 if (skb) {
2274 *sp++ = c;
2275 } else {
Jiri Slaby92a19f92013-01-03 15:53:03 +01002276 if (tty_insert_flip_char(port, c, TTY_NORMAL) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279 }
2280 if (skb) {
2281 __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
2282 dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
2283 spin_unlock_irqrestore(&info->readlock, flags);
2284 /* Schedule dequeuing */
Alan Cox33f0f882006-01-09 20:54:13 -08002285 if (dev->modempoll && info->rcvsched)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
2287
2288 } else {
2289 spin_unlock_irqrestore(&info->readlock, flags);
Jiri Slaby2e124b42013-01-03 15:53:06 +01002290 tty_flip_buffer_push(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292}
2293
2294/*
2295 * Perform ATH Hangup
2296 */
2297static void
Joe Perches475be4d2012-02-19 19:52:38 -08002298isdn_tty_on_hook(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 if (info->isdn_channel >= 0) {
2301#ifdef ISDN_DEBUG_MODEM_HUP
2302 printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
2303#endif
2304 isdn_tty_modem_hup(info, 1);
2305 }
2306}
2307
2308static void
2309isdn_tty_off_hook(void)
2310{
2311 printk(KERN_DEBUG "isdn_tty_off_hook\n");
2312}
2313
Joe Perches475be4d2012-02-19 19:52:38 -08002314#define PLUSWAIT1 (HZ / 2) /* 0.5 sec. */
2315#define PLUSWAIT2 (HZ * 3 / 2) /* 1.5 sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317/*
2318 * Check Buffer for Modem-escape-sequence, activate timer-callback to
2319 * isdn_tty_modem_escape() if sequence found.
2320 *
2321 * Parameters:
2322 * p pointer to databuffer
2323 * plus escape-character
2324 * count length of buffer
2325 * pluscount count of valid escape-characters so far
2326 * lastplus timestamp of last character
2327 */
2328static void
Joe Perches475be4d2012-02-19 19:52:38 -08002329isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 u_long *lastplus)
2331{
2332 if (plus > 127)
2333 return;
2334 if (count > 3) {
2335 p += count - 3;
2336 count = 3;
2337 *pluscount = 0;
2338 }
2339 while (count > 0) {
2340 if (*(p++) == plus) {
2341 if ((*pluscount)++) {
2342 /* Time since last '+' > 0.5 sec. ? */
2343 if (time_after(jiffies, *lastplus + PLUSWAIT1))
2344 *pluscount = 1;
2345 } else {
2346 /* Time since last non-'+' < 1.5 sec. ? */
2347 if (time_before(jiffies, *lastplus + PLUSWAIT2))
2348 *pluscount = 0;
2349 }
2350 if ((*pluscount == 3) && (count == 1))
2351 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
2352 if (*pluscount > 3)
2353 *pluscount = 1;
2354 } else
2355 *pluscount = 0;
2356 *lastplus = jiffies;
2357 count--;
2358 }
2359}
2360
2361/*
2362 * Return result of AT-emulator to tty-receive-buffer, depending on
2363 * modem-register 12, bit 0 and 1.
2364 * For CONNECT-messages also switch to online-mode.
2365 * For RING-message handle auto-ATA if register 0 != 0
2366 */
2367
2368static void
Joe Perches475be4d2012-02-19 19:52:38 -08002369isdn_tty_modem_result(int code, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 atemu *m = &info->emu;
2372 static char *msg[] =
Joe Perches475be4d2012-02-19 19:52:38 -08002373 {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
2374 "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
2375 "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
2376 char s[ISDN_MSNLEN + 10];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 switch (code) {
Joe Perches475be4d2012-02-19 19:52:38 -08002379 case RESULT_RING:
2380 m->mdmreg[REG_RINGCNT]++;
2381 if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
2382 /* Automatically accept incoming call */
2383 isdn_tty_cmd_ATA(info);
2384 break;
2385 case RESULT_NO_CARRIER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386#ifdef ISDN_DEBUG_MODEM_HUP
Joe Perches475be4d2012-02-19 19:52:38 -08002387 printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
Peter Hurleya657eec2016-01-10 14:51:39 -08002388 info->closing, !info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002390 m->mdmreg[REG_RINGCNT] = 0;
2391 del_timer(&info->nc_timer);
2392 info->ncarrier = 0;
Peter Hurleya657eec2016-01-10 14:51:39 -08002393 if (info->closing || !info->port.tty)
Joe Perches475be4d2012-02-19 19:52:38 -08002394 return;
Jiri Slaby6776a2f2012-04-02 13:53:50 +02002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002397 if (info->vonline & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398#ifdef ISDN_DEBUG_MODEM_VOICE
Joe Perches475be4d2012-02-19 19:52:38 -08002399 printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 info->line);
2401#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002402 /* voice-recording, add DLE-ETX */
2403 isdn_tty_at_cout("\020\003", info);
2404 }
2405 if (info->vonline & 2) {
2406#ifdef ISDN_DEBUG_MODEM_VOICE
2407 printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
2408 info->line);
2409#endif
2410 /* voice-playing, add DLE-DC4 */
2411 isdn_tty_at_cout("\020\024", info);
2412 }
2413#endif
2414 break;
2415 case RESULT_CONNECT:
2416 case RESULT_CONNECT64000:
2417 sprintf(info->last_cause, "0000");
2418 if (!info->online)
2419 info->online = 2;
2420 break;
2421 case RESULT_VCON:
2422#ifdef ISDN_DEBUG_MODEM_VOICE
2423 printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
2424 info->line);
2425#endif
2426 sprintf(info->last_cause, "0000");
2427 if (!info->online)
2428 info->online = 1;
2429 break;
2430 } /* switch (code) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 if (m->mdmreg[REG_RESP] & BIT_RESP) {
2433 /* Show results */
2434 if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
2435 /* Show numeric results only */
2436 sprintf(s, "\r\n%d\r\n", code);
2437 isdn_tty_at_cout(s, info);
2438 } else {
2439 if (code == RESULT_RING) {
Joe Perches475be4d2012-02-19 19:52:38 -08002440 /* return if "show RUNG" and ringcounter>1 */
2441 if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 (m->mdmreg[REG_RINGCNT] > 1))
Joe Perches475be4d2012-02-19 19:52:38 -08002443 return;
2444 /* print CID, _before_ _every_ ring */
2445 if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
2446 isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
2447 isdn_tty_at_cout(dev->num[info->drv_index], info);
2448 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2449 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2450 isdn_tty_at_cout(info->emu.cpn, info);
2451 }
2452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 isdn_tty_at_cout("\r\n", info);
2455 isdn_tty_at_cout(msg[code], info);
2456 switch (code) {
Joe Perches475be4d2012-02-19 19:52:38 -08002457 case RESULT_CONNECT:
2458 switch (m->mdmreg[REG_L2PROT]) {
2459 case ISDN_PROTO_L2_MODEM:
2460 isdn_tty_at_cout(" ", info);
2461 isdn_tty_at_cout(m->connmsg, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002463 }
2464 break;
2465 case RESULT_RING:
2466 /* Append CPN, if enabled */
2467 if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
2468 sprintf(s, "/%s", m->cpn);
2469 isdn_tty_at_cout(s, info);
2470 }
2471 /* Print CID only once, _after_ 1st RING */
2472 if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
2473 (m->mdmreg[REG_RINGCNT] == 1)) {
2474 isdn_tty_at_cout("\r\n", info);
2475 isdn_tty_at_cout("CALLER NUMBER: ", info);
2476 isdn_tty_at_cout(dev->num[info->drv_index], info);
2477 if (m->mdmreg[REG_CDN] & BIT_CDN) {
2478 isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
2479 isdn_tty_at_cout(info->emu.cpn, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Joe Perches475be4d2012-02-19 19:52:38 -08002481 }
2482 break;
2483 case RESULT_NO_CARRIER:
2484 case RESULT_NO_DIALTONE:
2485 case RESULT_BUSY:
2486 case RESULT_NO_ANSWER:
2487 m->mdmreg[REG_RINGCNT] = 0;
2488 /* Append Cause-Message if enabled */
2489 if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
2490 sprintf(s, "/%s", info->last_cause);
2491 isdn_tty_at_cout(s, info);
2492 }
2493 break;
2494 case RESULT_CONNECT64000:
2495 /* Append Protocol to CONNECT message */
2496 switch (m->mdmreg[REG_L2PROT]) {
2497 case ISDN_PROTO_L2_X75I:
2498 case ISDN_PROTO_L2_X75UI:
2499 case ISDN_PROTO_L2_X75BUI:
2500 isdn_tty_at_cout("/X.75", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002502 case ISDN_PROTO_L2_HDLC:
2503 isdn_tty_at_cout("/HDLC", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002505 case ISDN_PROTO_L2_V11096:
2506 isdn_tty_at_cout("/V110/9600", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002508 case ISDN_PROTO_L2_V11019:
2509 isdn_tty_at_cout("/V110/19200", info);
2510 break;
2511 case ISDN_PROTO_L2_V11038:
2512 isdn_tty_at_cout("/V110/38400", info);
2513 break;
2514 }
2515 if (m->mdmreg[REG_T70] & BIT_T70) {
2516 isdn_tty_at_cout("/T.70", info);
2517 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2518 isdn_tty_at_cout("+", info);
2519 }
2520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522 isdn_tty_at_cout("\r\n", info);
2523 }
2524 }
2525 if (code == RESULT_NO_CARRIER) {
Peter Hurleya657eec2016-01-10 14:51:39 -08002526 if (info->closing || (!info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return;
Jiri Slaby6776a2f2012-04-02 13:53:50 +02002528
Jiri Slaby48decc12012-04-02 13:53:54 +02002529 if (info->port.flags & ASYNC_CHECK_CD)
Jiri Slabyba432942012-04-02 13:53:57 +02002530 tty_hangup(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
2532}
2533
2534
2535/*
2536 * Display a modem-register-value.
2537 */
2538static void
Joe Perches475be4d2012-02-19 19:52:38 -08002539isdn_tty_show_profile(int ridx, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 char v[6];
2542
2543 sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
2544 isdn_tty_at_cout(v, info);
2545}
2546
2547/*
2548 * Get MSN-string from char-pointer, set pointer to end of number
2549 */
2550static void
2551isdn_tty_get_msnstr(char *n, char **p)
2552{
2553 int limit = ISDN_MSNLEN - 1;
2554
2555 while (((*p[0] >= '0' && *p[0] <= '9') ||
2556 /* Why a comma ??? */
2557 (*p[0] == ',') || (*p[0] == ':')) &&
Joe Perches475be4d2012-02-19 19:52:38 -08002558 (limit--))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 *n++ = *p[0]++;
2560 *n = '\0';
2561}
2562
2563/*
2564 * Get phone-number from modem-commandbuffer
2565 */
2566static void
Joe Perches475be4d2012-02-19 19:52:38 -08002567isdn_tty_getdial(char *p, char *q, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 int first = 1;
2570 int limit = ISDN_MSNLEN - 1; /* MUST match the size of interface var to avoid
Joe Perches475be4d2012-02-19 19:52:38 -08002571 buffer overflow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Joe Perches475be4d2012-02-19 19:52:38 -08002573 while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
Karsten Keil924ad152007-06-01 00:46:55 -07002575 ((*p == 'R') && first) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 (*p == '*') || (*p == '#')) {
2577 *q++ = *p;
2578 limit--;
2579 }
Joe Perches475be4d2012-02-19 19:52:38 -08002580 if (!limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 break;
2582 p++;
2583 first = 0;
2584 }
2585 *q = 0;
2586}
2587
2588#define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
2589#define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
2590
2591static void
Joe Perches475be4d2012-02-19 19:52:38 -08002592isdn_tty_report(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 atemu *m = &info->emu;
2595 char s[80];
2596
2597 isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
2598 sprintf(s, " Remote Number: %s\r\n", info->last_num);
2599 isdn_tty_at_cout(s, info);
2600 sprintf(s, " Direction: %s\r\n", info->last_dir ? "outgoing" : "incoming");
2601 isdn_tty_at_cout(s, info);
2602 isdn_tty_at_cout(" Layer-2 Protocol: ", info);
2603 switch (info->last_l2) {
Joe Perches475be4d2012-02-19 19:52:38 -08002604 case ISDN_PROTO_L2_X75I:
2605 isdn_tty_at_cout("X.75i", info);
2606 break;
2607 case ISDN_PROTO_L2_X75UI:
2608 isdn_tty_at_cout("X.75ui", info);
2609 break;
2610 case ISDN_PROTO_L2_X75BUI:
2611 isdn_tty_at_cout("X.75bui", info);
2612 break;
2613 case ISDN_PROTO_L2_HDLC:
2614 isdn_tty_at_cout("HDLC", info);
2615 break;
2616 case ISDN_PROTO_L2_V11096:
2617 isdn_tty_at_cout("V.110 9600 Baud", info);
2618 break;
2619 case ISDN_PROTO_L2_V11019:
2620 isdn_tty_at_cout("V.110 19200 Baud", info);
2621 break;
2622 case ISDN_PROTO_L2_V11038:
2623 isdn_tty_at_cout("V.110 38400 Baud", info);
2624 break;
2625 case ISDN_PROTO_L2_TRANS:
2626 isdn_tty_at_cout("transparent", info);
2627 break;
2628 case ISDN_PROTO_L2_MODEM:
2629 isdn_tty_at_cout("modem", info);
2630 break;
2631 case ISDN_PROTO_L2_FAX:
2632 isdn_tty_at_cout("fax", info);
2633 break;
2634 default:
2635 isdn_tty_at_cout("unknown", info);
2636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
2638 if (m->mdmreg[REG_T70] & BIT_T70) {
2639 isdn_tty_at_cout("/T.70", info);
2640 if (m->mdmreg[REG_T70] & BIT_T70_EXT)
2641 isdn_tty_at_cout("+", info);
2642 }
2643 isdn_tty_at_cout("\r\n", info);
2644 isdn_tty_at_cout(" Service: ", info);
2645 switch (info->last_si) {
Joe Perches475be4d2012-02-19 19:52:38 -08002646 case 1:
2647 isdn_tty_at_cout("audio\r\n", info);
2648 break;
2649 case 5:
2650 isdn_tty_at_cout("btx\r\n", info);
2651 break;
2652 case 7:
2653 isdn_tty_at_cout("data\r\n", info);
2654 break;
2655 default:
2656 sprintf(s, "%d\r\n", info->last_si);
2657 isdn_tty_at_cout(s, info);
2658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660 sprintf(s, " Hangup location: %s\r\n", info->last_lhup ? "local" : "remote");
2661 isdn_tty_at_cout(s, info);
2662 sprintf(s, " Last cause: %s\r\n", info->last_cause);
2663 isdn_tty_at_cout(s, info);
2664}
2665
2666/*
2667 * Parse AT&.. commands.
2668 */
2669static int
Joe Perches475be4d2012-02-19 19:52:38 -08002670isdn_tty_cmd_ATand(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 atemu *m = &info->emu;
2673 int i;
2674 char rb[100];
2675
2676#define MAXRB (sizeof(rb) - 1)
2677
2678 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08002679 case 'B':
2680 /* &B - Set Buffersize */
2681 p[0]++;
2682 i = isdn_getnum(p);
2683 if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
2684 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -08002686 if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
2687 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002689 m->mdmreg[REG_PSIZE] = i / 16;
2690 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2691 switch (m->mdmreg[REG_L2PROT]) {
2692 case ISDN_PROTO_L2_V11096:
2693 case ISDN_PROTO_L2_V11019:
2694 case ISDN_PROTO_L2_V11038:
2695 info->xmit_size /= 10;
2696 }
2697 break;
2698 case 'C':
2699 /* &C - DCD Status */
2700 p[0]++;
2701 switch (isdn_getnum(p)) {
2702 case 0:
2703 m->mdmreg[REG_DCD] &= ~BIT_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002705 case 1:
2706 m->mdmreg[REG_DCD] |= BIT_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002708 default:
2709 PARSE_ERROR1
2710 }
2711 break;
2712 case 'D':
2713 /* &D - Set DTR-Low-behavior */
2714 p[0]++;
2715 switch (isdn_getnum(p)) {
2716 case 0:
2717 m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
2718 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002720 case 2:
2721 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2722 m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002724 case 3:
2725 m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
2726 m->mdmreg[REG_DTRR] |= BIT_DTRR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002728 default:
2729 PARSE_ERROR1
2730 }
2731 break;
2732 case 'E':
2733 /* &E -Set EAZ/MSN */
2734 p[0]++;
2735 isdn_tty_get_msnstr(m->msn, p);
2736 break;
2737 case 'F':
2738 /* &F -Set Factory-Defaults */
2739 p[0]++;
2740 if (info->msr & UART_MSR_DCD)
2741 PARSE_ERROR1;
2742 isdn_tty_reset_profile(m);
2743 isdn_tty_modem_reset_regs(info, 1);
2744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745#ifdef DUMMY_HAYES_AT
Joe Perches475be4d2012-02-19 19:52:38 -08002746 case 'K':
2747 /* only for be compilant with common scripts */
2748 /* &K Flowcontrol - no function */
2749 p[0]++;
2750 isdn_getnum(p);
2751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752#endif
Joe Perches475be4d2012-02-19 19:52:38 -08002753 case 'L':
2754 /* &L -Set Numbers to listen on */
2755 p[0]++;
2756 i = 0;
2757 while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
2758 (i < ISDN_LMSNLEN - 1))
2759 m->lmsn[i++] = *p[0]++;
2760 m->lmsn[i] = '\0';
2761 break;
2762 case 'R':
2763 /* &R - Set V.110 bitrate adaption */
2764 p[0]++;
2765 i = isdn_getnum(p);
2766 switch (i) {
2767 case 0:
2768 /* Switch off V.110, back to X.75 */
2769 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2770 m->mdmreg[REG_SI2] = 0;
2771 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002773 case 9600:
2774 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
2775 m->mdmreg[REG_SI2] = 197;
2776 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002778 case 19200:
2779 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
2780 m->mdmreg[REG_SI2] = 199;
2781 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 break;
Joe Perches475be4d2012-02-19 19:52:38 -08002783 case 38400:
2784 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
2785 m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
2786 info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 break;
2788 default:
2789 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08002790 }
2791 /* Switch off T.70 */
2792 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2793 /* Set Service 7 */
2794 m->mdmreg[REG_SI1] |= 4;
2795 break;
2796 case 'S':
2797 /* &S - Set Windowsize */
2798 p[0]++;
2799 i = isdn_getnum(p);
2800 if ((i > 0) && (i < 9))
2801 m->mdmreg[REG_WSIZE] = i;
2802 else
2803 PARSE_ERROR1;
2804 break;
2805 case 'V':
2806 /* &V - Show registers */
2807 p[0]++;
2808 isdn_tty_at_cout("\r\n", info);
2809 for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
2810 sprintf(rb, "S%02d=%03d%s", i,
2811 m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
2812 isdn_tty_at_cout(rb, info);
2813 }
2814 sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
2815 strlen(m->msn) ? m->msn : "None");
2816 isdn_tty_at_cout(rb, info);
2817 if (strlen(m->lmsn)) {
2818 isdn_tty_at_cout("\r\nListen: ", info);
2819 isdn_tty_at_cout(m->lmsn, info);
2820 isdn_tty_at_cout("\r\n", info);
2821 }
2822 break;
2823 case 'W':
2824 /* &W - Write Profile */
2825 p[0]++;
2826 switch (*p[0]) {
2827 case '0':
2828 p[0]++;
2829 modem_write_profile(m);
2830 break;
2831 default:
2832 PARSE_ERROR1;
2833 }
2834 break;
2835 case 'X':
2836 /* &X - Switch to BTX-Mode and T.70 */
2837 p[0]++;
2838 switch (isdn_getnum(p)) {
2839 case 0:
2840 m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
2841 info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
2842 break;
2843 case 1:
2844 m->mdmreg[REG_T70] |= BIT_T70;
2845 m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
2846 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2847 info->xmit_size = 112;
2848 m->mdmreg[REG_SI1] = 4;
2849 m->mdmreg[REG_SI2] = 0;
2850 break;
2851 case 2:
2852 m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
2853 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
2854 info->xmit_size = 112;
2855 m->mdmreg[REG_SI1] = 4;
2856 m->mdmreg[REG_SI2] = 0;
2857 break;
2858 default:
2859 PARSE_ERROR1;
2860 }
2861 break;
2862 default:
2863 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865 return 0;
2866}
2867
2868static int
Joe Perches475be4d2012-02-19 19:52:38 -08002869isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
2871 /* Some plausibility checks */
2872 switch (mreg) {
Joe Perches475be4d2012-02-19 19:52:38 -08002873 case REG_L2PROT:
2874 if (mval > ISDN_PROTO_L2_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return 1;
Joe Perches475be4d2012-02-19 19:52:38 -08002876 break;
2877 case REG_PSIZE:
2878 if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
2879 return 1;
2880#ifdef CONFIG_ISDN_AUDIO
2881 if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
2882 return 1;
2883#endif
2884 info->xmit_size = mval * 16;
2885 switch (m->mdmreg[REG_L2PROT]) {
2886 case ISDN_PROTO_L2_V11096:
2887 case ISDN_PROTO_L2_V11019:
2888 case ISDN_PROTO_L2_V11038:
2889 info->xmit_size /= 10;
2890 }
2891 break;
2892 case REG_SI1I:
2893 case REG_PLAN:
2894 case REG_SCREEN:
2895 /* readonly registers */
2896 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898 return 0;
2899}
2900
2901/*
2902 * Perform ATS command
2903 */
2904static int
Joe Perches475be4d2012-02-19 19:52:38 -08002905isdn_tty_cmd_ATS(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 atemu *m = &info->emu;
2908 int bitpos;
2909 int mreg;
2910 int mval;
2911 int bval;
2912
2913 mreg = isdn_getnum(p);
2914 if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
2915 PARSE_ERROR1;
2916 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08002917 case '=':
2918 p[0]++;
2919 mval = isdn_getnum(p);
2920 if (mval < 0 || mval > 255)
2921 PARSE_ERROR1;
2922 if (isdn_tty_check_ats(mreg, mval, info, m))
2923 PARSE_ERROR1;
2924 m->mdmreg[mreg] = mval;
2925 break;
2926 case '.':
2927 /* Set/Clear a single bit */
2928 p[0]++;
2929 bitpos = isdn_getnum(p);
2930 if ((bitpos < 0) || (bitpos > 7))
2931 PARSE_ERROR1;
2932 switch (*p[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 case '=':
2934 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08002935 bval = isdn_getnum(p);
2936 if (bval < 0 || bval > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08002938 if (bval)
2939 mval = m->mdmreg[mreg] | (1 << bitpos);
2940 else
2941 mval = m->mdmreg[mreg] & ~(1 << bitpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (isdn_tty_check_ats(mreg, mval, info, m))
2943 PARSE_ERROR1;
2944 m->mdmreg[mreg] = mval;
2945 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 case '?':
2947 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08002948 isdn_tty_at_cout("\r\n", info);
2949 isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
2950 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 break;
2952 default:
2953 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08002954 }
2955 break;
2956 case '?':
2957 p[0]++;
2958 isdn_tty_show_profile(mreg, info);
2959 break;
2960 default:
2961 PARSE_ERROR1;
2962 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
2964 return 0;
2965}
2966
2967/*
2968 * Perform ATA command
2969 */
2970static void
Joe Perches475be4d2012-02-19 19:52:38 -08002971isdn_tty_cmd_ATA(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
2973 atemu *m = &info->emu;
2974 isdn_ctrl cmd;
2975 int l2;
2976
2977 if (info->msr & UART_MSR_RI) {
2978 /* Accept incoming call */
2979 info->last_dir = 0;
2980 strcpy(info->last_num, dev->num[info->drv_index]);
2981 m->mdmreg[REG_RINGCNT] = 0;
2982 info->msr &= ~UART_MSR_RI;
2983 l2 = m->mdmreg[REG_L2PROT];
2984#ifdef CONFIG_ISDN_AUDIO
2985 /* If more than one bit set in reg18, autoselect Layer2 */
2986 if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
2987 if (m->mdmreg[REG_SI1I] == 1) {
2988 if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
2989 l2 = ISDN_PROTO_L2_TRANS;
2990 } else
2991 l2 = ISDN_PROTO_L2_X75I;
2992 }
2993#endif
2994 cmd.driver = info->isdn_driver;
2995 cmd.command = ISDN_CMD_SETL2;
2996 cmd.arg = info->isdn_channel + (l2 << 8);
2997 info->last_l2 = l2;
2998 isdn_command(&cmd);
2999 cmd.driver = info->isdn_driver;
3000 cmd.command = ISDN_CMD_SETL3;
3001 cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
3002#ifdef CONFIG_ISDN_TTY_FAX
3003 if (l2 == ISDN_PROTO_L2_FAX) {
3004 cmd.parm.fax = info->fax;
3005 info->fax->direction = ISDN_TTY_FAX_CONN_IN;
3006 }
3007#endif
3008 isdn_command(&cmd);
3009 cmd.driver = info->isdn_driver;
3010 cmd.arg = info->isdn_channel;
3011 cmd.command = ISDN_CMD_ACCEPTD;
3012 info->dialing = 16;
3013 info->emu.carrierwait = 0;
3014 isdn_command(&cmd);
3015 isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
3016 } else
3017 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3018}
3019
3020#ifdef CONFIG_ISDN_AUDIO
3021/*
3022 * Parse AT+F.. commands
3023 */
3024static int
Joe Perches475be4d2012-02-19 19:52:38 -08003025isdn_tty_cmd_PLUSF(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 atemu *m = &info->emu;
3028 char rs[20];
3029
3030 if (!strncmp(p[0], "CLASS", 5)) {
3031 p[0] += 5;
3032 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003033 case '?':
3034 p[0]++;
3035 sprintf(rs, "\r\n%d",
3036 (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
3037#ifdef CONFIG_ISDN_TTY_FAX
3038 if (TTY_IS_FCLASS2(info))
3039 sprintf(rs, "\r\n2");
3040 else if (TTY_IS_FCLASS1(info))
3041 sprintf(rs, "\r\n1");
3042#endif
3043 isdn_tty_at_cout(rs, info);
3044 break;
3045 case '=':
3046 p[0]++;
3047 switch (*p[0]) {
3048 case '0':
3049 p[0]++;
3050 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3051 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3052 m->mdmreg[REG_SI1] = 4;
3053 info->xmit_size =
3054 m->mdmreg[REG_PSIZE] * 16;
3055 break;
3056#ifdef CONFIG_ISDN_TTY_FAX
3057 case '1':
3058 p[0]++;
3059 if (!(dev->global_features &
3060 ISDN_FEATURE_L3_FCLASS1))
3061 PARSE_ERROR1;
3062 m->mdmreg[REG_SI1] = 1;
3063 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3064 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
3065 info->xmit_size =
3066 m->mdmreg[REG_PSIZE] * 16;
3067 break;
3068 case '2':
3069 p[0]++;
3070 if (!(dev->global_features &
3071 ISDN_FEATURE_L3_FCLASS2))
3072 PARSE_ERROR1;
3073 m->mdmreg[REG_SI1] = 1;
3074 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
3075 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
3076 info->xmit_size =
3077 m->mdmreg[REG_PSIZE] * 16;
3078 break;
3079#endif
3080 case '8':
3081 p[0]++;
3082 /* L2 will change on dialout with si=1 */
3083 m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
3084 m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
3085 m->mdmreg[REG_SI1] = 5;
3086 info->xmit_size = VBUF;
3087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 case '?':
3089 p[0]++;
Joe Perches475be4d2012-02-19 19:52:38 -08003090 strcpy(rs, "\r\n0,");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -08003092 if (dev->global_features &
3093 ISDN_FEATURE_L3_FCLASS1)
3094 strcat(rs, "1,");
3095 if (dev->global_features &
3096 ISDN_FEATURE_L3_FCLASS2)
3097 strcat(rs, "2,");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098#endif
Joe Perches475be4d2012-02-19 19:52:38 -08003099 strcat(rs, "8");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 isdn_tty_at_cout(rs, info);
3101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 default:
3103 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003104 }
3105 break;
3106 default:
3107 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109 return 0;
3110 }
3111#ifdef CONFIG_ISDN_TTY_FAX
3112 return (isdn_tty_cmd_PLUSF_FAX(p, info));
3113#else
3114 PARSE_ERROR1;
3115#endif
3116}
3117
3118/*
3119 * Parse AT+V.. commands
3120 */
3121static int
Joe Perches475be4d2012-02-19 19:52:38 -08003122isdn_tty_cmd_PLUSV(char **p, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124 atemu *m = &info->emu;
3125 isdn_ctrl cmd;
3126 static char *vcmd[] =
Joe Perches475be4d2012-02-19 19:52:38 -08003127 {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 int i;
3129 int par1;
3130 int par2;
3131 char rs[20];
3132
3133 i = 0;
3134 while (vcmd[i]) {
3135 if (!strncmp(vcmd[i], p[0], 2)) {
3136 p[0] += 2;
3137 break;
3138 }
3139 i++;
3140 }
3141 switch (i) {
Joe Perches475be4d2012-02-19 19:52:38 -08003142 case 0:
3143 /* AT+VNH - Auto hangup feature */
3144 switch (*p[0]) {
3145 case '?':
3146 p[0]++;
3147 isdn_tty_at_cout("\r\n1", info);
3148 break;
3149 case '=':
3150 p[0]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 switch (*p[0]) {
Joe Perches475be4d2012-02-19 19:52:38 -08003152 case '1':
3153 p[0]++;
3154 break;
3155 case '?':
3156 p[0]++;
3157 isdn_tty_at_cout("\r\n1", info);
3158 break;
3159 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 }
3162 break;
3163 default:
3164 PARSE_ERROR1;
Joe Perches475be4d2012-02-19 19:52:38 -08003165 }
3166 break;
3167 case 1:
3168 /* AT+VIP - Reset all voice parameters */
3169 isdn_tty_modem_reset_vpar(m);
3170 break;
3171 case 2:
3172 /* AT+VLS - Select device, accept incoming call */
3173 switch (*p[0]) {
3174 case '?':
3175 p[0]++;
3176 sprintf(rs, "\r\n%d", m->vpar[0]);
3177 isdn_tty_at_cout(rs, info);
3178 break;
3179 case '=':
3180 p[0]++;
3181 switch (*p[0]) {
3182 case '0':
3183 p[0]++;
3184 m->vpar[0] = 0;
3185 break;
3186 case '2':
3187 p[0]++;
3188 m->vpar[0] = 2;
3189 break;
3190 case '?':
3191 p[0]++;
3192 isdn_tty_at_cout("\r\n0,2", info);
3193 break;
3194 default:
3195 PARSE_ERROR1;
3196 }
3197 break;
3198 default:
3199 PARSE_ERROR1;
3200 }
3201 break;
3202 case 3:
3203 /* AT+VRX - Start recording */
3204 if (!m->vpar[0])
3205 PARSE_ERROR1;
3206 if (info->online != 1) {
3207 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3208 return 1;
3209 }
3210 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3211 if (!info->dtmf_state) {
3212 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3213 PARSE_ERROR1;
3214 }
3215 info->silence_state = isdn_audio_silence_init(info->silence_state);
3216 if (!info->silence_state) {
3217 printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
3218 PARSE_ERROR1;
3219 }
3220 if (m->vpar[3] < 5) {
3221 info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
3222 if (!info->adpcmr) {
3223 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3224 PARSE_ERROR1;
3225 }
3226 }
3227#ifdef ISDN_DEBUG_AT
3228 printk(KERN_DEBUG "AT: +VRX\n");
3229#endif
3230 info->vonline |= 1;
3231 isdn_tty_modem_result(RESULT_CONNECT, info);
3232 return 0;
3233 break;
3234 case 4:
3235 /* AT+VSD - Silence detection */
3236 switch (*p[0]) {
3237 case '?':
3238 p[0]++;
3239 sprintf(rs, "\r\n<%d>,<%d>",
3240 m->vpar[1],
3241 m->vpar[2]);
3242 isdn_tty_at_cout(rs, info);
3243 break;
3244 case '=':
3245 p[0]++;
3246 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3247 par1 = isdn_getnum(p);
3248 if ((par1 < 0) || (par1 > 31))
3249 PARSE_ERROR1;
3250 if (*p[0] != ',')
3251 PARSE_ERROR1;
3252 p[0]++;
3253 par2 = isdn_getnum(p);
3254 if ((par2 < 0) || (par2 > 255))
3255 PARSE_ERROR1;
3256 m->vpar[1] = par1;
3257 m->vpar[2] = par2;
3258 break;
3259 } else
3260 if (*p[0] == '?') {
3261 p[0]++;
3262 isdn_tty_at_cout("\r\n<0-31>,<0-255>",
3263 info);
3264 break;
3265 } else
3266 PARSE_ERROR1;
3267 break;
3268 default:
3269 PARSE_ERROR1;
3270 }
3271 break;
3272 case 5:
3273 /* AT+VSM - Select compression */
3274 switch (*p[0]) {
3275 case '?':
3276 p[0]++;
3277 sprintf(rs, "\r\n<%d>,<%d><8000>",
3278 m->vpar[3],
3279 m->vpar[1]);
3280 isdn_tty_at_cout(rs, info);
3281 break;
3282 case '=':
3283 p[0]++;
3284 switch (*p[0]) {
3285 case '2':
3286 case '3':
3287 case '4':
3288 case '5':
3289 case '6':
3290 par1 = isdn_getnum(p);
3291 if ((par1 < 2) || (par1 > 6))
3292 PARSE_ERROR1;
3293 m->vpar[3] = par1;
3294 break;
3295 case '?':
3296 p[0]++;
3297 isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
3298 info);
3299 isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
3300 info);
3301 isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
3302 info);
3303 isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
3304 info);
3305 isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
3306 info);
3307 break;
3308 default:
3309 PARSE_ERROR1;
3310 }
3311 break;
3312 default:
3313 PARSE_ERROR1;
3314 }
3315 break;
3316 case 6:
3317 /* AT+VTX - Start sending */
3318 if (!m->vpar[0])
3319 PARSE_ERROR1;
3320 if (info->online != 1) {
3321 isdn_tty_modem_result(RESULT_NO_ANSWER, info);
3322 return 1;
3323 }
3324 info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
3325 if (!info->dtmf_state) {
3326 printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
3327 PARSE_ERROR1;
3328 }
3329 if (m->vpar[3] < 5) {
3330 info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
3331 if (!info->adpcms) {
3332 printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
3333 PARSE_ERROR1;
3334 }
3335 }
3336#ifdef ISDN_DEBUG_AT
3337 printk(KERN_DEBUG "AT: +VTX\n");
3338#endif
3339 m->lastDLE = 0;
3340 info->vonline |= 2;
3341 isdn_tty_modem_result(RESULT_CONNECT, info);
3342 return 0;
3343 break;
3344 case 7:
3345 /* AT+VDD - DTMF detection */
3346 switch (*p[0]) {
3347 case '?':
3348 p[0]++;
3349 sprintf(rs, "\r\n<%d>,<%d>",
3350 m->vpar[4],
3351 m->vpar[5]);
3352 isdn_tty_at_cout(rs, info);
3353 break;
3354 case '=':
3355 p[0]++;
3356 if ((*p[0] >= '0') && (*p[0] <= '9')) {
3357 if (info->online != 1)
3358 PARSE_ERROR1;
3359 par1 = isdn_getnum(p);
3360 if ((par1 < 0) || (par1 > 15))
3361 PARSE_ERROR1;
3362 if (*p[0] != ',')
3363 PARSE_ERROR1;
3364 p[0]++;
3365 par2 = isdn_getnum(p);
3366 if ((par2 < 0) || (par2 > 255))
3367 PARSE_ERROR1;
3368 m->vpar[4] = par1;
3369 m->vpar[5] = par2;
3370 cmd.driver = info->isdn_driver;
3371 cmd.command = ISDN_CMD_AUDIO;
3372 cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
3373 cmd.parm.num[0] = par1;
3374 cmd.parm.num[1] = par2;
3375 isdn_command(&cmd);
3376 break;
3377 } else
3378 if (*p[0] == '?') {
3379 p[0]++;
3380 isdn_tty_at_cout("\r\n<0-15>,<0-255>",
3381 info);
3382 break;
3383 } else
3384 PARSE_ERROR1;
3385 break;
3386 default:
3387 PARSE_ERROR1;
3388 }
3389 break;
3390 default:
3391 PARSE_ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
3393 return 0;
3394}
3395#endif /* CONFIG_ISDN_AUDIO */
3396
3397/*
3398 * Parse and perform an AT-command-line.
3399 */
3400static void
Joe Perches475be4d2012-02-19 19:52:38 -08003401isdn_tty_parse_at(modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
3403 atemu *m = &info->emu;
3404 char *p;
Dan Carpenterf417f5e2010-09-04 08:33:03 +00003405 char ds[ISDN_MSNLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407#ifdef ISDN_DEBUG_AT
3408 printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
3409#endif
3410 for (p = &m->mdmcmd[2]; *p;) {
3411 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -08003412 case ' ':
3413 p++;
3414 break;
3415 case 'A':
3416 /* A - Accept incoming call */
3417 p++;
3418 isdn_tty_cmd_ATA(info);
3419 return;
Joe Perches475be4d2012-02-19 19:52:38 -08003420 case 'D':
3421 /* D - Dial */
3422 if (info->msr & UART_MSR_DCD)
3423 PARSE_ERROR;
3424 if (info->msr & UART_MSR_RI) {
3425 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return;
Joe Perches475be4d2012-02-19 19:52:38 -08003427 }
3428 isdn_tty_getdial(++p, ds, sizeof ds);
3429 p += strlen(p);
3430 if (!strlen(m->msn))
3431 isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
3432 else if (strlen(ds))
3433 isdn_tty_dial(ds, info, m);
3434 else
3435 PARSE_ERROR;
3436 return;
3437 case 'E':
3438 /* E - Turn Echo on/off */
3439 p++;
3440 switch (isdn_getnum(&p)) {
3441 case 0:
3442 m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 break;
Joe Perches475be4d2012-02-19 19:52:38 -08003444 case 1:
3445 m->mdmreg[REG_ECHO] |= BIT_ECHO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 break;
3447 default:
3448 PARSE_ERROR;
Joe Perches475be4d2012-02-19 19:52:38 -08003449 }
3450 break;
3451 case 'H':
3452 /* H - On/Off-hook */
3453 p++;
3454 switch (*p) {
3455 case '0':
3456 p++;
3457 isdn_tty_on_hook(info);
3458 break;
3459 case '1':
3460 p++;
3461 isdn_tty_off_hook();
3462 break;
3463 default:
3464 isdn_tty_on_hook(info);
3465 break;
3466 }
3467 break;
3468 case 'I':
3469 /* I - Information */
3470 p++;
3471 isdn_tty_at_cout("\r\nLinux ISDN", info);
3472 switch (*p) {
3473 case '0':
3474 case '1':
3475 p++;
3476 break;
3477 case '2':
3478 p++;
3479 isdn_tty_report(info);
3480 break;
3481 case '3':
3482 p++;
3483 snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
3484 isdn_tty_at_cout(ds, info);
3485 break;
3486 default:;
3487 }
3488 break;
3489#ifdef DUMMY_HAYES_AT
3490 case 'L':
3491 case 'M':
3492 /* only for be compilant with common scripts */
3493 /* no function */
3494 p++;
3495 isdn_getnum(&p);
3496 break;
3497#endif
3498 case 'O':
3499 /* O - Go online */
3500 p++;
3501 if (info->msr & UART_MSR_DCD)
3502 /* if B-Channel is up */
3503 isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
3504 else
3505 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3506 return;
3507 case 'Q':
3508 /* Q - Turn Emulator messages on/off */
3509 p++;
3510 switch (isdn_getnum(&p)) {
3511 case 0:
3512 m->mdmreg[REG_RESP] |= BIT_RESP;
3513 break;
3514 case 1:
3515 m->mdmreg[REG_RESP] &= ~BIT_RESP;
3516 break;
3517 default:
3518 PARSE_ERROR;
3519 }
3520 break;
3521 case 'S':
3522 /* S - Set/Get Register */
3523 p++;
3524 if (isdn_tty_cmd_ATS(&p, info))
3525 return;
3526 break;
3527 case 'V':
3528 /* V - Numeric or ASCII Emulator-messages */
3529 p++;
3530 switch (isdn_getnum(&p)) {
3531 case 0:
3532 m->mdmreg[REG_RESP] |= BIT_RESPNUM;
3533 break;
3534 case 1:
3535 m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
3536 break;
3537 default:
3538 PARSE_ERROR;
3539 }
3540 break;
3541 case 'Z':
3542 /* Z - Load Registers from Profile */
3543 p++;
3544 if (info->msr & UART_MSR_DCD) {
3545 info->online = 0;
3546 isdn_tty_on_hook(info);
3547 }
3548 isdn_tty_modem_reset_regs(info, 1);
3549 break;
3550 case '+':
3551 p++;
3552 switch (*p) {
3553#ifdef CONFIG_ISDN_AUDIO
3554 case 'F':
3555 p++;
3556 if (isdn_tty_cmd_PLUSF(&p, info))
3557 return;
3558 break;
3559 case 'V':
3560 if ((!(m->mdmreg[REG_SI1] & 1)) ||
3561 (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
3562 PARSE_ERROR;
3563 p++;
3564 if (isdn_tty_cmd_PLUSV(&p, info))
3565 return;
3566 break;
3567#endif /* CONFIG_ISDN_AUDIO */
3568 case 'S': /* SUSPEND */
3569 p++;
3570 isdn_tty_get_msnstr(ds, &p);
3571 isdn_tty_suspend(ds, info, m);
3572 break;
3573 case 'R': /* RESUME */
3574 p++;
3575 isdn_tty_get_msnstr(ds, &p);
3576 isdn_tty_resume(ds, info, m);
3577 break;
3578 case 'M': /* MESSAGE */
3579 p++;
3580 isdn_tty_send_msg(info, m, p);
3581 break;
3582 default:
3583 PARSE_ERROR;
3584 }
3585 break;
3586 case '&':
3587 p++;
3588 if (isdn_tty_cmd_ATand(&p, info))
3589 return;
3590 break;
3591 default:
3592 PARSE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 }
3594 }
3595#ifdef CONFIG_ISDN_AUDIO
3596 if (!info->vonline)
3597#endif
3598 isdn_tty_modem_result(RESULT_OK, info);
3599}
3600
3601/* Need own toupper() because standard-toupper is not available
3602 * within modules.
3603 */
Joe Perches475be4d2012-02-19 19:52:38 -08003604#define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606/*
3607 * Perform line-editing of AT-commands
3608 *
3609 * Parameters:
3610 * p inputbuffer
3611 * count length of buffer
3612 * channel index to line (minor-device)
3613 */
3614static int
Joe Perches475be4d2012-02-19 19:52:38 -08003615isdn_tty_edit_at(const char *p, int count, modem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
3617 atemu *m = &info->emu;
3618 int total = 0;
3619 u_char c;
3620 char eb[2];
3621 int cnt;
3622
3623 for (cnt = count; cnt > 0; p++, cnt--) {
3624 c = *p;
3625 total++;
3626 if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
3627 /* Separator (CR or LF) */
3628 m->mdmcmd[m->mdmcmdl] = 0;
3629 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3630 eb[0] = c;
3631 eb[1] = 0;
3632 isdn_tty_at_cout(eb, info);
3633 }
3634 if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
3635 isdn_tty_parse_at(info);
3636 m->mdmcmdl = 0;
3637 continue;
3638 }
3639 if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
3640 /* Backspace-Function */
3641 if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
3642 if (m->mdmcmdl)
3643 m->mdmcmdl--;
3644 if (m->mdmreg[REG_ECHO] & BIT_ECHO)
3645 isdn_tty_at_cout("\b", info);
3646 }
3647 continue;
3648 }
3649 if (cmdchar(c)) {
3650 if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
3651 eb[0] = c;
3652 eb[1] = 0;
3653 isdn_tty_at_cout(eb, info);
3654 }
3655 if (m->mdmcmdl < 255) {
3656 c = my_toupper(c);
3657 switch (m->mdmcmdl) {
Joe Perches475be4d2012-02-19 19:52:38 -08003658 case 1:
3659 if (c == 'T') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 m->mdmcmd[m->mdmcmdl] = c;
3661 m->mdmcmd[++m->mdmcmdl] = 0;
Joe Perches475be4d2012-02-19 19:52:38 -08003662 break;
3663 } else
3664 m->mdmcmdl = 0;
3665 /* Fall through, check for 'A' */
3666 case 0:
3667 if (c == 'A') {
3668 m->mdmcmd[m->mdmcmdl] = c;
3669 m->mdmcmd[++m->mdmcmdl] = 0;
3670 }
3671 break;
3672 default:
3673 m->mdmcmd[m->mdmcmdl] = c;
3674 m->mdmcmd[++m->mdmcmdl] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
3676 }
3677 }
3678 }
3679 return total;
3680}
3681
3682/*
3683 * Switch all modem-channels who are online and got a valid
3684 * escape-sequence 1.5 seconds ago, to command-mode.
3685 * This function is called every second via timer-interrupt from within
3686 * timer-dispatcher isdn_timer_function()
3687 */
3688void
3689isdn_tty_modem_escape(void)
3690{
3691 int ton = 0;
3692 int i;
3693 int midx;
3694
3695 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
Jiri Slaby37630f42012-04-02 13:53:52 +02003696 if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
3697 modem_info *info = &dev->mdm.info[midx];
3698 if (info->online) {
3699 ton = 1;
3700 if ((info->emu.pluscount == 3) &&
3701 time_after(jiffies,
3702 info->emu.lastplus + PLUSWAIT2)) {
3703 info->emu.pluscount = 0;
3704 info->online = 0;
3705 isdn_tty_modem_result(RESULT_OK, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
3707 }
Jiri Slaby37630f42012-04-02 13:53:52 +02003708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
3710}
3711
3712/*
3713 * Put a RING-message to all modem-channels who have the RI-bit set.
3714 * This function is called every second via timer-interrupt from within
3715 * timer-dispatcher isdn_timer_function()
3716 */
3717void
3718isdn_tty_modem_ring(void)
3719{
3720 int ton = 0;
3721 int i;
3722
3723 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3724 modem_info *info = &dev->mdm.info[i];
3725 if (info->msr & UART_MSR_RI) {
3726 ton = 1;
3727 isdn_tty_modem_result(RESULT_RING, info);
3728 }
3729 }
3730 isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
3731}
3732
3733/*
3734 * For all online tty's, try sending data to
3735 * the lower levels.
3736 */
3737void
3738isdn_tty_modem_xmit(void)
3739{
3740 int ton = 1;
3741 int i;
3742
3743 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3744 modem_info *info = &dev->mdm.info[i];
3745 if (info->online) {
3746 ton = 1;
3747 isdn_tty_senddown(info);
3748 isdn_tty_tint(info);
3749 }
3750 }
3751 isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
3752}
3753
3754/*
3755 * Check all channels if we have a 'no carrier' timeout.
3756 * Timeout value is set by Register S7.
3757 */
3758void
3759isdn_tty_carrier_timeout(void)
3760{
3761 int ton = 0;
3762 int i;
3763
3764 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
3765 modem_info *info = &dev->mdm.info[i];
Jiri Slaby37630f42012-04-02 13:53:52 +02003766 if (!info->dialing)
3767 continue;
3768 if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
3769 info->dialing = 0;
3770 isdn_tty_modem_result(RESULT_NO_CARRIER, info);
3771 isdn_tty_modem_hup(info, 1);
3772 } else
3773 ton = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
3775 isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
3776}