Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[linux-2.6] / drivers / isdn / hisax / hisax_fcpcipnp.c
1 /*
2  * Driver for AVM Fritz!PCI, Fritz!PCI v2, Fritz!PnP ISDN cards
3  *
4  * Author       Kai Germaschewski
5  * Copyright    2001 by Kai Germaschewski  <kai.germaschewski@gmx.de>
6  *              2001 by Karsten Keil       <keil@isdn4linux.de>
7  * 
8  * based upon Karsten Keil's original avm_pci.c driver
9  *
10  * This software may be used and distributed according to the terms
11  * of the GNU General Public License, incorporated herein by reference.
12  *
13  * Thanks to Wizard Computersysteme GmbH, Bremervoerde and
14  *           SoHaNet Technology GmbH, Berlin
15  * for supporting the development of this driver
16  */
17
18
19 /* TODO:
20  *
21  * o POWER PC
22  * o clean up debugging
23  * o tx_skb at PH_DEACTIVATE time
24  */
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
29 #include <linux/isapnp.h>
30 #include <linux/kmod.h>
31 #include <linux/slab.h>
32 #include <linux/skbuff.h>
33 #include <linux/netdevice.h>
34 #include <linux/delay.h>
35
36 #include <asm/io.h>
37
38 #include "hisax_fcpcipnp.h"
39
40 // debugging cruft
41 #define __debug_variable debug
42 #include "hisax_debug.h"
43
44 #ifdef CONFIG_HISAX_DEBUG
45 static int debug = 0;
46 /* static int hdlcfifosize = 32; */
47 module_param(debug, int, 0);
48 /* module_param(hdlcfifosize, int, 0); */
49 #endif
50
51 MODULE_AUTHOR("Kai Germaschewski <kai.germaschewski@gmx.de>/Karsten Keil <kkeil@suse.de>");
52 MODULE_DESCRIPTION("AVM Fritz!PCI/PnP ISDN driver");
53
54 static struct pci_device_id fcpci_ids[] = {
55         { .vendor      = PCI_VENDOR_ID_AVM,
56           .device      = PCI_DEVICE_ID_AVM_A1,
57           .subvendor   = PCI_ANY_ID,
58           .subdevice   = PCI_ANY_ID,
59           .driver_data = (unsigned long) "Fritz!Card PCI",
60         },
61         { .vendor      = PCI_VENDOR_ID_AVM,
62           .device      = PCI_DEVICE_ID_AVM_A1_V2,
63           .subvendor   = PCI_ANY_ID,
64           .subdevice   = PCI_ANY_ID,
65           .driver_data = (unsigned long) "Fritz!Card PCI v2" },
66         {}
67 };
68
69 MODULE_DEVICE_TABLE(pci, fcpci_ids);
70
71 #ifdef __ISAPNP__
72 static struct pnp_device_id fcpnp_ids[] __devinitdata = {
73         { 
74                 .id             = "AVM0900",
75                 .driver_data    = (unsigned long) "Fritz!Card PnP",
76         },
77 };
78
79 MODULE_DEVICE_TABLE(isapnp, fcpnp_ids);
80 #endif
81
82 static int protocol = 2;       /* EURO-ISDN Default */
83 module_param(protocol, int, 0);
84 MODULE_LICENSE("GPL");
85
86 // ----------------------------------------------------------------------
87
88 #define  AVM_INDEX              0x04
89 #define  AVM_DATA               0x10
90
91 #define  AVM_IDX_HDLC_1         0x00
92 #define  AVM_IDX_HDLC_2         0x01
93 #define  AVM_IDX_ISAC_FIFO      0x02
94 #define  AVM_IDX_ISAC_REG_LOW   0x04
95 #define  AVM_IDX_ISAC_REG_HIGH  0x06
96
97 #define  AVM_STATUS0            0x02
98
99 #define  AVM_STATUS0_IRQ_ISAC   0x01
100 #define  AVM_STATUS0_IRQ_HDLC   0x02
101 #define  AVM_STATUS0_IRQ_TIMER  0x04
102 #define  AVM_STATUS0_IRQ_MASK   0x07
103
104 #define  AVM_STATUS0_RESET      0x01
105 #define  AVM_STATUS0_DIS_TIMER  0x02
106 #define  AVM_STATUS0_RES_TIMER  0x04
107 #define  AVM_STATUS0_ENA_IRQ    0x08
108 #define  AVM_STATUS0_TESTBIT    0x10
109
110 #define  AVM_STATUS1            0x03
111 #define  AVM_STATUS1_ENA_IOM    0x80
112
113 #define  HDLC_FIFO              0x0
114 #define  HDLC_STATUS            0x4
115 #define  HDLC_CTRL              0x4
116
117 #define  HDLC_MODE_ITF_FLG      0x01
118 #define  HDLC_MODE_TRANS        0x02
119 #define  HDLC_MODE_CCR_7        0x04
120 #define  HDLC_MODE_CCR_16       0x08
121 #define  HDLC_MODE_TESTLOOP     0x80
122
123 #define  HDLC_INT_XPR           0x80
124 #define  HDLC_INT_XDU           0x40
125 #define  HDLC_INT_RPR           0x20
126 #define  HDLC_INT_MASK          0xE0
127
128 #define  HDLC_STAT_RME          0x01
129 #define  HDLC_STAT_RDO          0x10
130 #define  HDLC_STAT_CRCVFRRAB    0x0E
131 #define  HDLC_STAT_CRCVFR       0x06
132 #define  HDLC_STAT_RML_MASK     0xff00
133
134 #define  HDLC_CMD_XRS           0x80
135 #define  HDLC_CMD_XME           0x01
136 #define  HDLC_CMD_RRS           0x20
137 #define  HDLC_CMD_XML_MASK      0xff00
138
139 #define  AVM_HDLC_FIFO_1        0x10
140 #define  AVM_HDLC_FIFO_2        0x18
141
142 #define  AVM_HDLC_STATUS_1      0x14
143 #define  AVM_HDLC_STATUS_2      0x1c
144
145 #define  AVM_ISACSX_INDEX       0x04
146 #define  AVM_ISACSX_DATA        0x08
147
148 // ----------------------------------------------------------------------
149 // Fritz!PCI
150
151 static unsigned char fcpci_read_isac(struct isac *isac, unsigned char offset)
152 {
153         struct fritz_adapter *adapter = isac->priv;
154         unsigned char idx = (offset > 0x2f) ? 
155                 AVM_IDX_ISAC_REG_HIGH : AVM_IDX_ISAC_REG_LOW;
156         unsigned char val;
157         unsigned long flags;
158
159         spin_lock_irqsave(&adapter->hw_lock, flags);
160         outb(idx, adapter->io + AVM_INDEX);
161         val = inb(adapter->io + AVM_DATA + (offset & 0xf));
162         spin_unlock_irqrestore(&adapter->hw_lock, flags);
163         DBG(0x1000, " port %#x, value %#x",
164             offset, val);
165         return val;
166 }
167
168 static void fcpci_write_isac(struct isac *isac, unsigned char offset,
169                              unsigned char value)
170 {
171         struct fritz_adapter *adapter = isac->priv;
172         unsigned char idx = (offset > 0x2f) ? 
173                 AVM_IDX_ISAC_REG_HIGH : AVM_IDX_ISAC_REG_LOW;
174         unsigned long flags;
175
176         DBG(0x1000, " port %#x, value %#x",
177             offset, value);
178         spin_lock_irqsave(&adapter->hw_lock, flags);
179         outb(idx, adapter->io + AVM_INDEX);
180         outb(value, adapter->io + AVM_DATA + (offset & 0xf));
181         spin_unlock_irqrestore(&adapter->hw_lock, flags);
182 }
183
184 static void fcpci_read_isac_fifo(struct isac *isac, unsigned char * data, 
185                                  int size)
186 {
187         struct fritz_adapter *adapter = isac->priv;
188         unsigned long flags;
189
190         spin_lock_irqsave(&adapter->hw_lock, flags);
191         outb(AVM_IDX_ISAC_FIFO, adapter->io + AVM_INDEX);
192         insb(adapter->io + AVM_DATA, data, size);
193         spin_unlock_irqrestore(&adapter->hw_lock, flags);
194 }
195
196 static void fcpci_write_isac_fifo(struct isac *isac, unsigned char * data, 
197                                   int size)
198 {
199         struct fritz_adapter *adapter = isac->priv;
200         unsigned long flags;
201
202         spin_lock_irqsave(&adapter->hw_lock, flags);
203         outb(AVM_IDX_ISAC_FIFO, adapter->io + AVM_INDEX);
204         outsb(adapter->io + AVM_DATA, data, size);
205         spin_unlock_irqrestore(&adapter->hw_lock, flags);
206 }
207
208 static u32 fcpci_read_hdlc_status(struct fritz_adapter *adapter, int nr)
209 {
210         u32 val;
211         int idx = nr ? AVM_IDX_HDLC_2 : AVM_IDX_HDLC_1;
212         unsigned long flags;
213
214         spin_lock_irqsave(&adapter->hw_lock, flags);
215         outl(idx, adapter->io + AVM_INDEX);
216         val = inl(adapter->io + AVM_DATA + HDLC_STATUS);
217         spin_unlock_irqrestore(&adapter->hw_lock, flags);
218         return val;
219 }
220
221 static void __fcpci_write_ctrl(struct fritz_bcs *bcs, int which)
222 {
223         struct fritz_adapter *adapter = bcs->adapter;
224         int idx = bcs->channel ? AVM_IDX_HDLC_2 : AVM_IDX_HDLC_1;
225
226         DBG(0x40, "hdlc %c wr%x ctrl %x",
227             'A' + bcs->channel, which, bcs->ctrl.ctrl);
228
229         outl(idx, adapter->io + AVM_INDEX);
230         outl(bcs->ctrl.ctrl, adapter->io + AVM_DATA + HDLC_CTRL);
231 }
232
233 static void fcpci_write_ctrl(struct fritz_bcs *bcs, int which)
234 {
235         struct fritz_adapter *adapter = bcs->adapter;
236         unsigned long flags;
237
238         spin_lock_irqsave(&adapter->hw_lock, flags);
239         __fcpci_write_ctrl(bcs, which);
240         spin_unlock_irqrestore(&adapter->hw_lock, flags);
241 }
242
243 // ----------------------------------------------------------------------
244 // Fritz!PCI v2
245
246 static unsigned char fcpci2_read_isac(struct isac *isac, unsigned char offset)
247 {
248         struct fritz_adapter *adapter = isac->priv;
249         unsigned char val;
250         unsigned long flags;
251
252         spin_lock_irqsave(&adapter->hw_lock, flags);
253         outl(offset, adapter->io + AVM_ISACSX_INDEX);
254         val = inl(adapter->io + AVM_ISACSX_DATA);
255         spin_unlock_irqrestore(&adapter->hw_lock, flags);
256         DBG(0x1000, " port %#x, value %#x",
257             offset, val);
258
259         return val;
260 }
261
262 static void fcpci2_write_isac(struct isac *isac, unsigned char offset, 
263                               unsigned char value)
264 {
265         struct fritz_adapter *adapter = isac->priv;
266         unsigned long flags;
267
268         DBG(0x1000, " port %#x, value %#x",
269             offset, value);
270         spin_lock_irqsave(&adapter->hw_lock, flags);
271         outl(offset, adapter->io + AVM_ISACSX_INDEX);
272         outl(value, adapter->io + AVM_ISACSX_DATA);
273         spin_unlock_irqrestore(&adapter->hw_lock, flags);
274 }
275
276 static void fcpci2_read_isac_fifo(struct isac *isac, unsigned char * data, 
277                                   int size)
278 {
279         struct fritz_adapter *adapter = isac->priv;
280         int i;
281         unsigned long flags;
282
283         spin_lock_irqsave(&adapter->hw_lock, flags);
284         outl(0, adapter->io + AVM_ISACSX_INDEX);
285         for (i = 0; i < size; i++)
286                 data[i] = inl(adapter->io + AVM_ISACSX_DATA);
287         spin_unlock_irqrestore(&adapter->hw_lock, flags);
288 }
289
290 static void fcpci2_write_isac_fifo(struct isac *isac, unsigned char * data, 
291                                    int size)
292 {
293         struct fritz_adapter *adapter = isac->priv;
294         int i;
295         unsigned long flags;
296
297         spin_lock_irqsave(&adapter->hw_lock, flags);
298         outl(0, adapter->io + AVM_ISACSX_INDEX);
299         for (i = 0; i < size; i++)
300                 outl(data[i], adapter->io + AVM_ISACSX_DATA);
301         spin_unlock_irqrestore(&adapter->hw_lock, flags);
302 }
303
304 static u32 fcpci2_read_hdlc_status(struct fritz_adapter *adapter, int nr)
305 {
306         int offset = nr ? AVM_HDLC_STATUS_2 : AVM_HDLC_STATUS_1;
307
308         return inl(adapter->io + offset);
309 }
310
311 static void fcpci2_write_ctrl(struct fritz_bcs *bcs, int which)
312 {
313         struct fritz_adapter *adapter = bcs->adapter;
314         int offset = bcs->channel ? AVM_HDLC_STATUS_2 : AVM_HDLC_STATUS_1;
315
316         DBG(0x40, "hdlc %c wr%x ctrl %x",
317             'A' + bcs->channel, which, bcs->ctrl.ctrl);
318
319         outl(bcs->ctrl.ctrl, adapter->io + offset);
320 }
321
322 // ----------------------------------------------------------------------
323 // Fritz!PnP (ISAC access as for Fritz!PCI)
324
325 static u32 fcpnp_read_hdlc_status(struct fritz_adapter *adapter, int nr)
326 {
327         unsigned char idx = nr ? AVM_IDX_HDLC_2 : AVM_IDX_HDLC_1;
328         u32 val;
329         unsigned long flags;
330
331         spin_lock_irqsave(&adapter->hw_lock, flags);
332         outb(idx, adapter->io + AVM_INDEX);
333         val = inb(adapter->io + AVM_DATA + HDLC_STATUS);
334         if (val & HDLC_INT_RPR)
335                 val |= inb(adapter->io + AVM_DATA + HDLC_STATUS + 1) << 8;
336         spin_unlock_irqrestore(&adapter->hw_lock, flags);
337         return val;
338 }
339
340 static void __fcpnp_write_ctrl(struct fritz_bcs *bcs, int which)
341 {
342         struct fritz_adapter *adapter = bcs->adapter;
343         unsigned char idx = bcs->channel ? AVM_IDX_HDLC_2 : AVM_IDX_HDLC_1;
344
345         DBG(0x40, "hdlc %c wr%x ctrl %x",
346             'A' + bcs->channel, which, bcs->ctrl.ctrl);
347
348         outb(idx, adapter->io + AVM_INDEX);
349         if (which & 4)
350                 outb(bcs->ctrl.sr.mode, 
351                      adapter->io + AVM_DATA + HDLC_STATUS + 2);
352         if (which & 2)
353                 outb(bcs->ctrl.sr.xml, 
354                      adapter->io + AVM_DATA + HDLC_STATUS + 1);
355         if (which & 1)
356                 outb(bcs->ctrl.sr.cmd,
357                      adapter->io + AVM_DATA + HDLC_STATUS + 0);
358 }
359
360 static void fcpnp_write_ctrl(struct fritz_bcs *bcs, int which)
361 {
362         struct fritz_adapter *adapter = bcs->adapter;
363         unsigned long flags;
364
365         spin_lock_irqsave(&adapter->hw_lock, flags);
366         __fcpnp_write_ctrl(bcs, which);
367         spin_unlock_irqrestore(&adapter->hw_lock, flags);
368 }
369
370 // ----------------------------------------------------------------------
371
372 static inline void B_L1L2(struct fritz_bcs *bcs, int pr, void *arg)
373 {
374         struct hisax_if *ifc = (struct hisax_if *) &bcs->b_if;
375
376         DBG(2, "pr %#x", pr);
377         ifc->l1l2(ifc, pr, arg);
378 }
379
380 static void hdlc_fill_fifo(struct fritz_bcs *bcs)
381 {
382         struct fritz_adapter *adapter = bcs->adapter;
383         struct sk_buff *skb = bcs->tx_skb;
384         int count;
385         unsigned long flags;
386         unsigned char *p;
387
388         DBG(0x40, "hdlc_fill_fifo");
389
390         if (skb->len == 0)
391                 BUG();
392
393         bcs->ctrl.sr.cmd &= ~HDLC_CMD_XME;
394         if (bcs->tx_skb->len > bcs->fifo_size) {
395                 count = bcs->fifo_size;
396         } else {
397                 count = bcs->tx_skb->len;
398                 if (bcs->mode != L1_MODE_TRANS)
399                         bcs->ctrl.sr.cmd |= HDLC_CMD_XME;
400         }
401         DBG(0x40, "hdlc_fill_fifo %d/%d", count, bcs->tx_skb->len);
402         p = bcs->tx_skb->data;
403         skb_pull(bcs->tx_skb, count);
404         bcs->tx_cnt += count;
405         bcs->ctrl.sr.xml = ((count == bcs->fifo_size) ? 0 : count);
406
407         switch (adapter->type) {
408         case AVM_FRITZ_PCI:
409                 spin_lock_irqsave(&adapter->hw_lock, flags);
410                 // sets the correct AVM_INDEX, too
411                 __fcpci_write_ctrl(bcs, 3);
412                 outsl(adapter->io + AVM_DATA + HDLC_FIFO,
413                       p, (count + 3) / 4);
414                 spin_unlock_irqrestore(&adapter->hw_lock, flags);
415                 break;
416         case AVM_FRITZ_PCIV2:
417                 fcpci2_write_ctrl(bcs, 3);
418                 outsl(adapter->io + 
419                       (bcs->channel ? AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1),
420                       p, (count + 3) / 4);
421                 break;
422         case AVM_FRITZ_PNP:
423                 spin_lock_irqsave(&adapter->hw_lock, flags);
424                 // sets the correct AVM_INDEX, too
425                 __fcpnp_write_ctrl(bcs, 3);
426                 outsb(adapter->io + AVM_DATA, p, count);
427                 spin_unlock_irqrestore(&adapter->hw_lock, flags);
428                 break;
429         }
430 }
431
432 static inline void hdlc_empty_fifo(struct fritz_bcs *bcs, int count)
433 {
434         struct fritz_adapter *adapter = bcs->adapter;
435         unsigned char *p;
436         unsigned char idx = bcs->channel ? AVM_IDX_HDLC_2 : AVM_IDX_HDLC_1;
437
438         DBG(0x10, "hdlc_empty_fifo %d", count);
439         if (bcs->rcvidx + count > HSCX_BUFMAX) {
440                 DBG(0x10, "hdlc_empty_fifo: incoming packet too large");
441                 return;
442         }
443         p = bcs->rcvbuf + bcs->rcvidx;
444         bcs->rcvidx += count;
445         switch (adapter->type) {
446         case AVM_FRITZ_PCI:
447                 spin_lock(&adapter->hw_lock);
448                 outl(idx, adapter->io + AVM_INDEX);
449                 insl(adapter->io + AVM_DATA + HDLC_FIFO, 
450                      p, (count + 3) / 4);
451                 spin_unlock(&adapter->hw_lock);
452                 break;
453         case AVM_FRITZ_PCIV2:
454                 insl(adapter->io + 
455                      (bcs->channel ? AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1),
456                      p, (count + 3) / 4);
457                 break;
458         case AVM_FRITZ_PNP:
459                 spin_lock(&adapter->hw_lock);
460                 outb(idx, adapter->io + AVM_INDEX);
461                 insb(adapter->io + AVM_DATA, p, count);
462                 spin_unlock(&adapter->hw_lock);
463                 break;
464         }
465 }
466
467 static inline void hdlc_rpr_irq(struct fritz_bcs *bcs, u32 stat)
468 {
469         struct fritz_adapter *adapter = bcs->adapter;
470         struct sk_buff *skb;
471         int len;
472
473         if (stat & HDLC_STAT_RDO) {
474                 DBG(0x10, "RDO");
475                 bcs->ctrl.sr.xml = 0;
476                 bcs->ctrl.sr.cmd |= HDLC_CMD_RRS;
477                 adapter->write_ctrl(bcs, 1);
478                 bcs->ctrl.sr.cmd &= ~HDLC_CMD_RRS;
479                 adapter->write_ctrl(bcs, 1);
480                 bcs->rcvidx = 0;
481                 return;
482         }
483
484         len = (stat & HDLC_STAT_RML_MASK) >> 8;
485         if (len == 0)
486                 len = bcs->fifo_size;
487
488         hdlc_empty_fifo(bcs, len);
489
490         if ((stat & HDLC_STAT_RME) || (bcs->mode == L1_MODE_TRANS)) {
491                 if (((stat & HDLC_STAT_CRCVFRRAB)== HDLC_STAT_CRCVFR) ||
492                     (bcs->mode == L1_MODE_TRANS)) {
493                         skb = dev_alloc_skb(bcs->rcvidx);
494                         if (!skb) {
495                                 printk(KERN_WARNING "HDLC: receive out of memory\n");
496                         } else {
497                                 memcpy(skb_put(skb, bcs->rcvidx), bcs->rcvbuf,
498                                        bcs->rcvidx);
499                                 DBG_SKB(1, skb);
500                                 B_L1L2(bcs, PH_DATA | INDICATION, skb);
501                         }
502                         bcs->rcvidx = 0;
503                 } else {
504                         DBG(0x10, "ch%d invalid frame %#x",
505                             bcs->channel, stat);
506                         bcs->rcvidx = 0;
507                 }
508         }
509 }
510
511 static inline void hdlc_xdu_irq(struct fritz_bcs *bcs)
512 {
513         struct fritz_adapter *adapter = bcs->adapter;
514         
515
516         /* Here we lost an TX interrupt, so
517          * restart transmitting the whole frame.
518          */
519         bcs->ctrl.sr.xml = 0;
520         bcs->ctrl.sr.cmd |= HDLC_CMD_XRS;
521         adapter->write_ctrl(bcs, 1);
522         bcs->ctrl.sr.cmd &= ~HDLC_CMD_XRS;
523
524         if (!bcs->tx_skb) {
525                 DBG(0x10, "XDU without skb");
526                 adapter->write_ctrl(bcs, 1);
527                 return;
528         }
529         /* only hdlc restarts the frame, transparent mode must continue */
530         if (bcs->mode == L1_MODE_HDLC) {
531                 skb_push(bcs->tx_skb, bcs->tx_cnt);
532                 bcs->tx_cnt = 0;
533         }
534 }
535
536 static inline void hdlc_xpr_irq(struct fritz_bcs *bcs)
537 {
538         struct sk_buff *skb;
539
540         skb = bcs->tx_skb;
541         if (!skb)
542                 return;
543
544         if (skb->len) {
545                 hdlc_fill_fifo(bcs);
546                 return;
547         }
548         bcs->tx_cnt = 0;
549         bcs->tx_skb = NULL;
550         B_L1L2(bcs, PH_DATA | CONFIRM, (void *) skb->truesize);
551         dev_kfree_skb_irq(skb);
552 }
553
554 static void hdlc_irq_one(struct fritz_bcs *bcs, u32 stat)
555 {
556         DBG(0x10, "ch%d stat %#x", bcs->channel, stat);
557         if (stat & HDLC_INT_RPR) {
558                 DBG(0x10, "RPR");
559                 hdlc_rpr_irq(bcs, stat);
560         }
561         if (stat & HDLC_INT_XDU) {
562                 DBG(0x10, "XDU");
563                 hdlc_xdu_irq(bcs);
564                 hdlc_xpr_irq(bcs);
565                 return;
566         }
567         if (stat & HDLC_INT_XPR) {
568                 DBG(0x10, "XPR");
569                 hdlc_xpr_irq(bcs);
570         }
571 }
572
573 static inline void hdlc_irq(struct fritz_adapter *adapter)
574 {
575         int nr;
576         u32 stat;
577
578         for (nr = 0; nr < 2; nr++) {
579                 stat = adapter->read_hdlc_status(adapter, nr);
580                 DBG(0x10, "HDLC %c stat %#x", 'A' + nr, stat);
581                 if (stat & HDLC_INT_MASK)
582                         hdlc_irq_one(&adapter->bcs[nr], stat);
583         }
584 }
585
586 static void modehdlc(struct fritz_bcs *bcs, int mode)
587 {
588         struct fritz_adapter *adapter = bcs->adapter;
589         
590         DBG(0x40, "hdlc %c mode %d --> %d",
591             'A' + bcs->channel, bcs->mode, mode);
592
593         if (bcs->mode == mode)
594                 return;
595
596         bcs->fifo_size = 32;
597         bcs->ctrl.ctrl = 0;
598         bcs->ctrl.sr.cmd  = HDLC_CMD_XRS | HDLC_CMD_RRS;
599         switch (mode) {
600         case L1_MODE_NULL:
601                 bcs->ctrl.sr.mode = HDLC_MODE_TRANS;
602                 adapter->write_ctrl(bcs, 5);
603                 break;
604         case L1_MODE_TRANS:
605         case L1_MODE_HDLC:
606                 bcs->rcvidx = 0;
607                 bcs->tx_cnt = 0;
608                 bcs->tx_skb = NULL;
609                 if (mode == L1_MODE_TRANS) {
610                         bcs->ctrl.sr.mode = HDLC_MODE_TRANS;
611                 } else {
612                         bcs->ctrl.sr.mode = HDLC_MODE_ITF_FLG;
613                 }
614                 adapter->write_ctrl(bcs, 5);
615                 bcs->ctrl.sr.cmd = HDLC_CMD_XRS;
616                 adapter->write_ctrl(bcs, 1);
617                 bcs->ctrl.sr.cmd = 0;
618                 break;
619         }
620         bcs->mode = mode;
621 }
622
623 static void fritz_b_l2l1(struct hisax_if *ifc, int pr, void *arg)
624 {
625         struct fritz_bcs *bcs = ifc->priv;
626         struct sk_buff *skb = arg;
627         int mode;
628
629         DBG(0x10, "pr %#x", pr);
630
631         switch (pr) {
632         case PH_DATA | REQUEST:
633                 if (bcs->tx_skb)
634                         BUG();
635                 
636                 bcs->tx_skb = skb;
637                 DBG_SKB(1, skb);
638                 hdlc_fill_fifo(bcs);
639                 break;
640         case PH_ACTIVATE | REQUEST:
641                 mode = (int) arg;
642                 DBG(4,"B%d,PH_ACTIVATE_REQUEST %d", bcs->channel + 1, mode);
643                 modehdlc(bcs, mode);
644                 B_L1L2(bcs, PH_ACTIVATE | INDICATION, NULL);
645                 break;
646         case PH_DEACTIVATE | REQUEST:
647                 DBG(4,"B%d,PH_DEACTIVATE_REQUEST", bcs->channel + 1);
648                 modehdlc(bcs, L1_MODE_NULL);
649                 B_L1L2(bcs, PH_DEACTIVATE | INDICATION, NULL);
650                 break;
651         }
652 }
653
654 // ----------------------------------------------------------------------
655
656 static irqreturn_t
657 fcpci2_irq(int intno, void *dev, struct pt_regs *regs)
658 {
659         struct fritz_adapter *adapter = dev;
660         unsigned char val;
661
662         val = inb(adapter->io + AVM_STATUS0);
663         if (!(val & AVM_STATUS0_IRQ_MASK))
664                 /* hopefully a shared  IRQ reqest */
665                 return IRQ_NONE;
666         DBG(2, "STATUS0 %#x", val);
667         if (val & AVM_STATUS0_IRQ_ISAC)
668                 isacsx_irq(&adapter->isac);
669         if (val & AVM_STATUS0_IRQ_HDLC)
670                 hdlc_irq(adapter);
671         if (val & AVM_STATUS0_IRQ_ISAC)
672                 isacsx_irq(&adapter->isac);
673         return IRQ_HANDLED;
674 }
675
676 static irqreturn_t
677 fcpci_irq(int intno, void *dev, struct pt_regs *regs)
678 {
679         struct fritz_adapter *adapter = dev;
680         unsigned char sval;
681
682         sval = inb(adapter->io + 2);
683         if ((sval & AVM_STATUS0_IRQ_MASK) == AVM_STATUS0_IRQ_MASK)
684                 /* possibly a shared  IRQ reqest */
685                 return IRQ_NONE;
686         DBG(2, "sval %#x", sval);
687         if (!(sval & AVM_STATUS0_IRQ_ISAC))
688                 isac_irq(&adapter->isac);
689
690         if (!(sval & AVM_STATUS0_IRQ_HDLC))
691                 hdlc_irq(adapter);
692         return IRQ_HANDLED;
693 }
694
695 // ----------------------------------------------------------------------
696
697 static inline void fcpci2_init(struct fritz_adapter *adapter)
698 {
699         outb(AVM_STATUS0_RES_TIMER, adapter->io + AVM_STATUS0);
700         outb(AVM_STATUS0_ENA_IRQ, adapter->io + AVM_STATUS0);
701
702 }
703
704 static inline void fcpci_init(struct fritz_adapter *adapter)
705 {
706         outb(AVM_STATUS0_DIS_TIMER | AVM_STATUS0_RES_TIMER | 
707              AVM_STATUS0_ENA_IRQ, adapter->io + AVM_STATUS0);
708
709         outb(AVM_STATUS1_ENA_IOM | adapter->irq, 
710              adapter->io + AVM_STATUS1);
711         mdelay(10);
712 }
713
714 // ----------------------------------------------------------------------
715
716 static int __devinit fcpcipnp_setup(struct fritz_adapter *adapter)
717 {
718         u32 val = 0;
719         int retval;
720
721         DBG(1,"");
722
723         isac_init(&adapter->isac); // FIXME is this okay now
724
725         retval = -EBUSY;
726         if (!request_region(adapter->io, 32, "fcpcipnp"))
727                 goto err;
728
729         switch (adapter->type) {
730         case AVM_FRITZ_PCIV2:
731                 retval = request_irq(adapter->irq, fcpci2_irq, SA_SHIRQ, 
732                                      "fcpcipnp", adapter);
733                 break;
734         case AVM_FRITZ_PCI:
735                 retval = request_irq(adapter->irq, fcpci_irq, SA_SHIRQ,
736                                      "fcpcipnp", adapter);
737                 break;
738         case AVM_FRITZ_PNP:
739                 retval = request_irq(adapter->irq, fcpci_irq, 0,
740                                      "fcpcipnp", adapter);
741                 break;
742         }
743         if (retval)
744                 goto err_region;
745
746         switch (adapter->type) {
747         case AVM_FRITZ_PCIV2:
748         case AVM_FRITZ_PCI:
749                 val = inl(adapter->io);
750                 break;
751         case AVM_FRITZ_PNP:
752                 val = inb(adapter->io);
753                 val |= inb(adapter->io + 1) << 8;
754                 break;
755         }
756
757         DBG(1, "stat %#x Class %X Rev %d",
758             val, val & 0xff, (val>>8) & 0xff);
759
760         spin_lock_init(&adapter->hw_lock);
761         adapter->isac.priv = adapter;
762         switch (adapter->type) {
763         case AVM_FRITZ_PCIV2:
764                 adapter->isac.read_isac       = &fcpci2_read_isac;
765                 adapter->isac.write_isac      = &fcpci2_write_isac;
766                 adapter->isac.read_isac_fifo  = &fcpci2_read_isac_fifo;
767                 adapter->isac.write_isac_fifo = &fcpci2_write_isac_fifo;
768
769                 adapter->read_hdlc_status     = &fcpci2_read_hdlc_status;
770                 adapter->write_ctrl           = &fcpci2_write_ctrl;
771                 break;
772         case AVM_FRITZ_PCI:
773                 adapter->isac.read_isac       = &fcpci_read_isac;
774                 adapter->isac.write_isac      = &fcpci_write_isac;
775                 adapter->isac.read_isac_fifo  = &fcpci_read_isac_fifo;
776                 adapter->isac.write_isac_fifo = &fcpci_write_isac_fifo;
777
778                 adapter->read_hdlc_status     = &fcpci_read_hdlc_status;
779                 adapter->write_ctrl           = &fcpci_write_ctrl;
780                 break;
781         case AVM_FRITZ_PNP:
782                 adapter->isac.read_isac       = &fcpci_read_isac;
783                 adapter->isac.write_isac      = &fcpci_write_isac;
784                 adapter->isac.read_isac_fifo  = &fcpci_read_isac_fifo;
785                 adapter->isac.write_isac_fifo = &fcpci_write_isac_fifo;
786
787                 adapter->read_hdlc_status     = &fcpnp_read_hdlc_status;
788                 adapter->write_ctrl           = &fcpnp_write_ctrl;
789                 break;
790         }
791
792         // Reset
793         outb(0, adapter->io + AVM_STATUS0);
794         mdelay(10);
795         outb(AVM_STATUS0_RESET, adapter->io + AVM_STATUS0);
796         mdelay(10);
797         outb(0, adapter->io + AVM_STATUS0);
798         mdelay(10);
799
800         switch (adapter->type) {
801         case AVM_FRITZ_PCIV2:
802                 fcpci2_init(adapter);
803                 isacsx_setup(&adapter->isac);
804                 break;
805         case AVM_FRITZ_PCI:
806         case AVM_FRITZ_PNP:
807                 fcpci_init(adapter);
808                 isac_setup(&adapter->isac);
809                 break;
810         }
811         val = adapter->read_hdlc_status(adapter, 0);
812         DBG(0x20, "HDLC A STA %x", val);
813         val = adapter->read_hdlc_status(adapter, 1);
814         DBG(0x20, "HDLC B STA %x", val);
815
816         adapter->bcs[0].mode = -1;
817         adapter->bcs[1].mode = -1;
818         modehdlc(&adapter->bcs[0], L1_MODE_NULL);
819         modehdlc(&adapter->bcs[1], L1_MODE_NULL);
820
821         return 0;
822
823  err_region:
824         release_region(adapter->io, 32);
825  err:
826         return retval;
827 }
828
829 static void __devexit fcpcipnp_release(struct fritz_adapter *adapter)
830 {
831         DBG(1,"");
832
833         outb(0, adapter->io + AVM_STATUS0);
834         free_irq(adapter->irq, adapter);
835         release_region(adapter->io, 32);
836 }
837
838 // ----------------------------------------------------------------------
839
840 static struct fritz_adapter * __devinit 
841 new_adapter(void)
842 {
843         struct fritz_adapter *adapter;
844         struct hisax_b_if *b_if[2];
845         int i;
846
847         adapter = kmalloc(sizeof(struct fritz_adapter), GFP_KERNEL);
848         if (!adapter)
849                 return NULL;
850
851         memset(adapter, 0, sizeof(struct fritz_adapter));
852
853         adapter->isac.hisax_d_if.owner = THIS_MODULE;
854         adapter->isac.hisax_d_if.ifc.priv = &adapter->isac;
855         adapter->isac.hisax_d_if.ifc.l2l1 = isac_d_l2l1;
856         
857         for (i = 0; i < 2; i++) {
858                 adapter->bcs[i].adapter = adapter;
859                 adapter->bcs[i].channel = i;
860                 adapter->bcs[i].b_if.ifc.priv = &adapter->bcs[i];
861                 adapter->bcs[i].b_if.ifc.l2l1 = fritz_b_l2l1;
862         }
863
864         for (i = 0; i < 2; i++)
865                 b_if[i] = &adapter->bcs[i].b_if;
866
867         hisax_register(&adapter->isac.hisax_d_if, b_if, "fcpcipnp", protocol);
868
869         return adapter;
870 }
871
872 static void delete_adapter(struct fritz_adapter *adapter)
873 {
874         hisax_unregister(&adapter->isac.hisax_d_if);
875         kfree(adapter);
876 }
877
878 static int __devinit fcpci_probe(struct pci_dev *pdev,
879                                  const struct pci_device_id *ent)
880 {
881         struct fritz_adapter *adapter;
882         int retval;
883
884         retval = -ENOMEM;
885         adapter = new_adapter();
886         if (!adapter)
887                 goto err;
888
889         pci_set_drvdata(pdev, adapter);
890
891         if (pdev->device == PCI_DEVICE_ID_AVM_A1_V2) 
892                 adapter->type = AVM_FRITZ_PCIV2;
893         else
894                 adapter->type = AVM_FRITZ_PCI;
895
896         retval = pci_enable_device(pdev);
897         if (retval)
898                 goto err_free;
899
900         adapter->io = pci_resource_start(pdev, 1);
901         adapter->irq = pdev->irq;
902
903         printk(KERN_INFO "hisax_fcpcipnp: found adapter %s at %s\n",
904                (char *) ent->driver_data, pci_name(pdev));
905
906         retval = fcpcipnp_setup(adapter);
907         if (retval)
908                 goto err_free;
909
910         return 0;
911         
912  err_free:
913         delete_adapter(adapter);
914  err:
915         return retval;
916 }
917
918 #ifdef __ISAPNP__
919 static int __devinit fcpnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *dev_id)
920 {
921         struct fritz_adapter *adapter;
922         int retval;
923
924         if (!pdev)
925                 return(-ENODEV);
926
927         retval = -ENOMEM;
928         adapter = new_adapter();
929         if (!adapter)
930                 goto err;
931
932         pnp_set_drvdata(pdev, adapter);
933
934         adapter->type = AVM_FRITZ_PNP;
935
936         pnp_disable_dev(pdev);
937         retval = pnp_activate_dev(pdev);
938         if (retval < 0) {
939                 printk(KERN_WARNING "%s: pnp_activate_dev(%s) ret(%d)\n", __FUNCTION__,
940                         (char *)dev_id->driver_data, retval);
941                 goto err_free;
942         }
943         adapter->io = pnp_port_start(pdev, 0);
944         adapter->irq = pnp_irq(pdev, 0);
945
946         printk(KERN_INFO "hisax_fcpcipnp: found adapter %s at IO %#x irq %d\n",
947                (char *) dev_id->driver_data, adapter->io, adapter->irq);
948
949         retval = fcpcipnp_setup(adapter);
950         if (retval)
951                 goto err_free;
952
953         return 0;
954         
955  err_free:
956         delete_adapter(adapter);
957  err:
958         return retval;
959 }
960
961 static void __devexit fcpnp_remove(struct pnp_dev *pdev)
962 {
963         struct fritz_adapter *adapter = pnp_get_drvdata(pdev);
964
965         if (adapter) {
966                 fcpcipnp_release(adapter);
967                 delete_adapter(adapter);
968         }
969         pnp_disable_dev(pdev);
970 }
971
972 static struct pnp_driver fcpnp_driver = {
973         .name           = "fcpnp",
974         .probe          = fcpnp_probe,
975         .remove         = __devexit_p(fcpnp_remove),
976         .id_table       = fcpnp_ids,
977 };
978 #endif
979
980 static void __devexit fcpci_remove(struct pci_dev *pdev)
981 {
982         struct fritz_adapter *adapter = pci_get_drvdata(pdev);
983
984         fcpcipnp_release(adapter);
985         pci_disable_device(pdev);
986         delete_adapter(adapter);
987 }
988
989 static struct pci_driver fcpci_driver = {
990         .name           = "fcpci",
991         .probe          = fcpci_probe,
992         .remove         = __devexit_p(fcpci_remove),
993         .id_table       = fcpci_ids,
994 };
995
996 static int __init hisax_fcpcipnp_init(void)
997 {
998         int retval;
999
1000         printk(KERN_INFO "hisax_fcpcipnp: Fritz!Card PCI/PCIv2/PnP ISDN driver v0.0.1\n");
1001
1002         retval = pci_register_driver(&fcpci_driver);
1003         if (retval)
1004                 goto out;
1005 #ifdef __ISAPNP__
1006         retval = pnp_register_driver(&fcpnp_driver);
1007         if (retval < 0)
1008                 goto out_unregister_pci;
1009 #endif
1010         return 0;
1011
1012  out_unregister_pci:
1013         pci_unregister_driver(&fcpci_driver);
1014  out:
1015         return retval;
1016 }
1017
1018 static void __exit hisax_fcpcipnp_exit(void)
1019 {
1020 #ifdef __ISAPNP__
1021         pnp_unregister_driver(&fcpnp_driver);
1022 #endif
1023         pci_unregister_driver(&fcpci_driver);
1024 }
1025
1026 module_init(hisax_fcpcipnp_init);
1027 module_exit(hisax_fcpcipnp_exit);