Merge branch 'master' of /home/trondmy/kernel/linux-2.6/
[linux-2.6] / drivers / isdn / hisax / elsa_ser.c
1 /* $Id: elsa_ser.c,v 2.14.2.3 2004/02/11 13:21:33 keil Exp $
2  *
3  * stuff for the serial modem on ELSA cards
4  *
5  * This software may be used and distributed according to the terms
6  * of the GNU General Public License, incorporated herein by reference.
7  *
8  */
9
10 #include <linux/serial.h>
11 #include <linux/serial_reg.h>
12
13 #define MAX_MODEM_BUF   256
14 #define WAKEUP_CHARS    (MAX_MODEM_BUF/2)
15 #define RS_ISR_PASS_LIMIT 256
16 #define BASE_BAUD ( 1843200 / 16 )
17
18 //#define SERIAL_DEBUG_OPEN 1
19 //#define SERIAL_DEBUG_INTR 1
20 //#define SERIAL_DEBUG_FLOW 1
21 #undef SERIAL_DEBUG_OPEN
22 #undef SERIAL_DEBUG_INTR
23 #undef SERIAL_DEBUG_FLOW
24 #undef SERIAL_DEBUG_REG
25 //#define SERIAL_DEBUG_REG 1
26
27 #ifdef SERIAL_DEBUG_REG
28 static u_char deb[32];
29 const char *ModemIn[] = {"RBR","IER","IIR","LCR","MCR","LSR","MSR","SCR"};
30 const char *ModemOut[] = {"THR","IER","FCR","LCR","MCR","LSR","MSR","SCR"};
31 #endif
32
33 static char *MInit_1 = "AT&F&C1E0&D2\r\0";
34 static char *MInit_2 = "ATL2M1S64=13\r\0";
35 static char *MInit_3 = "AT+FCLASS=0\r\0";
36 static char *MInit_4 = "ATV1S2=128X1\r\0";
37 static char *MInit_5 = "AT\\V8\\N3\r\0";
38 static char *MInit_6 = "ATL0M0&G0%E1\r\0";
39 static char *MInit_7 = "AT%L1%M0%C3\r\0";
40
41 static char *MInit_speed28800 = "AT%G0%B28800\r\0";
42
43 static char *MInit_dialout = "ATs7=60 x1 d\r\0";
44 static char *MInit_dialin = "ATs7=60 x1 a\r\0";
45
46
47 static inline unsigned int serial_in(struct IsdnCardState *cs, int offset)
48 {
49 #ifdef SERIAL_DEBUG_REG
50         u_int val = inb(cs->hw.elsa.base + 8 + offset);
51         debugl1(cs,"in   %s %02x",ModemIn[offset], val);
52         return(val);
53 #else
54         return inb(cs->hw.elsa.base + 8 + offset);
55 #endif
56 }
57
58 static inline unsigned int serial_inp(struct IsdnCardState *cs, int offset)
59 {
60 #ifdef SERIAL_DEBUG_REG
61 #ifdef ELSA_SERIAL_NOPAUSE_IO
62         u_int val = inb(cs->hw.elsa.base + 8 + offset);
63         debugl1(cs,"inp  %s %02x",ModemIn[offset], val);
64 #else
65         u_int val = inb_p(cs->hw.elsa.base + 8 + offset);
66         debugl1(cs,"inP  %s %02x",ModemIn[offset], val);
67 #endif
68         return(val);
69 #else
70 #ifdef ELSA_SERIAL_NOPAUSE_IO
71         return inb(cs->hw.elsa.base + 8 + offset);
72 #else
73         return inb_p(cs->hw.elsa.base + 8 + offset);
74 #endif
75 #endif
76 }
77
78 static inline void serial_out(struct IsdnCardState *cs, int offset, int value)
79 {
80 #ifdef SERIAL_DEBUG_REG
81         debugl1(cs,"out  %s %02x",ModemOut[offset], value);
82 #endif
83         outb(value, cs->hw.elsa.base + 8 + offset);
84 }
85
86 static inline void serial_outp(struct IsdnCardState *cs, int offset,
87                                int value)
88 {
89 #ifdef SERIAL_DEBUG_REG
90 #ifdef ELSA_SERIAL_NOPAUSE_IO
91         debugl1(cs,"outp %s %02x",ModemOut[offset], value);
92 #else
93         debugl1(cs,"outP %s %02x",ModemOut[offset], value);
94 #endif
95 #endif
96 #ifdef ELSA_SERIAL_NOPAUSE_IO
97         outb(value, cs->hw.elsa.base + 8 + offset);
98 #else
99         outb_p(value, cs->hw.elsa.base + 8 + offset);
100 #endif
101 }
102
103 /*
104  * This routine is called to set the UART divisor registers to match
105  * the specified baud rate for a serial port.
106  */
107 static void change_speed(struct IsdnCardState *cs, int baud)
108 {
109         int     quot = 0, baud_base;
110         unsigned cval, fcr = 0;
111         int     bits;
112
113
114         /* byte size and parity */
115         cval = 0x03; bits = 10;
116         /* Determine divisor based on baud rate */
117         baud_base = BASE_BAUD;
118         quot = baud_base / baud;
119         /* If the quotient is ever zero, default to 9600 bps */
120         if (!quot)
121                 quot = baud_base / 9600;
122
123         /* Set up FIFO's */
124         if ((baud_base / quot) < 2400)
125                 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
126         else
127                 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
128         serial_outp(cs, UART_FCR, fcr);
129         /* CTS flow control flag and modem status interrupts */
130         cs->hw.elsa.IER &= ~UART_IER_MSI;
131         cs->hw.elsa.IER |= UART_IER_MSI;
132         serial_outp(cs, UART_IER, cs->hw.elsa.IER);
133
134         debugl1(cs,"modem quot=0x%x", quot);
135         serial_outp(cs, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
136         serial_outp(cs, UART_DLL, quot & 0xff);         /* LS of divisor */
137         serial_outp(cs, UART_DLM, quot >> 8);           /* MS of divisor */
138         serial_outp(cs, UART_LCR, cval);                /* reset DLAB */
139         serial_inp(cs, UART_RX);
140 }
141
142 static int mstartup(struct IsdnCardState *cs)
143 {
144         int     retval=0;
145
146         /*
147          * Clear the FIFO buffers and disable them
148          * (they will be reenabled in change_speed())
149          */
150         serial_outp(cs, UART_FCR, (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT));
151
152         /*
153          * At this point there's no way the LSR could still be 0xFF;
154          * if it is, then bail out, because there's likely no UART
155          * here.
156          */
157         if (serial_inp(cs, UART_LSR) == 0xff) {
158                 retval = -ENODEV;
159                 goto errout;
160         }
161         
162         /*
163          * Clear the interrupt registers.
164          */
165         (void) serial_inp(cs, UART_RX);
166         (void) serial_inp(cs, UART_IIR);
167         (void) serial_inp(cs, UART_MSR);
168
169         /*
170          * Now, initialize the UART 
171          */
172         serial_outp(cs, UART_LCR, UART_LCR_WLEN8);      /* reset DLAB */
173
174         cs->hw.elsa.MCR = 0;
175         cs->hw.elsa.MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
176         serial_outp(cs, UART_MCR, cs->hw.elsa.MCR);
177         
178         /*
179          * Finally, enable interrupts
180          */
181         cs->hw.elsa.IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
182         serial_outp(cs, UART_IER, cs->hw.elsa.IER);     /* enable interrupts */
183         
184         /*
185          * And clear the interrupt registers again for luck.
186          */
187         (void)serial_inp(cs, UART_LSR);
188         (void)serial_inp(cs, UART_RX);
189         (void)serial_inp(cs, UART_IIR);
190         (void)serial_inp(cs, UART_MSR);
191
192         cs->hw.elsa.transcnt = cs->hw.elsa.transp = 0;
193         cs->hw.elsa.rcvcnt = cs->hw.elsa.rcvp =0;
194
195         /*
196          * and set the speed of the serial port
197          */
198         change_speed(cs, BASE_BAUD);
199         cs->hw.elsa.MFlag = 1;
200 errout:
201         return retval;
202 }
203
204 /*
205  * This routine will shutdown a serial port; interrupts are disabled, and
206  * DTR is dropped if the hangup on close termio flag is on.
207  */
208 static void mshutdown(struct IsdnCardState *cs)
209 {
210
211 #ifdef SERIAL_DEBUG_OPEN
212         printk(KERN_DEBUG"Shutting down serial ....");
213 #endif
214         
215         /*
216          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
217          * here so the queue might never be waken up
218          */
219
220         cs->hw.elsa.IER = 0;
221         serial_outp(cs, UART_IER, 0x00);        /* disable all intrs */
222         cs->hw.elsa.MCR &= ~UART_MCR_OUT2;
223         
224         /* disable break condition */
225         serial_outp(cs, UART_LCR, serial_inp(cs, UART_LCR) & ~UART_LCR_SBC);
226         
227         cs->hw.elsa.MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
228         serial_outp(cs, UART_MCR, cs->hw.elsa.MCR);
229
230         /* disable FIFO's */    
231         serial_outp(cs, UART_FCR, (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT));
232         serial_inp(cs, UART_RX);    /* read data port to reset things */
233         
234 #ifdef SERIAL_DEBUG_OPEN
235         printk(" done\n");
236 #endif
237 }
238
239 static inline int
240 write_modem(struct BCState *bcs) {
241         int ret=0;
242         struct IsdnCardState *cs = bcs->cs;
243         int count, len, fp;
244         
245         if (!bcs->tx_skb)
246                 return 0;
247         if (bcs->tx_skb->len <= 0)
248                 return 0;
249         len = bcs->tx_skb->len;
250         if (len > MAX_MODEM_BUF - cs->hw.elsa.transcnt)
251                 len = MAX_MODEM_BUF - cs->hw.elsa.transcnt;
252         fp = cs->hw.elsa.transcnt + cs->hw.elsa.transp;
253         fp &= (MAX_MODEM_BUF -1);
254         count = len;
255         if (count > MAX_MODEM_BUF - fp) {
256                 count = MAX_MODEM_BUF - fp;
257                 memcpy(cs->hw.elsa.transbuf + fp, bcs->tx_skb->data, count);
258                 skb_pull(bcs->tx_skb, count);
259                 cs->hw.elsa.transcnt += count;
260                 ret = count;
261                 count = len - count;
262                 fp = 0;
263         }
264         memcpy((cs->hw.elsa.transbuf + fp), bcs->tx_skb->data, count);
265         skb_pull(bcs->tx_skb, count);
266         cs->hw.elsa.transcnt += count;
267         ret += count;
268         
269         if (cs->hw.elsa.transcnt && 
270             !(cs->hw.elsa.IER & UART_IER_THRI)) {
271                         cs->hw.elsa.IER |= UART_IER_THRI;
272                 serial_outp(cs, UART_IER, cs->hw.elsa.IER);
273         }
274         return(ret);
275 }
276
277 static inline void
278 modem_fill(struct BCState *bcs) {
279                 
280         if (bcs->tx_skb) {
281                 if (bcs->tx_skb->len) {
282                         write_modem(bcs);
283                         return;
284                 } else {
285                         if (test_bit(FLG_LLI_L1WAKEUP,&bcs->st->lli.flag) &&
286                                 (PACKET_NOACK != bcs->tx_skb->pkt_type)) {
287                                 u_long  flags;
288                                 spin_lock_irqsave(&bcs->aclock, flags);
289                                 bcs->ackcnt += bcs->hw.hscx.count;
290                                 spin_unlock_irqrestore(&bcs->aclock, flags);
291                                 schedule_event(bcs, B_ACKPENDING);
292                         }
293                         dev_kfree_skb_any(bcs->tx_skb);
294                         bcs->tx_skb = NULL;
295                 }
296         }
297         if ((bcs->tx_skb = skb_dequeue(&bcs->squeue))) {
298                 bcs->hw.hscx.count = 0;
299                 test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
300                 write_modem(bcs);
301         } else {
302                 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
303                 schedule_event(bcs, B_XMTBUFREADY);
304         }
305 }
306
307 static inline void receive_chars(struct IsdnCardState *cs,
308                                  int *status)
309 {
310         unsigned char ch;
311         struct sk_buff *skb;
312
313         do {
314                 ch = serial_in(cs, UART_RX);
315                 if (cs->hw.elsa.rcvcnt >= MAX_MODEM_BUF)
316                         break;
317                 cs->hw.elsa.rcvbuf[cs->hw.elsa.rcvcnt++] = ch;
318 #ifdef SERIAL_DEBUG_INTR
319                 printk("DR%02x:%02x...", ch, *status);
320 #endif
321                 if (*status & (UART_LSR_BI | UART_LSR_PE |
322                                UART_LSR_FE | UART_LSR_OE)) {
323                                         
324 #ifdef SERIAL_DEBUG_INTR
325                         printk("handling exept....");
326 #endif
327                 }
328                 *status = serial_inp(cs, UART_LSR);
329         } while (*status & UART_LSR_DR);
330         if (cs->hw.elsa.MFlag == 2) {
331                 if (!(skb = dev_alloc_skb(cs->hw.elsa.rcvcnt)))
332                         printk(KERN_WARNING "ElsaSER: receive out of memory\n");
333                 else {
334                         memcpy(skb_put(skb, cs->hw.elsa.rcvcnt), cs->hw.elsa.rcvbuf, 
335                                 cs->hw.elsa.rcvcnt);
336                         skb_queue_tail(& cs->hw.elsa.bcs->rqueue, skb);
337                 }
338                 schedule_event(cs->hw.elsa.bcs, B_RCVBUFREADY);
339         } else {
340                 char tmp[128];
341                 char *t = tmp;
342
343                 t += sprintf(t, "modem read cnt %d", cs->hw.elsa.rcvcnt);
344                 QuickHex(t, cs->hw.elsa.rcvbuf, cs->hw.elsa.rcvcnt);
345                 debugl1(cs, tmp);
346         }
347         cs->hw.elsa.rcvcnt = 0;
348 }
349
350 static inline void transmit_chars(struct IsdnCardState *cs, int *intr_done)
351 {
352         int count;
353         
354         debugl1(cs, "transmit_chars: p(%x) cnt(%x)", cs->hw.elsa.transp, 
355                 cs->hw.elsa.transcnt);
356         
357         if (cs->hw.elsa.transcnt <= 0) {
358                 cs->hw.elsa.IER &= ~UART_IER_THRI;
359                 serial_out(cs, UART_IER, cs->hw.elsa.IER);
360                 return;
361         }
362         count = 16;
363         do {
364                 serial_outp(cs, UART_TX, cs->hw.elsa.transbuf[cs->hw.elsa.transp++]);
365                 if (cs->hw.elsa.transp >= MAX_MODEM_BUF)
366                         cs->hw.elsa.transp=0;
367                 if (--cs->hw.elsa.transcnt <= 0)
368                         break;
369         } while (--count > 0);
370         if ((cs->hw.elsa.transcnt < WAKEUP_CHARS) && (cs->hw.elsa.MFlag==2))
371                 modem_fill(cs->hw.elsa.bcs);
372
373 #ifdef SERIAL_DEBUG_INTR
374         printk("THRE...");
375 #endif
376         if (intr_done)
377                 *intr_done = 0;
378         if (cs->hw.elsa.transcnt <= 0) {
379                 cs->hw.elsa.IER &= ~UART_IER_THRI;
380                 serial_outp(cs, UART_IER, cs->hw.elsa.IER);
381         }
382 }
383
384
385 static void rs_interrupt_elsa(int irq, struct IsdnCardState *cs)
386 {
387         int status, iir, msr;
388         int pass_counter = 0;
389         
390 #ifdef SERIAL_DEBUG_INTR
391         printk("rs_interrupt_single(%d)...", irq);
392 #endif
393
394         do {
395                 status = serial_inp(cs, UART_LSR);
396                 debugl1(cs,"rs LSR %02x", status);
397 #ifdef SERIAL_DEBUG_INTR
398                 printk("status = %x...", status);
399 #endif
400                 if (status & UART_LSR_DR)
401                         receive_chars(cs, &status);
402                 if (status & UART_LSR_THRE)
403                         transmit_chars(cs, NULL);
404                 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
405                         printk("rs_single loop break.\n");
406                         break;
407                 }
408                 iir = serial_inp(cs, UART_IIR);
409                 debugl1(cs,"rs IIR %02x", iir);
410                 if ((iir & 0xf) == 0) {
411                         msr = serial_inp(cs, UART_MSR);
412                         debugl1(cs,"rs MSR %02x", msr);
413                 }
414         } while (!(iir & UART_IIR_NO_INT));
415 #ifdef SERIAL_DEBUG_INTR
416         printk("end.\n");
417 #endif
418 }
419
420 extern int open_hscxstate(struct IsdnCardState *cs, struct BCState *bcs);
421 extern void modehscx(struct BCState *bcs, int mode, int bc);
422 extern void hscx_l2l1(struct PStack *st, int pr, void *arg);
423
424 static void
425 close_elsastate(struct BCState *bcs)
426 {
427         modehscx(bcs, 0, bcs->channel);
428         if (test_and_clear_bit(BC_FLG_INIT, &bcs->Flag)) {
429                 if (bcs->hw.hscx.rcvbuf) {
430                         if (bcs->mode != L1_MODE_MODEM)
431                                 kfree(bcs->hw.hscx.rcvbuf);
432                         bcs->hw.hscx.rcvbuf = NULL;
433                 }
434                 skb_queue_purge(&bcs->rqueue);
435                 skb_queue_purge(&bcs->squeue);
436                 if (bcs->tx_skb) {
437                         dev_kfree_skb_any(bcs->tx_skb);
438                         bcs->tx_skb = NULL;
439                         test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
440                 }
441         }
442 }
443
444 static void
445 modem_write_cmd(struct IsdnCardState *cs, u_char *buf, int len) {
446         int count, fp;
447         u_char *msg = buf;
448         
449         if (!len)
450                 return;
451         if (len > (MAX_MODEM_BUF - cs->hw.elsa.transcnt)) {
452                 return;
453         }
454         fp = cs->hw.elsa.transcnt + cs->hw.elsa.transp;
455         fp &= (MAX_MODEM_BUF -1);
456         count = len;
457         if (count > MAX_MODEM_BUF - fp) {
458                 count = MAX_MODEM_BUF - fp;
459                 memcpy(cs->hw.elsa.transbuf + fp, msg, count);
460                 cs->hw.elsa.transcnt += count;
461                 msg += count;
462                 count = len - count;
463                 fp = 0;
464         }
465         memcpy(cs->hw.elsa.transbuf + fp, msg, count);
466         cs->hw.elsa.transcnt += count;
467         if (cs->hw.elsa.transcnt && 
468             !(cs->hw.elsa.IER & UART_IER_THRI)) {
469                 cs->hw.elsa.IER |= UART_IER_THRI;
470                 serial_outp(cs, UART_IER, cs->hw.elsa.IER);
471         }
472 }
473
474 static void
475 modem_set_init(struct IsdnCardState *cs) {
476         int timeout;
477
478 #define RCV_DELAY 20000 
479         modem_write_cmd(cs, MInit_1, strlen(MInit_1));
480         timeout = 1000;
481         while(timeout-- && cs->hw.elsa.transcnt)
482                 udelay(1000);
483         debugl1(cs, "msi tout=%d", timeout);
484         udelay(RCV_DELAY);
485         modem_write_cmd(cs, MInit_2, strlen(MInit_2));
486         timeout = 1000;
487         while(timeout-- && cs->hw.elsa.transcnt)
488                 udelay(1000);
489         debugl1(cs, "msi tout=%d", timeout);
490         udelay(RCV_DELAY);
491         modem_write_cmd(cs, MInit_3, strlen(MInit_3));
492         timeout = 1000;
493         while(timeout-- && cs->hw.elsa.transcnt)
494                 udelay(1000);
495         debugl1(cs, "msi tout=%d", timeout);
496         udelay(RCV_DELAY);
497         modem_write_cmd(cs, MInit_4, strlen(MInit_4));
498         timeout = 1000;
499         while(timeout-- && cs->hw.elsa.transcnt)
500                 udelay(1000);
501         debugl1(cs, "msi tout=%d", timeout);
502         udelay(RCV_DELAY );
503         modem_write_cmd(cs, MInit_5, strlen(MInit_5));
504         timeout = 1000;
505         while(timeout-- && cs->hw.elsa.transcnt)
506                 udelay(1000);
507         debugl1(cs, "msi tout=%d", timeout);
508         udelay(RCV_DELAY);
509         modem_write_cmd(cs, MInit_6, strlen(MInit_6));
510         timeout = 1000;
511         while(timeout-- && cs->hw.elsa.transcnt)
512                 udelay(1000);
513         debugl1(cs, "msi tout=%d", timeout);
514         udelay(RCV_DELAY);
515         modem_write_cmd(cs, MInit_7, strlen(MInit_7));
516         timeout = 1000;
517         while(timeout-- && cs->hw.elsa.transcnt)
518                 udelay(1000);
519         debugl1(cs, "msi tout=%d", timeout);
520         udelay(RCV_DELAY);
521 }
522
523 static void
524 modem_set_dial(struct IsdnCardState *cs, int outgoing) {
525         int timeout;
526 #define RCV_DELAY 20000 
527
528         modem_write_cmd(cs, MInit_speed28800, strlen(MInit_speed28800));
529         timeout = 1000;
530         while(timeout-- && cs->hw.elsa.transcnt)
531                 udelay(1000);
532         debugl1(cs, "msi tout=%d", timeout);
533         udelay(RCV_DELAY);
534         if (outgoing)
535                 modem_write_cmd(cs, MInit_dialout, strlen(MInit_dialout));
536         else
537                 modem_write_cmd(cs, MInit_dialin, strlen(MInit_dialin));
538         timeout = 1000;
539         while(timeout-- && cs->hw.elsa.transcnt)
540                 udelay(1000);
541         debugl1(cs, "msi tout=%d", timeout);
542         udelay(RCV_DELAY);
543 }
544
545 static void
546 modem_l2l1(struct PStack *st, int pr, void *arg)
547 {
548         struct BCState *bcs = st->l1.bcs;
549         struct sk_buff *skb = arg;
550         u_long flags;
551
552         if (pr == (PH_DATA | REQUEST)) {
553                 spin_lock_irqsave(&bcs->cs->lock, flags);
554                 if (bcs->tx_skb) {
555                         skb_queue_tail(&bcs->squeue, skb);
556                 } else {
557                         bcs->tx_skb = skb;
558                         test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
559                         bcs->hw.hscx.count = 0;
560                         write_modem(bcs);
561                 }
562                 spin_unlock_irqrestore(&bcs->cs->lock, flags);
563         } else if (pr == (PH_ACTIVATE | REQUEST)) {
564                 test_and_set_bit(BC_FLG_ACTIV, &bcs->Flag);
565                 st->l1.l1l2(st, PH_ACTIVATE | CONFIRM, NULL);
566                 set_arcofi(bcs->cs, st->l1.bc);
567                 mstartup(bcs->cs);
568                 modem_set_dial(bcs->cs, test_bit(FLG_ORIG, &st->l2.flag));
569                 bcs->cs->hw.elsa.MFlag=2;
570         } else if (pr == (PH_DEACTIVATE | REQUEST)) {
571                 test_and_clear_bit(BC_FLG_ACTIV, &bcs->Flag);
572                 bcs->cs->dc.isac.arcofi_bc = st->l1.bc;
573                 arcofi_fsm(bcs->cs, ARCOFI_START, &ARCOFI_XOP_0);
574                 interruptible_sleep_on(&bcs->cs->dc.isac.arcofi_wait);
575                 bcs->cs->hw.elsa.MFlag=1;
576         } else {
577                 printk(KERN_WARNING"ElsaSer: unknown pr %x\n", pr);
578         }
579 }
580
581 static int
582 setstack_elsa(struct PStack *st, struct BCState *bcs)
583 {
584
585         bcs->channel = st->l1.bc;
586         switch (st->l1.mode) {
587                 case L1_MODE_HDLC:
588                 case L1_MODE_TRANS:
589                         if (open_hscxstate(st->l1.hardware, bcs))
590                                 return (-1);
591                         st->l2.l2l1 = hscx_l2l1;
592                         break;
593                 case L1_MODE_MODEM:
594                         bcs->mode = L1_MODE_MODEM;
595                         if (!test_and_set_bit(BC_FLG_INIT, &bcs->Flag)) {
596                                 bcs->hw.hscx.rcvbuf = bcs->cs->hw.elsa.rcvbuf;
597                                 skb_queue_head_init(&bcs->rqueue);
598                                 skb_queue_head_init(&bcs->squeue);
599                         }
600                         bcs->tx_skb = NULL;
601                         test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
602                         bcs->event = 0;
603                         bcs->hw.hscx.rcvidx = 0;
604                         bcs->tx_cnt = 0;
605                         bcs->cs->hw.elsa.bcs = bcs;
606                         st->l2.l2l1 = modem_l2l1;
607                         break;
608         }
609         st->l1.bcs = bcs;
610         setstack_manager(st);
611         bcs->st = st;
612         setstack_l1_B(st);
613         return (0);
614 }
615
616 static void
617 init_modem(struct IsdnCardState *cs) {
618
619         cs->bcs[0].BC_SetStack = setstack_elsa;
620         cs->bcs[1].BC_SetStack = setstack_elsa;
621         cs->bcs[0].BC_Close = close_elsastate;
622         cs->bcs[1].BC_Close = close_elsastate;
623         if (!(cs->hw.elsa.rcvbuf = kmalloc(MAX_MODEM_BUF,
624                 GFP_ATOMIC))) {
625                 printk(KERN_WARNING
626                         "Elsa: No modem mem hw.elsa.rcvbuf\n");
627                 return;
628         }
629         if (!(cs->hw.elsa.transbuf = kmalloc(MAX_MODEM_BUF,
630                 GFP_ATOMIC))) {
631                 printk(KERN_WARNING
632                         "Elsa: No modem mem hw.elsa.transbuf\n");
633                 kfree(cs->hw.elsa.rcvbuf);
634                 cs->hw.elsa.rcvbuf = NULL;
635                 return;
636         }
637         if (mstartup(cs)) {
638                 printk(KERN_WARNING "Elsa: problem startup modem\n");
639         }
640         modem_set_init(cs);
641 }
642
643 static void
644 release_modem(struct IsdnCardState *cs) {
645
646         cs->hw.elsa.MFlag = 0;
647         if (cs->hw.elsa.transbuf) {
648                 if (cs->hw.elsa.rcvbuf) {
649                         mshutdown(cs);
650                         kfree(cs->hw.elsa.rcvbuf);
651                         cs->hw.elsa.rcvbuf = NULL;
652                 }
653                 kfree(cs->hw.elsa.transbuf);
654                 cs->hw.elsa.transbuf = NULL;
655         }
656 }