1 /* $Id: elsa_ser.c,v 1.1.1.1 2005/04/11 02:50:23 jack Exp $
3 * stuff for the serial modem on ELSA cards
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
10 #include <linux/config.h>
11 #include <linux/serial.h>
12 #include <linux/serial_reg.h>
14 #define MAX_MODEM_BUF 256
15 #define WAKEUP_CHARS (MAX_MODEM_BUF/2)
16 #define RS_ISR_PASS_LIMIT 256
17 #define BASE_BAUD ( 1843200 / 16 )
19 //#define SERIAL_DEBUG_OPEN 1
20 //#define SERIAL_DEBUG_INTR 1
21 //#define SERIAL_DEBUG_FLOW 1
22 #undef SERIAL_DEBUG_OPEN
23 #undef SERIAL_DEBUG_INTR
24 #undef SERIAL_DEBUG_FLOW
25 #undef SERIAL_DEBUG_REG
26 //#define SERIAL_DEBUG_REG 1
28 #ifdef SERIAL_DEBUG_REG
29 static u_char deb[32];
30 const char *ModemIn[] = {"RBR","IER","IIR","LCR","MCR","LSR","MSR","SCR"};
31 const char *ModemOut[] = {"THR","IER","FCR","LCR","MCR","LSR","MSR","SCR"};
34 static char *MInit_1 = "AT&F&C1E0&D2\r\0";
35 static char *MInit_2 = "ATL2M1S64=13\r\0";
36 static char *MInit_3 = "AT+FCLASS=0\r\0";
37 static char *MInit_4 = "ATV1S2=128X1\r\0";
38 static char *MInit_5 = "AT\\V8\\N3\r\0";
39 static char *MInit_6 = "ATL0M0&G0%E1\r\0";
40 static char *MInit_7 = "AT%L1%M0%C3\r\0";
42 static char *MInit_speed28800 = "AT%G0%B28800\r\0";
44 static char *MInit_dialout = "ATs7=60 x1 d\r\0";
45 static char *MInit_dialin = "ATs7=60 x1 a\r\0";
48 static inline unsigned int serial_in(struct IsdnCardState *cs, int offset)
50 #ifdef SERIAL_DEBUG_REG
51 u_int val = inb(cs->hw.elsa.base + 8 + offset);
52 debugl1(cs,"in %s %02x",ModemIn[offset], val);
55 return inb(cs->hw.elsa.base + 8 + offset);
59 static inline unsigned int serial_inp(struct IsdnCardState *cs, int offset)
61 #ifdef SERIAL_DEBUG_REG
62 #ifdef CONFIG_SERIAL_NOPAUSE_IO
63 u_int val = inb(cs->hw.elsa.base + 8 + offset);
64 debugl1(cs,"inp %s %02x",ModemIn[offset], val);
66 u_int val = inb_p(cs->hw.elsa.base + 8 + offset);
67 debugl1(cs,"inP %s %02x",ModemIn[offset], val);
71 #ifdef CONFIG_SERIAL_NOPAUSE_IO
72 return inb(cs->hw.elsa.base + 8 + offset);
74 return inb_p(cs->hw.elsa.base + 8 + offset);
79 static inline void serial_out(struct IsdnCardState *cs, int offset, int value)
81 #ifdef SERIAL_DEBUG_REG
82 debugl1(cs,"out %s %02x",ModemOut[offset], value);
84 outb(value, cs->hw.elsa.base + 8 + offset);
87 static inline void serial_outp(struct IsdnCardState *cs, int offset,
90 #ifdef SERIAL_DEBUG_REG
91 #ifdef CONFIG_SERIAL_NOPAUSE_IO
92 debugl1(cs,"outp %s %02x",ModemOut[offset], value);
94 debugl1(cs,"outP %s %02x",ModemOut[offset], value);
97 #ifdef CONFIG_SERIAL_NOPAUSE_IO
98 outb(value, cs->hw.elsa.base + 8 + offset);
100 outb_p(value, cs->hw.elsa.base + 8 + offset);
105 * This routine is called to set the UART divisor registers to match
106 * the specified baud rate for a serial port.
108 static void change_speed(struct IsdnCardState *cs, int baud)
110 int quot = 0, baud_base;
111 unsigned cval, fcr = 0;
116 /* byte size and parity */
117 cval = 0x03; bits = 10;
118 /* Determine divisor based on baud rate */
119 baud_base = BASE_BAUD;
120 quot = baud_base / baud;
121 /* If the quotient is ever zero, default to 9600 bps */
123 quot = baud_base / 9600;
126 if ((baud_base / quot) < 2400)
127 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
129 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
130 serial_outp(cs, UART_FCR, fcr);
131 /* CTS flow control flag and modem status interrupts */
132 cs->hw.elsa.IER &= ~UART_IER_MSI;
133 cs->hw.elsa.IER |= UART_IER_MSI;
134 serial_outp(cs, UART_IER, cs->hw.elsa.IER);
136 debugl1(cs,"modem quot=0x%x", quot);
139 serial_outp(cs, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
140 serial_outp(cs, UART_DLL, quot & 0xff); /* LS of divisor */
141 serial_outp(cs, UART_DLM, quot >> 8); /* MS of divisor */
142 serial_outp(cs, UART_LCR, cval); /* reset DLAB */
143 serial_inp(cs, UART_RX);
144 restore_flags(flags);
147 static int mstartup(struct IsdnCardState *cs)
153 save_flags(flags); cli();
156 * Clear the FIFO buffers and disable them
157 * (they will be reenabled in change_speed())
159 serial_outp(cs, UART_FCR, (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT));
162 * At this point there's no way the LSR could still be 0xFF;
163 * if it is, then bail out, because there's likely no UART
166 if (serial_inp(cs, UART_LSR) == 0xff) {
172 * Clear the interrupt registers.
174 (void) serial_inp(cs, UART_RX);
175 (void) serial_inp(cs, UART_IIR);
176 (void) serial_inp(cs, UART_MSR);
179 * Now, initialize the UART
181 serial_outp(cs, UART_LCR, UART_LCR_WLEN8); /* reset DLAB */
184 cs->hw.elsa.MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
185 serial_outp(cs, UART_MCR, cs->hw.elsa.MCR);
188 * Finally, enable interrupts
190 cs->hw.elsa.IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
191 serial_outp(cs, UART_IER, cs->hw.elsa.IER); /* enable interrupts */
194 * And clear the interrupt registers again for luck.
196 (void)serial_inp(cs, UART_LSR);
197 (void)serial_inp(cs, UART_RX);
198 (void)serial_inp(cs, UART_IIR);
199 (void)serial_inp(cs, UART_MSR);
201 cs->hw.elsa.transcnt = cs->hw.elsa.transp = 0;
202 cs->hw.elsa.rcvcnt = cs->hw.elsa.rcvp =0;
205 * and set the speed of the serial port
207 change_speed(cs, BASE_BAUD);
208 cs->hw.elsa.MFlag = 1;
210 restore_flags(flags);
215 * This routine will shutdown a serial port; interrupts are disabled, and
216 * DTR is dropped if the hangup on close termio flag is on.
218 static void mshutdown(struct IsdnCardState *cs)
223 #ifdef SERIAL_DEBUG_OPEN
224 printk(KERN_DEBUG"Shutting down serial ....");
227 save_flags(flags); cli(); /* Disable interrupts */
230 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
231 * here so the queue might never be waken up
235 serial_outp(cs, UART_IER, 0x00); /* disable all intrs */
236 cs->hw.elsa.MCR &= ~UART_MCR_OUT2;
238 /* disable break condition */
239 serial_outp(cs, UART_LCR, serial_inp(cs, UART_LCR) & ~UART_LCR_SBC);
241 cs->hw.elsa.MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
242 serial_outp(cs, UART_MCR, cs->hw.elsa.MCR);
245 serial_outp(cs, UART_FCR, (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT));
246 serial_inp(cs, UART_RX); /* read data port to reset things */
248 restore_flags(flags);
249 #ifdef SERIAL_DEBUG_OPEN
255 write_modem(struct BCState *bcs) {
257 struct IsdnCardState *cs = bcs->cs;
263 if (bcs->tx_skb->len <= 0)
267 len = bcs->tx_skb->len;
268 if (len > MAX_MODEM_BUF - cs->hw.elsa.transcnt)
269 len = MAX_MODEM_BUF - cs->hw.elsa.transcnt;
270 fp = cs->hw.elsa.transcnt + cs->hw.elsa.transp;
271 fp &= (MAX_MODEM_BUF -1);
273 if (count > MAX_MODEM_BUF - fp) {
274 count = MAX_MODEM_BUF - fp;
275 memcpy(cs->hw.elsa.transbuf + fp, bcs->tx_skb->data, count);
276 skb_pull(bcs->tx_skb, count);
277 cs->hw.elsa.transcnt += count;
282 memcpy((cs->hw.elsa.transbuf + fp), bcs->tx_skb->data, count);
283 skb_pull(bcs->tx_skb, count);
284 cs->hw.elsa.transcnt += count;
287 if (cs->hw.elsa.transcnt &&
288 !(cs->hw.elsa.IER & UART_IER_THRI)) {
289 cs->hw.elsa.IER |= UART_IER_THRI;
290 serial_outp(cs, UART_IER, cs->hw.elsa.IER);
292 restore_flags(flags);
297 modem_fill(struct BCState *bcs) {
300 if (bcs->tx_skb->len) {
304 if (bcs->st->lli.l1writewakeup &&
305 (PACKET_NOACK != bcs->tx_skb->pkt_type))
306 bcs->st->lli.l1writewakeup(bcs->st,
308 dev_kfree_skb_any(bcs->tx_skb);
312 if ((bcs->tx_skb = skb_dequeue(&bcs->squeue))) {
313 bcs->hw.hscx.count = 0;
314 test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
317 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
318 hscx_sched_event(bcs, B_XMTBUFREADY);
322 static inline void receive_chars(struct IsdnCardState *cs,
329 ch = serial_in(cs, UART_RX);
330 if (cs->hw.elsa.rcvcnt >= MAX_MODEM_BUF)
332 cs->hw.elsa.rcvbuf[cs->hw.elsa.rcvcnt++] = ch;
333 #ifdef SERIAL_DEBUG_INTR
334 printk("DR%02x:%02x...", ch, *status);
336 if (*status & (UART_LSR_BI | UART_LSR_PE |
337 UART_LSR_FE | UART_LSR_OE)) {
339 #ifdef SERIAL_DEBUG_INTR
340 printk("handling exept....");
343 *status = serial_inp(cs, UART_LSR);
344 } while (*status & UART_LSR_DR);
345 if (cs->hw.elsa.MFlag == 2) {
346 if (!(skb = dev_alloc_skb(cs->hw.elsa.rcvcnt)))
347 printk(KERN_WARNING "ElsaSER: receive out of memory\n");
349 memcpy(skb_put(skb, cs->hw.elsa.rcvcnt), cs->hw.elsa.rcvbuf,
351 skb_queue_tail(& cs->hw.elsa.bcs->rqueue, skb);
353 hscx_sched_event(cs->hw.elsa.bcs, B_RCVBUFREADY);
358 t += sprintf(t, "modem read cnt %d", cs->hw.elsa.rcvcnt);
359 QuickHex(t, cs->hw.elsa.rcvbuf, cs->hw.elsa.rcvcnt);
362 cs->hw.elsa.rcvcnt = 0;
365 static inline void transmit_chars(struct IsdnCardState *cs, int *intr_done)
369 debugl1(cs, "transmit_chars: p(%x) cnt(%x)", cs->hw.elsa.transp,
370 cs->hw.elsa.transcnt);
372 if (cs->hw.elsa.transcnt <= 0) {
373 cs->hw.elsa.IER &= ~UART_IER_THRI;
374 serial_out(cs, UART_IER, cs->hw.elsa.IER);
379 serial_outp(cs, UART_TX, cs->hw.elsa.transbuf[cs->hw.elsa.transp++]);
380 if (cs->hw.elsa.transp >= MAX_MODEM_BUF)
381 cs->hw.elsa.transp=0;
382 if (--cs->hw.elsa.transcnt <= 0)
384 } while (--count > 0);
385 if ((cs->hw.elsa.transcnt < WAKEUP_CHARS) && (cs->hw.elsa.MFlag==2))
386 modem_fill(cs->hw.elsa.bcs);
388 #ifdef SERIAL_DEBUG_INTR
393 if (cs->hw.elsa.transcnt <= 0) {
394 cs->hw.elsa.IER &= ~UART_IER_THRI;
395 serial_outp(cs, UART_IER, cs->hw.elsa.IER);
400 static void rs_interrupt_elsa(int irq, struct IsdnCardState *cs)
402 int status, iir, msr;
403 int pass_counter = 0;
405 #ifdef SERIAL_DEBUG_INTR
406 printk("rs_interrupt_single(%d)...", irq);
410 status = serial_inp(cs, UART_LSR);
411 debugl1(cs,"rs LSR %02x", status);
412 #ifdef SERIAL_DEBUG_INTR
413 printk("status = %x...", status);
415 if (status & UART_LSR_DR)
416 receive_chars(cs, &status);
417 if (status & UART_LSR_THRE)
418 transmit_chars(cs, 0);
419 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
420 printk("rs_single loop break.\n");
423 iir = serial_inp(cs, UART_IIR);
424 debugl1(cs,"rs IIR %02x", iir);
425 if ((iir & 0xf) == 0) {
426 msr = serial_inp(cs, UART_MSR);
427 debugl1(cs,"rs MSR %02x", msr);
429 } while (!(iir & UART_IIR_NO_INT));
430 #ifdef SERIAL_DEBUG_INTR
435 extern int open_hscxstate(struct IsdnCardState *cs, struct BCState *bcs);
436 extern void modehscx(struct BCState *bcs, int mode, int bc);
437 extern void hscx_l2l1(struct PStack *st, int pr, void *arg);
440 close_elsastate(struct BCState *bcs)
442 modehscx(bcs, 0, bcs->channel);
443 if (test_and_clear_bit(BC_FLG_INIT, &bcs->Flag)) {
444 if (bcs->hw.hscx.rcvbuf) {
445 if (bcs->mode != L1_MODE_MODEM)
446 kfree(bcs->hw.hscx.rcvbuf);
447 bcs->hw.hscx.rcvbuf = NULL;
449 skb_queue_purge(&bcs->rqueue);
450 skb_queue_purge(&bcs->squeue);
452 dev_kfree_skb_any(bcs->tx_skb);
454 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
460 modem_write_cmd(struct IsdnCardState *cs, u_char *buf, int len) {
469 if (len > (MAX_MODEM_BUF - cs->hw.elsa.transcnt)) {
470 restore_flags(flags);
473 fp = cs->hw.elsa.transcnt + cs->hw.elsa.transp;
474 fp &= (MAX_MODEM_BUF -1);
476 if (count > MAX_MODEM_BUF - fp) {
477 count = MAX_MODEM_BUF - fp;
478 memcpy(cs->hw.elsa.transbuf + fp, msg, count);
479 cs->hw.elsa.transcnt += count;
484 memcpy(cs->hw.elsa.transbuf + fp, msg, count);
485 cs->hw.elsa.transcnt += count;
486 if (cs->hw.elsa.transcnt &&
487 !(cs->hw.elsa.IER & UART_IER_THRI)) {
488 cs->hw.elsa.IER |= UART_IER_THRI;
489 serial_outp(cs, UART_IER, cs->hw.elsa.IER);
491 restore_flags(flags);
495 modem_set_init(struct IsdnCardState *cs) {
499 #define RCV_DELAY 20000
502 modem_write_cmd(cs, MInit_1, strlen(MInit_1));
504 while(timeout-- && cs->hw.elsa.transcnt)
506 debugl1(cs, "msi tout=%d", timeout);
508 modem_write_cmd(cs, MInit_2, strlen(MInit_2));
510 while(timeout-- && cs->hw.elsa.transcnt)
512 debugl1(cs, "msi tout=%d", timeout);
514 modem_write_cmd(cs, MInit_3, strlen(MInit_3));
516 while(timeout-- && cs->hw.elsa.transcnt)
518 debugl1(cs, "msi tout=%d", timeout);
520 modem_write_cmd(cs, MInit_4, strlen(MInit_4));
522 while(timeout-- && cs->hw.elsa.transcnt)
524 debugl1(cs, "msi tout=%d", timeout);
526 modem_write_cmd(cs, MInit_5, strlen(MInit_5));
528 while(timeout-- && cs->hw.elsa.transcnt)
530 debugl1(cs, "msi tout=%d", timeout);
532 modem_write_cmd(cs, MInit_6, strlen(MInit_6));
534 while(timeout-- && cs->hw.elsa.transcnt)
536 debugl1(cs, "msi tout=%d", timeout);
538 modem_write_cmd(cs, MInit_7, strlen(MInit_7));
540 while(timeout-- && cs->hw.elsa.transcnt)
542 debugl1(cs, "msi tout=%d", timeout);
544 restore_flags(flags);
548 modem_set_dial(struct IsdnCardState *cs, int outgoing) {
551 #define RCV_DELAY 20000
555 modem_write_cmd(cs, MInit_speed28800, strlen(MInit_speed28800));
557 while(timeout-- && cs->hw.elsa.transcnt)
559 debugl1(cs, "msi tout=%d", timeout);
562 modem_write_cmd(cs, MInit_dialout, strlen(MInit_dialout));
564 modem_write_cmd(cs, MInit_dialin, strlen(MInit_dialin));
566 while(timeout-- && cs->hw.elsa.transcnt)
568 debugl1(cs, "msi tout=%d", timeout);
570 restore_flags(flags);
574 modem_l2l1(struct PStack *st, int pr, void *arg)
576 struct sk_buff *skb = arg;
579 if (pr == (PH_DATA | REQUEST)) {
582 if (st->l1.bcs->tx_skb) {
583 skb_queue_tail(&st->l1.bcs->squeue, skb);
584 restore_flags(flags);
586 st->l1.bcs->tx_skb = skb;
587 test_and_set_bit(BC_FLG_BUSY, &st->l1.bcs->Flag);
588 st->l1.bcs->hw.hscx.count = 0;
589 restore_flags(flags);
590 write_modem(st->l1.bcs);
592 } else if (pr == (PH_ACTIVATE | REQUEST)) {
593 test_and_set_bit(BC_FLG_ACTIV, &st->l1.bcs->Flag);
594 st->l1.l1l2(st, PH_ACTIVATE | CONFIRM, NULL);
595 set_arcofi(st->l1.bcs->cs, st->l1.bc);
596 mstartup(st->l1.bcs->cs);
597 modem_set_dial(st->l1.bcs->cs, test_bit(FLG_ORIG, &st->l2.flag));
598 st->l1.bcs->cs->hw.elsa.MFlag=2;
599 } else if (pr == (PH_DEACTIVATE | REQUEST)) {
600 test_and_clear_bit(BC_FLG_ACTIV, &st->l1.bcs->Flag);
601 st->l1.bcs->cs->dc.isac.arcofi_bc = st->l1.bc;
602 arcofi_fsm(st->l1.bcs->cs, ARCOFI_START, &ARCOFI_XOP_0);
603 interruptible_sleep_on(&st->l1.bcs->cs->dc.isac.arcofi_wait);
604 st->l1.bcs->cs->hw.elsa.MFlag=1;
606 printk(KERN_WARNING"ElsaSer: unknown pr %x\n", pr);
611 setstack_elsa(struct PStack *st, struct BCState *bcs)
614 bcs->channel = st->l1.bc;
615 switch (st->l1.mode) {
618 if (open_hscxstate(st->l1.hardware, bcs))
620 st->l2.l2l1 = hscx_l2l1;
623 bcs->mode = L1_MODE_MODEM;
624 if (!test_and_set_bit(BC_FLG_INIT, &bcs->Flag)) {
625 bcs->hw.hscx.rcvbuf = bcs->cs->hw.elsa.rcvbuf;
626 skb_queue_head_init(&bcs->rqueue);
627 skb_queue_head_init(&bcs->squeue);
630 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
632 bcs->hw.hscx.rcvidx = 0;
634 bcs->cs->hw.elsa.bcs = bcs;
635 st->l2.l2l1 = modem_l2l1;
639 setstack_manager(st);
646 init_modem(struct IsdnCardState *cs) {
648 cs->bcs[0].BC_SetStack = setstack_elsa;
649 cs->bcs[1].BC_SetStack = setstack_elsa;
650 cs->bcs[0].BC_Close = close_elsastate;
651 cs->bcs[1].BC_Close = close_elsastate;
652 if (!(cs->hw.elsa.rcvbuf = kmalloc(MAX_MODEM_BUF,
655 "Elsa: No modem mem hw.elsa.rcvbuf\n");
658 if (!(cs->hw.elsa.transbuf = kmalloc(MAX_MODEM_BUF,
661 "Elsa: No modem mem hw.elsa.transbuf\n");
662 kfree(cs->hw.elsa.rcvbuf);
663 cs->hw.elsa.rcvbuf = NULL;
667 printk(KERN_WARNING "Elsa: problem startup modem\n");
673 release_modem(struct IsdnCardState *cs) {
675 cs->hw.elsa.MFlag = 0;
676 if (cs->hw.elsa.transbuf) {
677 if (cs->hw.elsa.rcvbuf) {
679 kfree(cs->hw.elsa.rcvbuf);
680 cs->hw.elsa.rcvbuf = NULL;
682 kfree(cs->hw.elsa.transbuf);
683 cs->hw.elsa.transbuf = NULL;