kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / net / i4b / layer1 / ifpnp / i4b_ifpnp_avm.c
1 /*
2  *   Copyright (c) 1999, 2000 Udo Schweigert. All rights reserved.
3  *
4  *   Redistribution and use in source and binary forms, with or without
5  *   modification, are permitted provided that the following conditions
6  *   are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *   2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  *   3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *   4. Altered versions must be plainly marked as such, and must not be
17  *      misrepresented as being the original software and/or documentation.
18  *   
19  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  *   SUCH DAMAGE.
30  *
31  *---------------------------------------------------------------------------
32  *
33  *      i4b_ifpnp_avm.c: AVM Fritz!Card PnP hardware driver
34  *      ---------------------------------------------------
35  *
36  * $FreeBSD: src/sys/i4b/layer1/ifpnp/i4b_ifpnp_avm.c,v 1.5.2.1 2001/08/10 14:08:37 obrien Exp $
37  * $DragonFly: src/sys/net/i4b/layer1/ifpnp/i4b_ifpnp_avm.c,v 1.4 2003/08/07 21:17:26 dillon Exp $
38  *
39  *      last edit-date: [Fri Jan 12 17:05:28 2001]
40  *
41  *---------------------------------------------------------------------------*/
42
43 #include "use_ifpnp.h"
44 #include "opt_i4b.h"
45
46 #if (NIFPNP > 0)
47
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52
53 #include <machine/bus.h>
54 #include <sys/bus.h>
55 #include <sys/rman.h>
56
57 #include <bus/isa/isavar.h>
58
59 #include <sys/socket.h>
60 #include <net/if.h>
61
62 #include <net/i4b/include/machine/i4b_debug.h>
63 #include <net/i4b/include/machine/i4b_ioctl.h>
64 #include <net/i4b/include/machine/i4b_trace.h>
65
66 #include "../../include/i4b_global.h"
67 #include "../../include/i4b_mbuf.h"
68
69 #include "../i4b_l1.h"
70 #include "../isic/i4b_isic.h"
71 #include "../isic/i4b_isac.h"
72 #include "../isic/i4b_hscx.h"
73
74 #include "i4b_ifpnp_ext.h"
75
76 /* prototypes */
77 static void avm_pnp_intr(void *);
78 static void hscx_write_reg(int, u_int, struct l1_softc *, u_int);
79 static void hscx_write_reg_val(int, u_int, u_int8_t, struct l1_softc *);
80 static u_int hscx_read_reg(int, u_int, struct l1_softc *);
81 static void hscx_read_fifo(int, void *, size_t, struct l1_softc *);
82 static void hscx_write_fifo(int, void *, size_t, struct l1_softc *);
83 static void avm_pnp_hscx_int_handler(struct l1_softc *);
84 static void avm_pnp_hscx_intr(int, u_int, u_int, struct l1_softc *);
85 static void avm_pnp_init_linktab(struct l1_softc *);
86 static void avm_pnp_bchannel_setup(int, int, int, int);
87 static void avm_pnp_bchannel_start(int, int);
88 static void avm_pnp_hscx_init(struct l1_softc *, int, int);
89 static void avm_pnp_bchannel_stat(int, int, bchan_statistics_t *);
90 static void avm_pnp_set_linktab(int, int, drvr_link_t *);
91 static isdn_link_t * avm_pnp_ret_linktab(int, int);
92 static int avm_pnp_probe(device_t);
93 static int avm_pnp_hscx_fifo(l1_bchan_state_t *, struct l1_softc *);
94 int avm_pnp_attach(device_t);
95 static void ifpnp_isac_intr(struct l1_softc *sc);
96
97 static device_method_t avm_pnp_methods[] = {
98         /* Device interface */
99         DEVMETHOD(device_probe,         avm_pnp_probe),
100         DEVMETHOD(device_attach,        avm_pnp_attach),
101         { 0, 0 }
102 };
103
104 static driver_t avm_pnp_driver = {
105         "ifpnp",
106         avm_pnp_methods,
107         sizeof(struct l1_softc)
108 };
109
110 static devclass_t avm_pnp_devclass;
111
112 DRIVER_MODULE(avm_pnp, isa, avm_pnp_driver, avm_pnp_devclass, 0, 0);
113
114 /* jump table for multiplex routines */
115
116 struct i4b_l1mux_func avm_pnp_l1mux_func = {
117         avm_pnp_ret_linktab,
118         avm_pnp_set_linktab,
119         ifpnp_mph_command_req,
120         ifpnp_ph_data_req,
121         ifpnp_ph_activate_req,
122 };
123
124 struct l1_softc *ifpnp_scp[IFPNP_MAXUNIT];
125
126 /*---------------------------------------------------------------------------*
127  *      AVM PnP Fritz!Card special registers
128  *---------------------------------------------------------------------------*/
129
130 /*
131  *      register offsets from i/o base
132  */
133 #define CLASS_OFFSET            0x00
134 #define REVISION_OFFSET         0x01
135 #define STAT0_OFFSET            0x02
136 #define STAT1_OFFSET            0x03
137 #define ADDR_REG_OFFSET         0x04
138 /*#define MODREG_OFFSET         0x06
139 #define VERREG_OFFSET           0x07*/
140
141 /* these 2 are used to select an ISAC register set */
142 #define ISAC_LO_REG_OFFSET      0x04
143 #define ISAC_HI_REG_OFFSET      0x06
144
145 /* offset higher than this goes to the HI register set */
146 #define MAX_LO_REG_OFFSET       0x2f
147
148 /* mask for the offset */
149 #define ISAC_REGSET_MASK        0x0f
150
151 /* the offset from the base to the ISAC registers */
152 #define ISAC_REG_OFFSET         0x10
153
154 /* the offset from the base to the ISAC FIFO */
155 #define ISAC_FIFO               0x02
156
157 /* not really the HSCX, but sort of */
158 #define HSCX_FIFO               0x00
159 #define HSCX_STAT               0x04
160
161 /*
162  *      AVM PnP Status Latch 0 read only bits
163  */
164 #define ASL_IRQ_ISAC            0x01    /* ISAC  interrupt, active low */
165 #define ASL_IRQ_HSCX            0x02    /* HSX   interrupt, active low */
166 #define ASL_IRQ_TIMER           0x04    /* Timer interrupt, active low */
167 #define ASL_IRQ_BCHAN           ASL_IRQ_HSCX
168 /* actually active LOW */
169 #define ASL_IRQ_Pending         (ASL_IRQ_ISAC | ASL_IRQ_HSCX | ASL_IRQ_TIMER)
170
171 /*
172  *      AVM Status Latch 0 write only bits
173  */
174 #define ASL_RESET_ALL           0x01  /* reset siemens IC's, active 1 */
175 #define ASL_TIMERDISABLE        0x02  /* active high */
176 #define ASL_TIMERRESET          0x04  /* active high */
177 #define ASL_ENABLE_INT          0x08  /* active high */
178 #define ASL_TESTBIT             0x10  /* active high */
179
180 /*
181  *      AVM Status Latch 1 write only bits
182  */
183 #define ASL1_INTSEL              0x0f  /* active high */
184 #define ASL1_ENABLE_IOM          0x80  /* active high */
185
186 /*
187  * "HSCX" mode bits
188  */
189 #define  HSCX_MODE_ITF_FLG      0x01
190 #define  HSCX_MODE_TRANS        0x02
191 #define  HSCX_MODE_CCR_7        0x04
192 #define  HSCX_MODE_CCR_16       0x08
193 #define  HSCX_MODE_TESTLOOP     0x80
194
195 /*
196  * "HSCX" status bits
197  */
198 #define  HSCX_STAT_RME          0x01
199 #define  HSCX_STAT_RDO          0x10
200 #define  HSCX_STAT_CRCVFRRAB    0x0E
201 #define  HSCX_STAT_CRCVFR       0x06
202 #define  HSCX_STAT_RML_MASK     0x3f00
203
204 /*
205  * "HSCX" interrupt bits
206  */
207 #define  HSCX_INT_XPR           0x80
208 #define  HSCX_INT_XDU           0x40
209 #define  HSCX_INT_RPR           0x20
210 #define  HSCX_INT_MASK          0xE0
211
212 /*
213  * "HSCX" command bits
214  */
215 #define  HSCX_CMD_XRS           0x80
216 #define  HSCX_CMD_XME           0x01
217 #define  HSCX_CMD_RRS           0x20
218 #define  HSCX_CMD_XML_MASK      0x3f00
219
220 /*
221  * to prevent deactivating the "HSCX" when both channels are active we
222  * define an HSCX_ACTIVE flag which is or'd into the channel's state
223  * flag in avm_pnp_bchannel_setup upon active and cleared upon deactivation.
224  * It is set high to allow room for new flags.
225  */
226 #define HSCX_AVMA1PP_ACTIVE     0x1000 
227
228 /*---------------------------------------------------------------------------*
229  *      AVM read fifo routines
230  *---------------------------------------------------------------------------*/
231
232 static void
233 avm_pnp_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
234 {
235         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
236         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
237
238         switch (what) {
239                 case ISIC_WHAT_ISAC:
240                         bus_space_write_1(btag, bhandle,  ADDR_REG_OFFSET, ISAC_FIFO);
241                         bus_space_read_multi_1(btag, bhandle,  ISAC_REG_OFFSET, buf, size);
242                         break;
243                 case ISIC_WHAT_HSCXA:
244                         hscx_read_fifo(0, buf, size, sc);
245                         break;
246                 case ISIC_WHAT_HSCXB:
247                         hscx_read_fifo(1, buf, size, sc);
248                         break;
249         }
250 }
251
252 static void
253 hscx_read_fifo(int chan, void *buf, size_t len, struct l1_softc *sc)
254 {
255         u_int8_t *ip;
256         size_t cnt;
257         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
258         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
259
260         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
261         ip = (u_int8_t *)buf;
262         cnt = 0;
263         while (cnt++ < len)
264         {
265                 *ip++ = bus_space_read_1(btag, bhandle, ISAC_REG_OFFSET);
266         }
267 }
268
269 /*---------------------------------------------------------------------------*
270  *      AVM write fifo routines
271  *---------------------------------------------------------------------------*/
272 static void
273 avm_pnp_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
274 {
275         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
276         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
277
278         switch (what) {
279                 case ISIC_WHAT_ISAC:
280                         bus_space_write_1(btag, bhandle,  ADDR_REG_OFFSET, ISAC_FIFO);
281                         bus_space_write_multi_1(btag, bhandle,  ISAC_REG_OFFSET, (u_int8_t*)buf, size);
282                         break;
283                 case ISIC_WHAT_HSCXA:
284                         hscx_write_fifo(0, buf, size, sc);
285                         break;
286                 case ISIC_WHAT_HSCXB:
287                         hscx_write_fifo(1, buf, size, sc);
288                         break;
289         }
290 }
291
292 static void
293 hscx_write_fifo(int chan, void *buf, size_t len, struct l1_softc *sc)
294 {
295         u_int8_t *ip;
296         size_t cnt;
297         l1_bchan_state_t *Bchan = &sc->sc_chan[chan];
298         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
299         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
300
301         sc->avma1pp_cmd &= ~HSCX_CMD_XME;
302         sc->avma1pp_txl = 0;
303
304         if (Bchan->out_mbuf_cur == NULL)
305         {
306           if (Bchan->bprot != BPROT_NONE)
307                  sc->avma1pp_cmd |= HSCX_CMD_XME;
308         }
309         if (len != sc->sc_bfifolen)
310                 sc->avma1pp_txl = len;
311         
312         hscx_write_reg(chan, HSCX_STAT, sc, 3);
313
314         ip = (u_int8_t *)buf;
315         cnt = 0;
316         while (cnt++ < len)
317         {
318                 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET, *ip++);
319         }
320 }
321
322 /*---------------------------------------------------------------------------*
323  *      AVM write register routines
324  *---------------------------------------------------------------------------*/
325
326 static void
327 avm_pnp_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
328 {
329         u_char reg_bank;
330         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
331         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
332
333         switch (what) {
334                 case ISIC_WHAT_ISAC:
335                         reg_bank = (offs > MAX_LO_REG_OFFSET) ? ISAC_HI_REG_OFFSET:ISAC_LO_REG_OFFSET;
336                         /* set the register bank */
337                         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, reg_bank);
338                         bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + (offs & ISAC_REGSET_MASK), data);
339                         break;
340                 case ISIC_WHAT_HSCXA:
341                         hscx_write_reg_val(0, offs, data, sc);
342                         break;
343                 case ISIC_WHAT_HSCXB:
344                         hscx_write_reg_val(1, offs, data, sc);
345                         break;
346         }
347 }
348
349 static void
350 hscx_write_reg(int chan, u_int off, struct l1_softc *sc, u_int which)
351 {
352         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
353         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
354
355         /* point at the correct channel */
356         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
357         if (which & 4) 
358                 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off + 2, sc->avma1pp_prot);
359         if (which & 2) 
360                 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off + 1, sc->avma1pp_txl);
361         if (which & 1) 
362                 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off, sc->avma1pp_cmd);
363 }
364
365 static void
366 hscx_write_reg_val(int chan, u_int off, u_int8_t val, struct l1_softc *sc)
367 {
368         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
369         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
370
371         /* point at the correct channel */
372         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
373         bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off, val);
374 }
375
376 /*---------------------------------------------------------------------------*
377  *      AVM read register routines
378  *---------------------------------------------------------------------------*/
379 static u_int8_t
380 avm_pnp_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
381 {
382         u_char reg_bank;
383         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
384         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
385
386         switch (what) {
387                 case ISIC_WHAT_ISAC:
388                         reg_bank = (offs > MAX_LO_REG_OFFSET) ? ISAC_HI_REG_OFFSET:ISAC_LO_REG_OFFSET;
389                         /* set the register bank */
390                         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, reg_bank);
391                         return(bus_space_read_1(btag, bhandle, ISAC_REG_OFFSET +
392                                 (offs & ISAC_REGSET_MASK)));
393                 case ISIC_WHAT_HSCXA:
394                         return hscx_read_reg(0, offs, sc);
395                 case ISIC_WHAT_HSCXB:
396                         return hscx_read_reg(1, offs, sc);
397         }
398         return 0;
399 }
400
401 static u_int
402 hscx_read_reg(int chan, u_int off, struct l1_softc *sc)
403 {
404         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
405         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
406
407         /* point at the correct channel */
408         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
409         return(bus_space_read_1(btag, bhandle, ISAC_REG_OFFSET + off));
410 }
411
412
413 static struct ifpnp_ids {
414         u_long vend_id;
415         char *id_str;
416 } ifpnp_ids[] = {
417         { 0x0009cd06,           "AVM Fritz!Card PnP"            },      
418         { 0, 0 }
419 };
420
421 /*---------------------------------------------------------------------------*
422  *      avm_pnp_probe - probe for a card
423  *---------------------------------------------------------------------------*/
424 static int
425 avm_pnp_probe(dev)
426         device_t                dev;
427 {
428         struct ifpnp_ids *ids;                  /* pnp id's */
429         char *string = NULL;                            /* the name */
430         u_int32_t vend_id = isa_get_vendorid(dev);      /* vendor id */
431
432         /* search table of knowd id's */
433         
434         for(ids = ifpnp_ids; ids->vend_id != 0; ids++)
435         {
436                 if(vend_id == ids->vend_id)
437                 {
438                         string = ids->id_str;
439                         break;
440                 }
441         }
442         
443         if(string)              /* set name if we have one */
444         {
445                 device_set_desc(dev, string);   /* set description */
446                 return 0;
447         }
448         else
449         {
450                 return ENXIO;
451         }
452 }
453
454 /*---------------------------------------------------------------------------*
455  *      avm_pnp_attach - attach Fritz!Card PnP
456  *---------------------------------------------------------------------------*/
457 int
458 avm_pnp_attach(device_t dev)
459 {
460         struct l1_softc *sc;
461         u_int v;
462         int unit, error = 0;
463         int s;
464         u_int16_t vid;
465         void *ih = 0;
466         bus_space_handle_t bhandle;
467         bus_space_tag_t btag; 
468
469         s = splimp();
470
471         vid = isa_get_vendorid(dev);
472         sc = device_get_softc(dev);
473         unit = device_get_unit(dev);
474         bzero(sc, sizeof(struct l1_softc));
475
476         /* probably not really required */
477         if(unit > IFPNP_MAXUNIT) {
478                 printf("avm_pnp%d: Error, unit > IFPNP_MAXUNIT!\n", unit);
479                 splx(s);
480                 return(ENXIO);
481         }
482
483         ifpnp_scp[unit] = sc;
484
485
486         /* get io_base */
487         if(!(sc->sc_resources.io_base[0] =
488                         bus_alloc_resource(dev, SYS_RES_IOPORT,
489                                                 &sc->sc_resources.io_rid[0],
490                                                 0UL, ~0UL, 1, RF_ACTIVE ) ))
491         {
492                 printf("avm_pnp_attach: Couldn't get my io_base.\n");
493                 return ENXIO;                                       
494         }
495         if (sc->sc_resources.io_base[0] == NULL) {
496                 printf("avm_pnp%d: couldn't map IO port\n", unit);
497                 error = ENXIO;
498                 goto fail;
499         }
500
501         bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
502         btag = rman_get_bustag(sc->sc_resources.io_base[0]);
503
504         /* will not be used for pnp devices */
505         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
506
507         /* get irq, release io_base if we don't get it */
508
509         if(!(sc->sc_resources.irq =
510                         bus_alloc_resource(dev, SYS_RES_IRQ,
511                                            &sc->sc_resources.irq_rid,
512                                            0UL, ~0UL, 1, RF_ACTIVE)))
513         {
514                 printf("avm_pnp%d: Could not get irq.\n",unit);
515                 error = ENXIO;                                       
516                 goto fail;
517         }
518
519         /* not needed */
520         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
521         bus_setup_intr(dev,sc->sc_resources.irq,INTR_TYPE_NET,
522                                 (void(*)(void*))avm_pnp_intr, sc,&ih);
523         sc->sc_unit = unit;
524
525         /* end of new-bus stuff */
526
527         ISAC_BASE = (caddr_t)ISIC_WHAT_ISAC;
528
529         HSCX_A_BASE = (caddr_t)ISIC_WHAT_HSCXA;
530         HSCX_B_BASE = (caddr_t)ISIC_WHAT_HSCXB;
531
532         /* setup access routines */
533
534         sc->clearirq = NULL;
535         sc->readreg = avm_pnp_read_reg;
536         sc->writereg = avm_pnp_write_reg;
537
538         sc->readfifo = avm_pnp_read_fifo;
539         sc->writefifo = avm_pnp_write_fifo;
540
541         /* setup card type */
542         
543         sc->sc_cardtyp = CARD_TYPEP_AVM_PNP;
544
545         /* setup IOM bus type */
546         
547         sc->sc_bustyp = BUS_TYPE_IOM2;
548
549         /* set up some other miscellaneous things */
550         sc->sc_ipac = 0;
551         sc->sc_bfifolen = HSCX_FIFO_LEN;
552
553         /* reset the card */
554         /* the Linux driver does this to clear any pending ISAC interrupts */
555         v = 0;
556         v = ISAC_READ(I_STAR);
557         v = ISAC_READ(I_MODE);
558         v = ISAC_READ(I_ADF2);
559         v = ISAC_READ(I_ISTA);
560         if (v & ISAC_ISTA_EXI)
561         {
562                  v = ISAC_READ(I_EXIR);
563         }
564         v = ISAC_READ(I_CIRR);
565         ISAC_WRITE(I_MASK, 0xff);
566         /* the Linux driver does this to clear any pending HSCX interrupts */
567         v = hscx_read_reg(0, HSCX_STAT, sc);
568         v = hscx_read_reg(1, HSCX_STAT, sc);
569
570         bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_RESET_ALL|ASL_TIMERDISABLE);
571         DELAY(SEC_DELAY/100); /* 10 ms */
572         bus_space_write_1(btag, bhandle, STAT1_OFFSET, ASL1_ENABLE_IOM|sc->sc_irq);
573         DELAY(SEC_DELAY/100); /* 10 ms */
574         bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_TIMERRESET|ASL_ENABLE_INT|ASL_TIMERDISABLE);
575         DELAY(SEC_DELAY/100); /* 10 ms */
576
577          printf("ifpnp%d: AVM Fritz!Card PnP Class %#x Revision %d \n", unit,
578                         bus_space_read_1(btag, bhandle, CLASS_OFFSET),
579                         bus_space_read_1(btag, bhandle, REVISION_OFFSET));
580
581          printf("ifpnp%d: ISAC %s (IOM-%c)\n", unit,
582                 "2085 Version A1/A2 or 2086/2186 Version 1.1",
583                  sc->sc_bustyp == BUS_TYPE_IOM1 ? '1' : '2');
584
585
586         /* init the ISAC */
587         ifpnp_isac_init(sc);
588
589         /* init the "HSCX" */
590         avm_pnp_bchannel_setup(sc->sc_unit, HSCX_CH_A, BPROT_NONE, 0);
591         
592         avm_pnp_bchannel_setup(sc->sc_unit, HSCX_CH_B, BPROT_NONE, 0);
593
594         /* can't use the normal B-Channel stuff */
595         avm_pnp_init_linktab(sc);
596
597         /* set trace level */
598
599         sc->sc_trace = TRACE_OFF;
600
601         sc->sc_state = ISAC_IDLE;
602
603         sc->sc_ibuf = NULL;
604         sc->sc_ib = NULL;
605         sc->sc_ilen = 0;
606
607         sc->sc_obuf = NULL;
608         sc->sc_op = NULL;
609         sc->sc_ol = 0;
610         sc->sc_freeflag = 0;
611
612         sc->sc_obuf2 = NULL;
613         sc->sc_freeflag2 = 0;
614
615         callout_handle_init(&sc->sc_T3_callout);
616         callout_handle_init(&sc->sc_T4_callout);        
617         
618         /* init higher protocol layers */
619         
620         i4b_l1_mph_status_ind(L0IFPNPUNIT(sc->sc_unit), STI_ATTACH, sc->sc_cardtyp, &avm_pnp_l1mux_func);
621
622   fail:
623         splx(s);
624         return(error);
625 }
626
627 /*
628  * this is the real interrupt routine
629  */
630 static void
631 avm_pnp_hscx_intr(int h_chan, u_int stat, u_int cnt, struct l1_softc *sc)
632 {
633         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
634         int activity = -1;
635         
636         NDBGL1(L1_H_IRQ, "%#x", stat);
637
638         if((stat & HSCX_INT_XDU) && (chan->bprot != BPROT_NONE))/* xmit data underrun */
639         {
640                 chan->stat_XDU++;                       
641                 NDBGL1(L1_H_XFRERR, "xmit data underrun");
642                 /* abort the transmission */
643                 sc->avma1pp_txl = 0;
644                 sc->avma1pp_cmd |= HSCX_CMD_XRS;
645                 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
646                 sc->avma1pp_cmd &= ~HSCX_CMD_XRS;
647                 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
648
649                 if (chan->out_mbuf_head != NULL)  /* don't continue to transmit this buffer */
650                 {
651                         i4b_Bfreembuf(chan->out_mbuf_head);
652                         chan->out_mbuf_cur = chan->out_mbuf_head = NULL;
653                 }
654         }
655
656         /*
657          * The following is based on examination of the Linux driver.
658          *
659          * The logic here is different than with a "real" HSCX; all kinds
660          * of information (interrupt/status bits) are in stat.
661          *              HSCX_INT_RPR indicates a receive interrupt
662          *                      HSCX_STAT_RDO indicates an overrun condition, abort -
663          *                      otherwise read the bytes ((stat & HSCX_STZT_RML_MASK) >> 8)
664          *                      HSCX_STAT_RME indicates end-of-frame and apparently any
665          *                      CRC/framing errors are only reported in this state.
666          *                              if ((stat & HSCX_STAT_CRCVFRRAB) != HSCX_STAT_CRCVFR)
667          *                                      CRC/framing error
668          */
669         
670         if(stat & HSCX_INT_RPR)
671         {
672                 int fifo_data_len;
673                 int error = 0;
674                 /* always have to read the FIFO, so use a scratch buffer */
675                 u_char scrbuf[HSCX_FIFO_LEN];
676
677                 if(stat & HSCX_STAT_RDO)
678                 {
679                         chan->stat_RDO++;
680                         NDBGL1(L1_H_XFRERR, "receive data overflow");
681                         error++;                                
682                 }
683
684                 /*
685                  * check whether we're receiving data for an inactive B-channel
686                  * and discard it. This appears to happen for telephony when
687                  * both B-channels are active and one is deactivated. Since
688                  * it is not really possible to deactivate the channel in that
689                  * case (the ASIC seems to deactivate _both_ channels), the
690                  * "deactivated" channel keeps receiving data which can lead
691                  * to exhaustion of mbufs and a kernel panic.
692                  *
693                  * This is a hack, but it's the only solution I can think of
694                  * without having the documentation for the ASIC.
695                  * GJ - 28 Nov 1999
696                  */
697                  if (chan->state == HSCX_IDLE)
698                  {
699                         NDBGL1(L1_H_XFRERR, "toss data from %d", h_chan);
700                         error++;
701                  }
702
703                 fifo_data_len = cnt;
704                 
705                 if(fifo_data_len == 0)
706                         fifo_data_len = sc->sc_bfifolen;
707
708                 /* ALWAYS read data from HSCX fifo */
709         
710                 HSCX_RDFIFO(h_chan, scrbuf, fifo_data_len);
711                 chan->rxcount += fifo_data_len;
712
713                 /* all error conditions checked, now decide and take action */
714                 
715                 if(error == 0)
716                 {
717                         if(chan->in_mbuf == NULL)
718                         {
719                                 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
720                                         panic("L1 avm_pnp_hscx_intr: RME, cannot allocate mbuf!\n");
721                                 chan->in_cbptr = chan->in_mbuf->m_data;
722                                 chan->in_len = 0;
723                         }
724
725                         if((chan->in_len + fifo_data_len) <= BCH_MAX_DATALEN)
726                         {
727                                 /* OK to copy the data */
728                                 bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
729                                 chan->in_cbptr += fifo_data_len;
730                                 chan->in_len += fifo_data_len;
731
732                                 /* setup mbuf data length */
733                                         
734                                 chan->in_mbuf->m_len = chan->in_len;
735                                 chan->in_mbuf->m_pkthdr.len = chan->in_len;
736
737                                 if(sc->sc_trace & TRACE_B_RX)
738                                 {
739                                         i4b_trace_hdr_t hdr;
740                                         hdr.unit = L0IFPNPUNIT(sc->sc_unit);
741                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
742                                         hdr.dir = FROM_NT;
743                                         hdr.count = ++sc->sc_trace_bcount;
744                                         MICROTIME(hdr.time);
745                                         i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
746                                 }
747
748                                 if (stat & HSCX_STAT_RME)
749                                 {
750                                   if((stat & HSCX_STAT_CRCVFRRAB) == HSCX_STAT_CRCVFR)
751                                   {
752                                          (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
753                                          activity = ACT_RX;
754                                 
755                                          /* mark buffer ptr as unused */
756                                         
757                                          chan->in_mbuf = NULL;
758                                          chan->in_cbptr = NULL;
759                                          chan->in_len = 0;
760                                   }
761                                   else
762                                   {
763                                                 chan->stat_CRC++;
764                                                 NDBGL1(L1_H_XFRERR, "CRC/RAB");
765                                           if (chan->in_mbuf != NULL)
766                                           {
767                                                   i4b_Bfreembuf(chan->in_mbuf);
768                                                   chan->in_mbuf = NULL;
769                                                   chan->in_cbptr = NULL;
770                                                   chan->in_len = 0;
771                                           }
772                                   }
773                                 }
774                         } /* END enough space in mbuf */
775                         else
776                         {
777                                  if(chan->bprot == BPROT_NONE)
778                                  {
779                                           /* setup mbuf data length */
780                                 
781                                           chan->in_mbuf->m_len = chan->in_len;
782                                           chan->in_mbuf->m_pkthdr.len = chan->in_len;
783
784                                           if(sc->sc_trace & TRACE_B_RX)
785                                           {
786                                                         i4b_trace_hdr_t hdr;
787                                                         hdr.unit = L0IFPNPUNIT(sc->sc_unit);
788                                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
789                                                         hdr.dir = FROM_NT;
790                                                         hdr.count = ++sc->sc_trace_bcount;
791                                                         MICROTIME(hdr.time);
792                                                         i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
793                                                 }
794
795                                           if(!(i4b_l1_bchan_tel_silence(chan->in_mbuf->m_data, chan->in_mbuf->m_len)))
796                                                  activity = ACT_RX;
797                                 
798                                           /* move rx'd data to rx queue */
799 #if defined (__FreeBSD__) && __FreeBSD__ > 4
800                                           (void) IF_HANDOFF(&chan->rx_queue, chan->in_mbuf, NULL);
801 #else
802                                           if(!(IF_QFULL(&chan->rx_queue)))
803                                           {
804                                                 IF_ENQUEUE(&chan->rx_queue, chan->in_mbuf);
805                                           }
806                                           else
807                                           {
808                                                 i4b_Bfreembuf(chan->in_mbuf);
809                                           }
810 #endif
811                                           /* signal upper layer that data are available */
812                                           (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
813
814                                           /* alloc new buffer */
815                                 
816                                           if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
817                                                  panic("L1 avm_pnp_hscx_intr: RPF, cannot allocate new mbuf!\n");
818         
819                                           /* setup new data ptr */
820                                 
821                                           chan->in_cbptr = chan->in_mbuf->m_data;
822         
823                                           /* OK to copy the data */
824                                           bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
825
826                                           chan->in_cbptr += fifo_data_len;
827                                           chan->in_len = fifo_data_len;
828
829                                           chan->rxcount += fifo_data_len;
830                                         }
831                                  else
832                                         {
833                                           NDBGL1(L1_H_XFRERR, "RAWHDLC rx buffer overflow in RPF, in_len=%d", chan->in_len);
834                                           chan->in_cbptr = chan->in_mbuf->m_data;
835                                           chan->in_len = 0;
836                                         }
837                           }
838                 } /* if(error == 0) */
839                 else
840                 {
841                         /* land here for RDO */
842                         if (chan->in_mbuf != NULL)
843                         {
844                                 i4b_Bfreembuf(chan->in_mbuf);
845                                 chan->in_mbuf = NULL;
846                                 chan->in_cbptr = NULL;
847                                 chan->in_len = 0;
848                         }
849                         sc->avma1pp_txl = 0;
850                         sc->avma1pp_cmd |= HSCX_CMD_RRS;
851                         hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
852                         sc->avma1pp_cmd &= ~HSCX_CMD_RRS;
853                         hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
854                 }
855         }
856
857
858         /* transmit fifo empty, new data can be written to fifo */
859         
860         if(stat & HSCX_INT_XPR)
861         {
862                 /*
863                  * for a description what is going on here, please have
864                  * a look at isic_bchannel_start() in i4b_bchan.c !
865                  */
866
867                 NDBGL1(L1_H_IRQ, "unit %d, chan %d - XPR, Tx Fifo Empty!", sc->sc_unit, h_chan);
868
869                 if(chan->out_mbuf_cur == NULL)  /* last frame is transmitted */
870                 {
871                         IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
872
873                         if(chan->out_mbuf_head == NULL)
874                         {
875                                 chan->state &= ~HSCX_TX_ACTIVE;
876                                 (*chan->isic_drvr_linktab->bch_tx_queue_empty)(chan->isic_drvr_linktab->unit);
877                         }
878                         else
879                         {
880                                 chan->state |= HSCX_TX_ACTIVE;
881                                 chan->out_mbuf_cur = chan->out_mbuf_head;
882                                 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
883                                 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
884
885                                 if(sc->sc_trace & TRACE_B_TX)
886                                 {
887                                         i4b_trace_hdr_t hdr;
888                                         hdr.unit = L0IFPNPUNIT(sc->sc_unit);
889                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
890                                         hdr.dir = FROM_TE;
891                                         hdr.count = ++sc->sc_trace_bcount;
892                                         MICROTIME(hdr.time);
893                                         i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
894                                 }
895                                 
896                                 if(chan->bprot == BPROT_NONE)
897                                 {
898                                         if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
899                                                 activity = ACT_TX;
900                                 }
901                                 else
902                                 {
903                                         activity = ACT_TX;
904                                 }
905                         }
906                 }
907                         
908                 avm_pnp_hscx_fifo(chan, sc);
909         }
910
911         /* call timeout handling routine */
912         
913         if(activity == ACT_RX || activity == ACT_TX)
914                 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
915 }
916
917 /*
918  * this is the main routine which checks each channel and then calls
919  * the real interrupt routine as appropriate
920  */
921 static void
922 avm_pnp_hscx_int_handler(struct l1_softc *sc)
923 {
924         u_char stat = 0;
925         u_char cnt = 0;
926
927         stat = hscx_read_reg(0, HSCX_STAT, sc);
928         if (stat & HSCX_INT_RPR)
929           cnt = hscx_read_reg(0, HSCX_STAT+1, sc);
930         if (stat & HSCX_INT_MASK)
931           avm_pnp_hscx_intr(0, stat, cnt, sc);
932
933         cnt = 0;
934         stat = hscx_read_reg(1, HSCX_STAT, sc);
935         if (stat & HSCX_INT_RPR)
936           cnt = hscx_read_reg(1, HSCX_STAT+1, sc);
937         if (stat & HSCX_INT_MASK)
938           avm_pnp_hscx_intr(1, stat, cnt, sc);
939 }
940
941 static void
942 avm_pnp_intr(void *xsc)
943 {
944         u_char stat;
945         struct l1_softc *sc;
946         bus_space_handle_t bhandle;
947         bus_space_tag_t btag; 
948
949         sc = xsc;
950         bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
951         btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
952
953         stat = bus_space_read_1(btag, bhandle, STAT0_OFFSET);
954         NDBGL1(L1_H_IRQ, "stat %x", stat);
955         /* was there an interrupt from this card ? */
956         if ((stat & ASL_IRQ_Pending) == ASL_IRQ_Pending)
957                 return; /* no */
958         /* interrupts are low active */
959         if (!(stat & ASL_IRQ_TIMER))
960           NDBGL1(L1_H_IRQ, "timer interrupt ???");
961         if (!(stat & ASL_IRQ_HSCX))
962         {
963           NDBGL1(L1_H_IRQ, "HSCX");
964                 avm_pnp_hscx_int_handler(sc);
965         }
966         if (!(stat & ASL_IRQ_ISAC))
967         {
968           NDBGL1(L1_H_IRQ, "ISAC");
969                 ifpnp_isac_intr(sc);
970         }
971 }
972
973 static void
974 avm_pnp_hscx_init(struct l1_softc *sc, int h_chan, int activate)
975 {
976         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
977
978         NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
979                 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
980
981         if (activate == 0)
982         {
983                 /* only deactivate if both channels are idle */
984                 if (sc->sc_chan[HSCX_CH_A].state != HSCX_IDLE ||
985                         sc->sc_chan[HSCX_CH_B].state != HSCX_IDLE)
986                 {
987                         return;
988                 }
989                 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
990                 sc->avma1pp_prot = HSCX_MODE_TRANS;
991                 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
992                 return;
993         }
994         if(chan->bprot == BPROT_RHDLC)
995         {
996                   NDBGL1(L1_BCHAN, "BPROT_RHDLC");
997
998                 /* HDLC Frames, transparent mode 0 */
999                 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1000                 sc->avma1pp_prot = HSCX_MODE_ITF_FLG;
1001                 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
1002                 sc->avma1pp_cmd = HSCX_CMD_XRS;
1003                 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
1004                 sc->avma1pp_cmd = 0;
1005         }
1006         else
1007         {
1008                   NDBGL1(L1_BCHAN, "BPROT_NONE??");
1009
1010                 /* Raw Telephony, extended transparent mode 1 */
1011                 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1012                 sc->avma1pp_prot = HSCX_MODE_TRANS;
1013                 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
1014                 sc->avma1pp_cmd = HSCX_CMD_XRS;
1015                 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
1016                 sc->avma1pp_cmd = 0;
1017         }
1018 }
1019
1020 static void
1021 avm_pnp_bchannel_setup(int unit, int h_chan, int bprot, int activate)
1022 {
1023         struct l1_softc *sc = ifpnp_scp[unit];
1024         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1025
1026         int s = SPLI4B();
1027         
1028         if(activate == 0)
1029         {
1030                 /* deactivation */
1031                 chan->state = HSCX_IDLE;
1032                 avm_pnp_hscx_init(sc, h_chan, activate);
1033         }
1034                 
1035         NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
1036                 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
1037
1038         /* general part */
1039
1040         chan->unit = sc->sc_unit;       /* unit number */
1041         chan->channel = h_chan;         /* B channel */
1042         chan->bprot = bprot;            /* B channel protocol */
1043         chan->state = HSCX_IDLE;        /* B channel state */
1044
1045         /* receiver part */
1046
1047         chan->rx_queue.ifq_maxlen = IFQ_MAXLEN;
1048
1049 #if defined (__FreeBSD__) && __FreeBSD__ > 4
1050         mtx_init(&chan->rx_queue.ifq_mtx, "i4b_avm_pnp_rx", MTX_DEF);
1051 #endif
1052
1053         i4b_Bcleanifq(&chan->rx_queue); /* clean rx queue */
1054
1055         chan->rxcount = 0;              /* reset rx counter */
1056         
1057         i4b_Bfreembuf(chan->in_mbuf);   /* clean rx mbuf */
1058
1059         chan->in_mbuf = NULL;           /* reset mbuf ptr */
1060         chan->in_cbptr = NULL;          /* reset mbuf curr ptr */
1061         chan->in_len = 0;               /* reset mbuf data len */
1062         
1063         /* transmitter part */
1064
1065         chan->tx_queue.ifq_maxlen = IFQ_MAXLEN;
1066
1067 #if defined (__FreeBSD__) && __FreeBSD__ > 4
1068         mtx_init(&chan->tx_queue.ifq_mtx, "i4b_avm_pnp_tx", MTX_DEF);
1069 #endif
1070         i4b_Bcleanifq(&chan->tx_queue); /* clean tx queue */
1071
1072         chan->txcount = 0;              /* reset tx counter */
1073         
1074         i4b_Bfreembuf(chan->out_mbuf_head);     /* clean tx mbuf */
1075
1076         chan->out_mbuf_head = NULL;     /* reset head mbuf ptr */
1077         chan->out_mbuf_cur = NULL;      /* reset current mbuf ptr */    
1078         chan->out_mbuf_cur_ptr = NULL;  /* reset current mbuf data ptr */
1079         chan->out_mbuf_cur_len = 0;     /* reset current mbuf data cnt */
1080         
1081         if(activate != 0)
1082         {
1083                 /* activation */
1084                 avm_pnp_hscx_init(sc, h_chan, activate);
1085                 chan->state |= HSCX_AVMA1PP_ACTIVE;
1086         }
1087
1088         splx(s);
1089 }
1090
1091 static void
1092 avm_pnp_bchannel_start(int unit, int h_chan)
1093 {
1094         struct l1_softc *sc = ifpnp_scp[unit];
1095         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1096         int s;
1097         int activity = -1;
1098
1099         s = SPLI4B();                           /* enter critical section */
1100         if(chan->state & HSCX_TX_ACTIVE)        /* already running ? */
1101         {
1102                 splx(s);
1103                 return;                         /* yes, leave */
1104         }
1105
1106         /* get next mbuf from queue */
1107         
1108         IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
1109         
1110         if(chan->out_mbuf_head == NULL)         /* queue empty ? */
1111         {
1112                 splx(s);                        /* leave critical section */
1113                 return;                         /* yes, exit */
1114         }
1115
1116         /* init current mbuf values */
1117         
1118         chan->out_mbuf_cur = chan->out_mbuf_head;
1119         chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1120         chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;    
1121         
1122         /* activity indicator for timeout handling */
1123
1124         if(chan->bprot == BPROT_NONE)
1125         {
1126                 if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
1127                         activity = ACT_TX;
1128         }
1129         else
1130         {
1131                 activity = ACT_TX;
1132         }
1133
1134         chan->state |= HSCX_TX_ACTIVE;          /* we start transmitting */
1135         
1136         if(sc->sc_trace & TRACE_B_TX)   /* if trace, send mbuf to trace dev */
1137         {
1138                 i4b_trace_hdr_t hdr;
1139                 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
1140                 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1141                 hdr.dir = FROM_TE;
1142                 hdr.count = ++sc->sc_trace_bcount;
1143                 MICROTIME(hdr.time);
1144                 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1145         }                       
1146
1147         avm_pnp_hscx_fifo(chan, sc);
1148
1149         /* call timeout handling routine */
1150         
1151         if(activity == ACT_RX || activity == ACT_TX)
1152                 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
1153
1154         splx(s);        
1155 }
1156
1157 /*---------------------------------------------------------------------------*
1158  *      return the address of isic drivers linktab      
1159  *---------------------------------------------------------------------------*/
1160 static isdn_link_t *
1161 avm_pnp_ret_linktab(int unit, int channel)
1162 {
1163         struct l1_softc *sc = ifpnp_scp[unit];
1164         l1_bchan_state_t *chan = &sc->sc_chan[channel];
1165
1166         return(&chan->isic_isdn_linktab);
1167 }
1168  
1169 /*---------------------------------------------------------------------------*
1170  *      set the driver linktab in the b channel softc
1171  *---------------------------------------------------------------------------*/
1172 static void
1173 avm_pnp_set_linktab(int unit, int channel, drvr_link_t *dlt)
1174 {
1175         struct l1_softc *sc = ifpnp_scp[unit];
1176         l1_bchan_state_t *chan = &sc->sc_chan[channel];
1177
1178         chan->isic_drvr_linktab = dlt;
1179 }
1180
1181
1182 /*---------------------------------------------------------------------------*
1183  *      initialize our local linktab
1184  *---------------------------------------------------------------------------*/
1185 static void
1186 avm_pnp_init_linktab(struct l1_softc *sc)
1187 {
1188         l1_bchan_state_t *chan = &sc->sc_chan[HSCX_CH_A];
1189         isdn_link_t *lt = &chan->isic_isdn_linktab;
1190
1191         /* make sure the hardware driver is known to layer 4 */
1192         /* avoid overwriting if already set */
1193         if (ctrl_types[CTRL_PASSIVE].set_linktab == NULL)
1194         {
1195                 ctrl_types[CTRL_PASSIVE].set_linktab = avm_pnp_set_linktab;
1196                 ctrl_types[CTRL_PASSIVE].get_linktab = avm_pnp_ret_linktab;
1197         }
1198
1199         /* local setup */
1200         lt->unit = sc->sc_unit;
1201         lt->channel = HSCX_CH_A;
1202         lt->bch_config = avm_pnp_bchannel_setup;
1203         lt->bch_tx_start = avm_pnp_bchannel_start;
1204         lt->bch_stat = avm_pnp_bchannel_stat;
1205         lt->tx_queue = &chan->tx_queue;
1206
1207         /* used by non-HDLC data transfers, i.e. telephony drivers */
1208         lt->rx_queue = &chan->rx_queue;
1209
1210         /* used by HDLC data transfers, i.e. ipr and isp drivers */     
1211         lt->rx_mbuf = &chan->in_mbuf;   
1212                                                 
1213         chan = &sc->sc_chan[HSCX_CH_B];
1214         lt = &chan->isic_isdn_linktab;
1215
1216         lt->unit = sc->sc_unit;
1217         lt->channel = HSCX_CH_B;
1218         lt->bch_config = avm_pnp_bchannel_setup;
1219         lt->bch_tx_start = avm_pnp_bchannel_start;
1220         lt->bch_stat = avm_pnp_bchannel_stat;
1221         lt->tx_queue = &chan->tx_queue;
1222
1223         /* used by non-HDLC data transfers, i.e. telephony drivers */
1224         lt->rx_queue = &chan->rx_queue;
1225
1226         /* used by HDLC data transfers, i.e. ipr and isp drivers */     
1227         lt->rx_mbuf = &chan->in_mbuf;   
1228 }
1229
1230 /*
1231  * use this instead of isic_bchannel_stat in i4b_bchan.c because it's static
1232  */
1233 static void
1234 avm_pnp_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp)
1235 {
1236         struct l1_softc *sc = ifpnp_scp[unit];
1237         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1238         int s;
1239
1240         s = SPLI4B();
1241         
1242         bsp->outbytes = chan->txcount;
1243         bsp->inbytes = chan->rxcount;
1244
1245         chan->txcount = 0;
1246         chan->rxcount = 0;
1247
1248         splx(s);
1249 }
1250
1251 /*---------------------------------------------------------------------------*
1252  *      fill HSCX fifo with data from the current mbuf
1253  *      Put this here until it can go into i4b_hscx.c
1254  *---------------------------------------------------------------------------*/
1255 static int
1256 avm_pnp_hscx_fifo(l1_bchan_state_t *chan, struct l1_softc *sc)
1257 {
1258         int len;
1259         int nextlen;
1260         int i;
1261         int cmd = 0;
1262         /* using a scratch buffer simplifies writing to the FIFO */
1263         u_char scrbuf[HSCX_FIFO_LEN];
1264
1265         len = 0;
1266
1267         /*
1268          * fill the HSCX tx fifo with data from the current mbuf. if
1269          * current mbuf holds less data than HSCX fifo length, try to
1270          * get the next mbuf from (a possible) mbuf chain. if there is
1271          * not enough data in a single mbuf or in a chain, then this
1272          * is the last mbuf and we tell the HSCX that it has to send
1273          * CRC and closing flag
1274          */
1275          
1276         while(chan->out_mbuf_cur && len != sc->sc_bfifolen)
1277         {
1278                 nextlen = min(chan->out_mbuf_cur_len, sc->sc_bfifolen - len);
1279
1280 #ifdef NOTDEF
1281                 printf("i:mh=%p, mc=%p, mcp=%p, mcl=%d l=%d nl=%d # ",
1282                         chan->out_mbuf_head,
1283                         chan->out_mbuf_cur,                     
1284                         chan->out_mbuf_cur_ptr,
1285                         chan->out_mbuf_cur_len,
1286                         len,
1287                         nextlen);
1288 #endif
1289
1290                 cmd |= HSCX_CMDR_XTF;
1291                 /* collect the data in the scratch buffer */
1292                 for (i = 0; i < nextlen; i++)
1293                         scrbuf[i + len] = chan->out_mbuf_cur_ptr[i];
1294
1295                 len += nextlen;
1296                 chan->txcount += nextlen;
1297         
1298                 chan->out_mbuf_cur_ptr += nextlen;
1299                 chan->out_mbuf_cur_len -= nextlen;
1300                         
1301                 if(chan->out_mbuf_cur_len == 0) 
1302                 {
1303                         if((chan->out_mbuf_cur = chan->out_mbuf_cur->m_next) != NULL)
1304                         {
1305                                 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
1306                                 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1307         
1308                                 if(sc->sc_trace & TRACE_B_TX)
1309                                 {
1310                                         i4b_trace_hdr_t hdr;
1311                                         hdr.unit = L0IFPNPUNIT(sc->sc_unit);
1312                                         hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1313                                         hdr.dir = FROM_TE;
1314                                         hdr.count = ++sc->sc_trace_bcount;
1315                                         MICROTIME(hdr.time);
1316                                         i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1317                                 }
1318                         }
1319                         else
1320                         {
1321                                 if (chan->bprot != BPROT_NONE)
1322                                         cmd |= HSCX_CMDR_XME;
1323                                 i4b_Bfreembuf(chan->out_mbuf_head);
1324                                 chan->out_mbuf_head = NULL;
1325                         }
1326                 }
1327         }
1328         /* write what we have from the scratch buf to the HSCX fifo */
1329         if (len != 0)
1330                 HSCX_WRFIFO(chan->channel, scrbuf, len);
1331         return(cmd);
1332 }
1333
1334 /*---------------------------------------------------------------------------*
1335  *      ifpnp - ISAC interrupt routine
1336  *---------------------------------------------------------------------------*/
1337 static void
1338 ifpnp_isac_intr(struct l1_softc *sc)
1339 {
1340         u_char isac_irq_stat;
1341
1342         for(;;)
1343         {
1344                 /* get isac irq status */
1345                 isac_irq_stat = ISAC_READ(I_ISTA);
1346
1347                 if(isac_irq_stat)
1348                         ifpnp_isac_irq(sc, isac_irq_stat); /* isac handler */
1349                 else
1350                         break;
1351         }
1352
1353         ISAC_WRITE(I_MASK, 0xff);
1354
1355         DELAY(100);
1356
1357         ISAC_WRITE(I_MASK, ISAC_IMASK);
1358 }
1359
1360 /*---------------------------------------------------------------------------*
1361  *      ifpnp_recover - try to recover from irq lockup
1362  *---------------------------------------------------------------------------*/
1363 void
1364 ifpnp_recover(struct l1_softc *sc)
1365 {
1366         u_char byte;
1367         
1368         /* get isac irq status */
1369
1370         byte = ISAC_READ(I_ISTA);
1371
1372         NDBGL1(L1_ERROR, "  ISAC: ISTA = 0x%x", byte);
1373         
1374         if(byte & ISAC_ISTA_EXI)
1375                 NDBGL1(L1_ERROR, "  ISAC: EXIR = 0x%x", (u_char)ISAC_READ(I_EXIR));
1376
1377         if(byte & ISAC_ISTA_CISQ)
1378         {
1379                 byte = ISAC_READ(I_CIRR);
1380         
1381                 NDBGL1(L1_ERROR, "  ISAC: CISQ = 0x%x", byte);
1382                 
1383                 if(byte & ISAC_CIRR_SQC)
1384                         NDBGL1(L1_ERROR, "  ISAC: SQRR = 0x%x", (u_char)ISAC_READ(I_SQRR));
1385         }
1386
1387         NDBGL1(L1_ERROR, "  ISAC: IMASK = 0x%x", ISAC_IMASK);
1388
1389         ISAC_WRITE(I_MASK, 0xff);       
1390         DELAY(100);
1391         ISAC_WRITE(I_MASK, ISAC_IMASK);
1392 }
1393
1394 #endif /* NIFPNP > 0 */