Merge /spare/repo/linux-2.6/
[linux-2.6] / drivers / isdn / hardware / avm / b1dma.c
1 /* $Id: b1dma.c,v 1.1.2.3 2004/02/10 01:07:12 keil Exp $
2  * 
3  * Common module for AVM B1 cards that support dma with AMCC
4  * 
5  * Copyright 2000 by Carsten Paeth <calle@calle.de>
6  * 
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/delay.h>
17 #include <linux/mm.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/capi.h>
21 #include <linux/kernelcapi.h>
22 #include <asm/io.h>
23 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <linux/netdevice.h>
26 #include <linux/isdn/capilli.h>
27 #include "avmcard.h"
28 #include <linux/isdn/capicmd.h>
29 #include <linux/isdn/capiutil.h>
30
31 static char *revision = "$Revision: 1.1.2.3 $";
32
33 #undef CONFIG_B1DMA_DEBUG
34
35 /* ------------------------------------------------------------- */
36
37 MODULE_DESCRIPTION("CAPI4Linux: DMA support for active AVM cards");
38 MODULE_AUTHOR("Carsten Paeth");
39 MODULE_LICENSE("GPL");
40
41 static int suppress_pollack = 0;
42 MODULE_PARM(suppress_pollack, "0-1i");
43
44 /* ------------------------------------------------------------- */
45
46 static void b1dma_dispatch_tx(avmcard *card);
47
48 /* ------------------------------------------------------------- */
49
50 /* S5933 */
51
52 #define AMCC_RXPTR      0x24
53 #define AMCC_RXLEN      0x28
54 #define AMCC_TXPTR      0x2c
55 #define AMCC_TXLEN      0x30
56
57 #define AMCC_INTCSR     0x38
58 #       define EN_READ_TC_INT           0x00008000L
59 #       define EN_WRITE_TC_INT          0x00004000L
60 #       define EN_TX_TC_INT             EN_READ_TC_INT
61 #       define EN_RX_TC_INT             EN_WRITE_TC_INT
62 #       define AVM_FLAG                 0x30000000L
63
64 #       define ANY_S5933_INT            0x00800000L
65 #       define READ_TC_INT              0x00080000L
66 #       define WRITE_TC_INT             0x00040000L
67 #       define  TX_TC_INT               READ_TC_INT
68 #       define  RX_TC_INT               WRITE_TC_INT
69 #       define MASTER_ABORT_INT         0x00100000L
70 #       define TARGET_ABORT_INT         0x00200000L
71 #       define BUS_MASTER_INT           0x00200000L
72 #       define ALL_INT                  0x000C0000L
73
74 #define AMCC_MCSR       0x3c
75 #       define A2P_HI_PRIORITY          0x00000100L
76 #       define EN_A2P_TRANSFERS         0x00000400L
77 #       define P2A_HI_PRIORITY          0x00001000L
78 #       define EN_P2A_TRANSFERS         0x00004000L
79 #       define RESET_A2P_FLAGS          0x04000000L
80 #       define RESET_P2A_FLAGS          0x02000000L
81
82 /* ------------------------------------------------------------- */
83
84 static inline void b1dma_writel(avmcard *card, u32 value, int off)
85 {
86         writel(value, card->mbase + off);
87 }
88
89 static inline u32 b1dma_readl(avmcard *card, int off)
90 {
91         return readl(card->mbase + off);
92 }
93
94 /* ------------------------------------------------------------- */
95
96 static inline int b1dma_tx_empty(unsigned int port)
97 {
98         return inb(port + 0x03) & 0x1;
99 }
100
101 static inline int b1dma_rx_full(unsigned int port)
102 {
103         return inb(port + 0x02) & 0x1;
104 }
105
106 static int b1dma_tolink(avmcard *card, void *buf, unsigned int len)
107 {
108         unsigned long stop = jiffies + 1 * HZ;  /* maximum wait time 1 sec */
109         unsigned char *s = (unsigned char *)buf;
110         while (len--) {
111                 while (   !b1dma_tx_empty(card->port)
112                        && time_before(jiffies, stop));
113                 if (!b1dma_tx_empty(card->port)) 
114                         return -1;
115                 t1outp(card->port, 0x01, *s++);
116         }
117         return 0;
118 }
119
120 static int b1dma_fromlink(avmcard *card, void *buf, unsigned int len)
121 {
122         unsigned long stop = jiffies + 1 * HZ;  /* maximum wait time 1 sec */
123         unsigned char *s = (unsigned char *)buf;
124         while (len--) {
125                 while (   !b1dma_rx_full(card->port)
126                        && time_before(jiffies, stop));
127                 if (!b1dma_rx_full(card->port)) 
128                         return -1;
129                 *s++ = t1inp(card->port, 0x00);
130         }
131         return 0;
132 }
133
134 static int WriteReg(avmcard *card, u32 reg, u8 val)
135 {
136         u8 cmd = 0x00;
137         if (   b1dma_tolink(card, &cmd, 1) == 0
138             && b1dma_tolink(card, &reg, 4) == 0) {
139                 u32 tmp = val;
140                 return b1dma_tolink(card, &tmp, 4);
141         }
142         return -1;
143 }
144
145 static u8 ReadReg(avmcard *card, u32 reg)
146 {
147         u8 cmd = 0x01;
148         if (   b1dma_tolink(card, &cmd, 1) == 0
149             && b1dma_tolink(card, &reg, 4) == 0) {
150                 u32 tmp;
151                 if (b1dma_fromlink(card, &tmp, 4) == 0)
152                         return (u8)tmp;
153         }
154         return 0xff;
155 }
156
157 /* ------------------------------------------------------------- */
158
159 static inline void _put_byte(void **pp, u8 val)
160 {
161         u8 *s = *pp;
162         *s++ = val;
163         *pp = s;
164 }
165
166 static inline void _put_word(void **pp, u32 val)
167 {
168         u8 *s = *pp;
169         *s++ = val & 0xff;
170         *s++ = (val >> 8) & 0xff;
171         *s++ = (val >> 16) & 0xff;
172         *s++ = (val >> 24) & 0xff;
173         *pp = s;
174 }
175
176 static inline void _put_slice(void **pp, unsigned char *dp, unsigned int len)
177 {
178         unsigned i = len;
179         _put_word(pp, i);
180         while (i-- > 0)
181                 _put_byte(pp, *dp++);
182 }
183
184 static inline u8 _get_byte(void **pp)
185 {
186         u8 *s = *pp;
187         u8 val;
188         val = *s++;
189         *pp = s;
190         return val;
191 }
192
193 static inline u32 _get_word(void **pp)
194 {
195         u8 *s = *pp;
196         u32 val;
197         val = *s++;
198         val |= (*s++ << 8);
199         val |= (*s++ << 16);
200         val |= (*s++ << 24);
201         *pp = s;
202         return val;
203 }
204
205 static inline u32 _get_slice(void **pp, unsigned char *dp)
206 {
207         unsigned int len, i;
208
209         len = i = _get_word(pp);
210         while (i-- > 0) *dp++ = _get_byte(pp);
211         return len;
212 }
213
214 /* ------------------------------------------------------------- */
215
216 void b1dma_reset(avmcard *card)
217 {
218         card->csr = 0x0;
219         b1dma_writel(card, card->csr, AMCC_INTCSR);
220         b1dma_writel(card, 0, AMCC_MCSR);
221         b1dma_writel(card, 0, AMCC_RXLEN);
222         b1dma_writel(card, 0, AMCC_TXLEN);
223
224         t1outp(card->port, 0x10, 0x00);
225         t1outp(card->port, 0x07, 0x00);
226
227         b1dma_writel(card, 0, AMCC_MCSR);
228         mdelay(10);
229         b1dma_writel(card, 0x0f000000, AMCC_MCSR); /* reset all */
230         mdelay(10);
231         b1dma_writel(card, 0, AMCC_MCSR);
232         if (card->cardtype == avm_t1pci)
233                 mdelay(42);
234         else
235                 mdelay(10);
236 }
237
238 /* ------------------------------------------------------------- */
239
240 static int b1dma_detect(avmcard *card)
241 {
242         b1dma_writel(card, 0, AMCC_MCSR);
243         mdelay(10);
244         b1dma_writel(card, 0x0f000000, AMCC_MCSR); /* reset all */
245         mdelay(10);
246         b1dma_writel(card, 0, AMCC_MCSR);
247         mdelay(42);
248
249         b1dma_writel(card, 0, AMCC_RXLEN);
250         b1dma_writel(card, 0, AMCC_TXLEN);
251         card->csr = 0x0;
252         b1dma_writel(card, card->csr, AMCC_INTCSR);
253
254         if (b1dma_readl(card, AMCC_MCSR) != 0x000000E6)
255                 return 1;
256
257         b1dma_writel(card, 0xffffffff, AMCC_RXPTR);
258         b1dma_writel(card, 0xffffffff, AMCC_TXPTR);
259         if (   b1dma_readl(card, AMCC_RXPTR) != 0xfffffffc
260             || b1dma_readl(card, AMCC_TXPTR) != 0xfffffffc)
261                 return 2;
262
263         b1dma_writel(card, 0x0, AMCC_RXPTR);
264         b1dma_writel(card, 0x0, AMCC_TXPTR);
265         if (   b1dma_readl(card, AMCC_RXPTR) != 0x0
266             || b1dma_readl(card, AMCC_TXPTR) != 0x0)
267                 return 3;
268
269         t1outp(card->port, 0x10, 0x00);
270         t1outp(card->port, 0x07, 0x00);
271         
272         t1outp(card->port, 0x02, 0x02);
273         t1outp(card->port, 0x03, 0x02);
274
275         if (   (t1inp(card->port, 0x02) & 0xFE) != 0x02
276             || t1inp(card->port, 0x3) != 0x03)
277                 return 4;
278
279         t1outp(card->port, 0x02, 0x00);
280         t1outp(card->port, 0x03, 0x00);
281
282         if (   (t1inp(card->port, 0x02) & 0xFE) != 0x00
283             || t1inp(card->port, 0x3) != 0x01)
284                 return 5;
285
286         return 0;
287 }
288
289 int t1pci_detect(avmcard *card)
290 {
291         int ret;
292
293         if ((ret = b1dma_detect(card)) != 0)
294                 return ret;
295         
296         /* Transputer test */
297         
298         if (   WriteReg(card, 0x80001000, 0x11) != 0
299             || WriteReg(card, 0x80101000, 0x22) != 0
300             || WriteReg(card, 0x80201000, 0x33) != 0
301             || WriteReg(card, 0x80301000, 0x44) != 0)
302                 return 6;
303
304         if (   ReadReg(card, 0x80001000) != 0x11
305             || ReadReg(card, 0x80101000) != 0x22
306             || ReadReg(card, 0x80201000) != 0x33
307             || ReadReg(card, 0x80301000) != 0x44)
308                 return 7;
309
310         if (   WriteReg(card, 0x80001000, 0x55) != 0
311             || WriteReg(card, 0x80101000, 0x66) != 0
312             || WriteReg(card, 0x80201000, 0x77) != 0
313             || WriteReg(card, 0x80301000, 0x88) != 0)
314                 return 8;
315
316         if (   ReadReg(card, 0x80001000) != 0x55
317             || ReadReg(card, 0x80101000) != 0x66
318             || ReadReg(card, 0x80201000) != 0x77
319             || ReadReg(card, 0x80301000) != 0x88)
320                 return 9;
321
322         return 0;
323 }
324
325 int b1pciv4_detect(avmcard *card)
326 {
327         int ret, i;
328
329         if ((ret = b1dma_detect(card)) != 0)
330                 return ret;
331         
332         for (i=0; i < 5 ; i++) {
333                 if (WriteReg(card, 0x80A00000, 0x21) != 0)
334                         return 6;
335                 if ((ReadReg(card, 0x80A00000) & 0x01) != 0x01)
336                         return 7;
337         }
338         for (i=0; i < 5 ; i++) {
339                 if (WriteReg(card, 0x80A00000, 0x20) != 0)
340                         return 8;
341                 if ((ReadReg(card, 0x80A00000) & 0x01) != 0x00)
342                         return 9;
343         }
344         
345         return 0;
346 }
347
348 static void b1dma_queue_tx(avmcard *card, struct sk_buff *skb)
349 {
350         unsigned long flags;
351
352         spin_lock_irqsave(&card->lock, flags);
353
354         skb_queue_tail(&card->dma->send_queue, skb);
355
356         if (!(card->csr & EN_TX_TC_INT)) {
357                 b1dma_dispatch_tx(card);
358                 b1dma_writel(card, card->csr, AMCC_INTCSR);
359         }
360
361         spin_unlock_irqrestore(&card->lock, flags);
362 }
363
364 /* ------------------------------------------------------------- */
365
366 static void b1dma_dispatch_tx(avmcard *card)
367 {
368         avmcard_dmainfo *dma = card->dma;
369         struct sk_buff *skb;
370         u8 cmd, subcmd;
371         u16 len;
372         u32 txlen;
373         void *p;
374         
375         skb = skb_dequeue(&dma->send_queue);
376
377         len = CAPIMSG_LEN(skb->data);
378
379         if (len) {
380                 cmd = CAPIMSG_COMMAND(skb->data);
381                 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
382
383                 p = dma->sendbuf.dmabuf;
384
385                 if (CAPICMD(cmd, subcmd) == CAPI_DATA_B3_REQ) {
386                         u16 dlen = CAPIMSG_DATALEN(skb->data);
387                         _put_byte(&p, SEND_DATA_B3_REQ);
388                         _put_slice(&p, skb->data, len);
389                         _put_slice(&p, skb->data + len, dlen);
390                 } else {
391                         _put_byte(&p, SEND_MESSAGE);
392                         _put_slice(&p, skb->data, len);
393                 }
394                 txlen = (u8 *)p - (u8 *)dma->sendbuf.dmabuf;
395 #ifdef CONFIG_B1DMA_DEBUG
396                 printk(KERN_DEBUG "tx: put msg len=%d\n", txlen);
397 #endif
398         } else {
399                 txlen = skb->len-2;
400 #ifdef CONFIG_B1DMA_POLLDEBUG
401                 if (skb->data[2] == SEND_POLLACK)
402                         printk(KERN_INFO "%s: send ack\n", card->name);
403 #endif
404 #ifdef CONFIG_B1DMA_DEBUG
405                 printk(KERN_DEBUG "tx: put 0x%x len=%d\n", 
406                        skb->data[2], txlen);
407 #endif
408                 memcpy(dma->sendbuf.dmabuf, skb->data+2, skb->len-2);
409         }
410         txlen = (txlen + 3) & ~3;
411
412         b1dma_writel(card, dma->sendbuf.dmaaddr, AMCC_TXPTR);
413         b1dma_writel(card, txlen, AMCC_TXLEN);
414
415         card->csr |= EN_TX_TC_INT;
416
417         dev_kfree_skb_any(skb);
418 }
419
420 /* ------------------------------------------------------------- */
421
422 static void queue_pollack(avmcard *card)
423 {
424         struct sk_buff *skb;
425         void *p;
426
427         skb = alloc_skb(3, GFP_ATOMIC);
428         if (!skb) {
429                 printk(KERN_CRIT "%s: no memory, lost poll ack\n",
430                                         card->name);
431                 return;
432         }
433         p = skb->data;
434         _put_byte(&p, 0);
435         _put_byte(&p, 0);
436         _put_byte(&p, SEND_POLLACK);
437         skb_put(skb, (u8 *)p - (u8 *)skb->data);
438
439         b1dma_queue_tx(card, skb);
440 }
441
442 /* ------------------------------------------------------------- */
443
444 static void b1dma_handle_rx(avmcard *card)
445 {
446         avmctrl_info *cinfo = &card->ctrlinfo[0];
447         avmcard_dmainfo *dma = card->dma;
448         struct capi_ctr *ctrl = &cinfo->capi_ctrl;
449         struct sk_buff *skb;
450         void *p = dma->recvbuf.dmabuf+4;
451         u32 ApplId, MsgLen, DataB3Len, NCCI, WindowSize;
452         u8 b1cmd =  _get_byte(&p);
453
454 #ifdef CONFIG_B1DMA_DEBUG
455         printk(KERN_DEBUG "rx: 0x%x %lu\n", b1cmd, (unsigned long)dma->recvlen);
456 #endif
457         
458         switch (b1cmd) {
459         case RECEIVE_DATA_B3_IND:
460
461                 ApplId = (unsigned) _get_word(&p);
462                 MsgLen = _get_slice(&p, card->msgbuf);
463                 DataB3Len = _get_slice(&p, card->databuf);
464
465                 if (MsgLen < 30) { /* not CAPI 64Bit */
466                         memset(card->msgbuf+MsgLen, 0, 30-MsgLen);
467                         MsgLen = 30;
468                         CAPIMSG_SETLEN(card->msgbuf, 30);
469                 }
470                 if (!(skb = alloc_skb(DataB3Len+MsgLen, GFP_ATOMIC))) {
471                         printk(KERN_ERR "%s: incoming packet dropped\n",
472                                         card->name);
473                 } else {
474                         memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
475                         memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
476                         capi_ctr_handle_message(ctrl, ApplId, skb);
477                 }
478                 break;
479
480         case RECEIVE_MESSAGE:
481
482                 ApplId = (unsigned) _get_word(&p);
483                 MsgLen = _get_slice(&p, card->msgbuf);
484                 if (!(skb = alloc_skb(MsgLen, GFP_ATOMIC))) {
485                         printk(KERN_ERR "%s: incoming packet dropped\n",
486                                         card->name);
487                 } else {
488                         memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
489                         if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF)
490                                 capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
491                                                      CAPIMSG_NCCI(skb->data),
492                                                      CAPIMSG_MSGID(skb->data));
493
494                         capi_ctr_handle_message(ctrl, ApplId, skb);
495                 }
496                 break;
497
498         case RECEIVE_NEW_NCCI:
499
500                 ApplId = _get_word(&p);
501                 NCCI = _get_word(&p);
502                 WindowSize = _get_word(&p);
503
504                 capilib_new_ncci(&cinfo->ncci_head, ApplId, NCCI, WindowSize);
505
506                 break;
507
508         case RECEIVE_FREE_NCCI:
509
510                 ApplId = _get_word(&p);
511                 NCCI = _get_word(&p);
512
513                 if (NCCI != 0xffffffff)
514                         capilib_free_ncci(&cinfo->ncci_head, ApplId, NCCI);
515
516                 break;
517
518         case RECEIVE_START:
519 #ifdef CONFIG_B1DMA_POLLDEBUG
520                 printk(KERN_INFO "%s: receive poll\n", card->name);
521 #endif
522                 if (!suppress_pollack)
523                         queue_pollack(card);
524                 capi_ctr_resume_output(ctrl);
525                 break;
526
527         case RECEIVE_STOP:
528                 capi_ctr_suspend_output(ctrl);
529                 break;
530
531         case RECEIVE_INIT:
532
533                 cinfo->versionlen = _get_slice(&p, cinfo->versionbuf);
534                 b1_parse_version(cinfo);
535                 printk(KERN_INFO "%s: %s-card (%s) now active\n",
536                        card->name,
537                        cinfo->version[VER_CARDTYPE],
538                        cinfo->version[VER_DRIVER]);
539                 capi_ctr_ready(ctrl);
540                 break;
541
542         case RECEIVE_TASK_READY:
543                 ApplId = (unsigned) _get_word(&p);
544                 MsgLen = _get_slice(&p, card->msgbuf);
545                 card->msgbuf[MsgLen] = 0;
546                 while (    MsgLen > 0
547                        && (   card->msgbuf[MsgLen-1] == '\n'
548                            || card->msgbuf[MsgLen-1] == '\r')) {
549                         card->msgbuf[MsgLen-1] = 0;
550                         MsgLen--;
551                 }
552                 printk(KERN_INFO "%s: task %d \"%s\" ready.\n",
553                                 card->name, ApplId, card->msgbuf);
554                 break;
555
556         case RECEIVE_DEBUGMSG:
557                 MsgLen = _get_slice(&p, card->msgbuf);
558                 card->msgbuf[MsgLen] = 0;
559                 while (    MsgLen > 0
560                        && (   card->msgbuf[MsgLen-1] == '\n'
561                            || card->msgbuf[MsgLen-1] == '\r')) {
562                         card->msgbuf[MsgLen-1] = 0;
563                         MsgLen--;
564                 }
565                 printk(KERN_INFO "%s: DEBUG: %s\n", card->name, card->msgbuf);
566                 break;
567
568         default:
569                 printk(KERN_ERR "%s: b1dma_interrupt: 0x%x ???\n",
570                                 card->name, b1cmd);
571                 return;
572         }
573 }
574
575 /* ------------------------------------------------------------- */
576
577 static void b1dma_handle_interrupt(avmcard *card)
578 {
579         u32 status;
580         u32 newcsr;
581
582         spin_lock(&card->lock);
583
584         status = b1dma_readl(card, AMCC_INTCSR);
585         if ((status & ANY_S5933_INT) == 0) {
586                 spin_unlock(&card->lock);
587                 return;
588         }
589
590         newcsr = card->csr | (status & ALL_INT);
591         if (status & TX_TC_INT) newcsr &= ~EN_TX_TC_INT;
592         if (status & RX_TC_INT) newcsr &= ~EN_RX_TC_INT;
593         b1dma_writel(card, newcsr, AMCC_INTCSR);
594
595         if ((status & RX_TC_INT) != 0) {
596                 struct avmcard_dmainfo *dma = card->dma;
597                 u32 rxlen;
598                 if (card->dma->recvlen == 0) {
599                         rxlen = b1dma_readl(card, AMCC_RXLEN);
600                         if (rxlen == 0) {
601                                 dma->recvlen = *((u32 *)dma->recvbuf.dmabuf);
602                                 rxlen = (dma->recvlen + 3) & ~3;
603                                 b1dma_writel(card, dma->recvbuf.dmaaddr+4, AMCC_RXPTR);
604                                 b1dma_writel(card, rxlen, AMCC_RXLEN);
605 #ifdef CONFIG_B1DMA_DEBUG
606                         } else {
607                                 printk(KERN_ERR "%s: rx not complete (%d).\n",
608                                         card->name, rxlen);
609 #endif
610                         }
611                 } else {
612                         spin_unlock(&card->lock);
613                         b1dma_handle_rx(card);
614                         dma->recvlen = 0;
615                         spin_lock(&card->lock);
616                         b1dma_writel(card, dma->recvbuf.dmaaddr, AMCC_RXPTR);
617                         b1dma_writel(card, 4, AMCC_RXLEN);
618                 }
619         }
620
621         if ((status & TX_TC_INT) != 0) {
622                 if (skb_queue_empty(&card->dma->send_queue))
623                         card->csr &= ~EN_TX_TC_INT;
624                 else
625                         b1dma_dispatch_tx(card);
626         }
627         b1dma_writel(card, card->csr, AMCC_INTCSR);
628
629         spin_unlock(&card->lock);
630 }
631
632 irqreturn_t b1dma_interrupt(int interrupt, void *devptr, struct pt_regs *regs)
633 {
634         avmcard *card = devptr;
635
636         b1dma_handle_interrupt(card);
637         return IRQ_HANDLED;
638 }
639
640 /* ------------------------------------------------------------- */
641
642 static int b1dma_loaded(avmcard *card)
643 {
644         unsigned long stop;
645         unsigned char ans;
646         unsigned long tout = 2;
647         unsigned int base = card->port;
648
649         for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
650                 if (b1_tx_empty(base))
651                         break;
652         }
653         if (!b1_tx_empty(base)) {
654                 printk(KERN_ERR "%s: b1dma_loaded: tx err, corrupted t4 file ?\n",
655                                 card->name);
656                 return 0;
657         }
658         b1_put_byte(base, SEND_POLLACK);
659         for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
660                 if (b1_rx_full(base)) {
661                         if ((ans = b1_get_byte(base)) == RECEIVE_POLLDWORD) {
662                                 return 1;
663                         }
664                         printk(KERN_ERR "%s: b1dma_loaded: got 0x%x, firmware not running in dword mode\n", card->name, ans);
665                         return 0;
666                 }
667         }
668         printk(KERN_ERR "%s: b1dma_loaded: firmware not running\n", card->name);
669         return 0;
670 }
671
672 /* ------------------------------------------------------------- */
673
674 static void b1dma_send_init(avmcard *card)
675 {
676         struct sk_buff *skb;
677         void *p;
678
679         skb = alloc_skb(15, GFP_ATOMIC);
680         if (!skb) {
681                 printk(KERN_CRIT "%s: no memory, lost register appl.\n",
682                                         card->name);
683                 return;
684         }
685         p = skb->data;
686         _put_byte(&p, 0);
687         _put_byte(&p, 0);
688         _put_byte(&p, SEND_INIT);
689         _put_word(&p, CAPI_MAXAPPL);
690         _put_word(&p, AVM_NCCI_PER_CHANNEL*30);
691         _put_word(&p, card->cardnr - 1);
692         skb_put(skb, (u8 *)p - (u8 *)skb->data);
693
694         b1dma_queue_tx(card, skb);
695 }
696
697 int b1dma_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
698 {
699         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
700         avmcard *card = cinfo->card;
701         int retval;
702
703         b1dma_reset(card);
704
705         if ((retval = b1_load_t4file(card, &data->firmware))) {
706                 b1dma_reset(card);
707                 printk(KERN_ERR "%s: failed to load t4file!!\n",
708                                         card->name);
709                 return retval;
710         }
711
712         if (data->configuration.len > 0 && data->configuration.data) {
713                 if ((retval = b1_load_config(card, &data->configuration))) {
714                         b1dma_reset(card);
715                         printk(KERN_ERR "%s: failed to load config!!\n",
716                                         card->name);
717                         return retval;
718                 }
719         }
720
721         if (!b1dma_loaded(card)) {
722                 b1dma_reset(card);
723                 printk(KERN_ERR "%s: failed to load t4file.\n", card->name);
724                 return -EIO;
725         }
726
727         card->csr = AVM_FLAG;
728         b1dma_writel(card, card->csr, AMCC_INTCSR);
729         b1dma_writel(card, EN_A2P_TRANSFERS|EN_P2A_TRANSFERS|A2P_HI_PRIORITY|
730                      P2A_HI_PRIORITY|RESET_A2P_FLAGS|RESET_P2A_FLAGS, 
731                      AMCC_MCSR);
732         t1outp(card->port, 0x07, 0x30);
733         t1outp(card->port, 0x10, 0xF0);
734
735         card->dma->recvlen = 0;
736         b1dma_writel(card, card->dma->recvbuf.dmaaddr, AMCC_RXPTR);
737         b1dma_writel(card, 4, AMCC_RXLEN);
738         card->csr |= EN_RX_TC_INT;
739         b1dma_writel(card, card->csr, AMCC_INTCSR);
740
741         b1dma_send_init(card);
742
743         return 0;
744 }
745
746 void b1dma_reset_ctr(struct capi_ctr *ctrl)
747 {
748         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
749         avmcard *card = cinfo->card;
750         unsigned long flags;
751
752         spin_lock_irqsave(&card->lock, flags);
753         b1dma_reset(card);
754         spin_unlock_irqrestore(&card->lock, flags);
755
756         memset(cinfo->version, 0, sizeof(cinfo->version));
757         capilib_release(&cinfo->ncci_head);
758         capi_ctr_reseted(ctrl);
759 }
760
761 /* ------------------------------------------------------------- */
762
763 void b1dma_register_appl(struct capi_ctr *ctrl,
764                                 u16 appl,
765                                 capi_register_params *rp)
766 {
767         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
768         avmcard *card = cinfo->card;
769         struct sk_buff *skb;
770         int want = rp->level3cnt;
771         int nconn;
772         void *p;
773
774         if (want > 0) nconn = want;
775         else nconn = ctrl->profile.nbchannel * -want;
776         if (nconn == 0) nconn = ctrl->profile.nbchannel;
777
778         skb = alloc_skb(23, GFP_ATOMIC);
779         if (!skb) {
780                 printk(KERN_CRIT "%s: no memory, lost register appl.\n",
781                                         card->name);
782                 return;
783         }
784         p = skb->data;
785         _put_byte(&p, 0);
786         _put_byte(&p, 0);
787         _put_byte(&p, SEND_REGISTER);
788         _put_word(&p, appl);
789         _put_word(&p, 1024 * (nconn+1));
790         _put_word(&p, nconn);
791         _put_word(&p, rp->datablkcnt);
792         _put_word(&p, rp->datablklen);
793         skb_put(skb, (u8 *)p - (u8 *)skb->data);
794
795         b1dma_queue_tx(card, skb);
796 }
797
798 /* ------------------------------------------------------------- */
799
800 void b1dma_release_appl(struct capi_ctr *ctrl, u16 appl)
801 {
802         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
803         avmcard *card = cinfo->card;
804         struct sk_buff *skb;
805         void *p;
806
807         capilib_release_appl(&cinfo->ncci_head, appl);
808
809         skb = alloc_skb(7, GFP_ATOMIC);
810         if (!skb) {
811                 printk(KERN_CRIT "%s: no memory, lost release appl.\n",
812                                         card->name);
813                 return;
814         }
815         p = skb->data;
816         _put_byte(&p, 0);
817         _put_byte(&p, 0);
818         _put_byte(&p, SEND_RELEASE);
819         _put_word(&p, appl);
820
821         skb_put(skb, (u8 *)p - (u8 *)skb->data);
822
823         b1dma_queue_tx(card, skb);
824 }
825
826 /* ------------------------------------------------------------- */
827
828 u16 b1dma_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
829 {
830         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
831         avmcard *card = cinfo->card;
832         u16 retval = CAPI_NOERROR;
833
834         if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
835                 retval = capilib_data_b3_req(&cinfo->ncci_head,
836                                              CAPIMSG_APPID(skb->data),
837                                              CAPIMSG_NCCI(skb->data),
838                                              CAPIMSG_MSGID(skb->data));
839         }
840         if (retval == CAPI_NOERROR) 
841                 b1dma_queue_tx(card, skb);
842
843         return retval;
844 }
845
846 /* ------------------------------------------------------------- */
847
848 int b1dmactl_read_proc(char *page, char **start, off_t off,
849                         int count, int *eof, struct capi_ctr *ctrl)
850 {
851         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
852         avmcard *card = cinfo->card;
853         u8 flag;
854         int len = 0;
855         char *s;
856         u32 txoff, txlen, rxoff, rxlen, csr;
857         unsigned long flags;
858
859         len += sprintf(page+len, "%-16s %s\n", "name", card->name);
860         len += sprintf(page+len, "%-16s 0x%x\n", "io", card->port);
861         len += sprintf(page+len, "%-16s %d\n", "irq", card->irq);
862         len += sprintf(page+len, "%-16s 0x%lx\n", "membase", card->membase);
863         switch (card->cardtype) {
864         case avm_b1isa: s = "B1 ISA"; break;
865         case avm_b1pci: s = "B1 PCI"; break;
866         case avm_b1pcmcia: s = "B1 PCMCIA"; break;
867         case avm_m1: s = "M1"; break;
868         case avm_m2: s = "M2"; break;
869         case avm_t1isa: s = "T1 ISA (HEMA)"; break;
870         case avm_t1pci: s = "T1 PCI"; break;
871         case avm_c4: s = "C4"; break;
872         case avm_c2: s = "C2"; break;
873         default: s = "???"; break;
874         }
875         len += sprintf(page+len, "%-16s %s\n", "type", s);
876         if ((s = cinfo->version[VER_DRIVER]) != 0)
877            len += sprintf(page+len, "%-16s %s\n", "ver_driver", s);
878         if ((s = cinfo->version[VER_CARDTYPE]) != 0)
879            len += sprintf(page+len, "%-16s %s\n", "ver_cardtype", s);
880         if ((s = cinfo->version[VER_SERIAL]) != 0)
881            len += sprintf(page+len, "%-16s %s\n", "ver_serial", s);
882
883         if (card->cardtype != avm_m1) {
884                 flag = ((u8 *)(ctrl->profile.manu))[3];
885                 if (flag)
886                         len += sprintf(page+len, "%-16s%s%s%s%s%s%s%s\n",
887                         "protocol",
888                         (flag & 0x01) ? " DSS1" : "",
889                         (flag & 0x02) ? " CT1" : "",
890                         (flag & 0x04) ? " VN3" : "",
891                         (flag & 0x08) ? " NI1" : "",
892                         (flag & 0x10) ? " AUSTEL" : "",
893                         (flag & 0x20) ? " ESS" : "",
894                         (flag & 0x40) ? " 1TR6" : ""
895                         );
896         }
897         if (card->cardtype != avm_m1) {
898                 flag = ((u8 *)(ctrl->profile.manu))[5];
899                 if (flag)
900                         len += sprintf(page+len, "%-16s%s%s%s%s\n",
901                         "linetype",
902                         (flag & 0x01) ? " point to point" : "",
903                         (flag & 0x02) ? " point to multipoint" : "",
904                         (flag & 0x08) ? " leased line without D-channel" : "",
905                         (flag & 0x04) ? " leased line with D-channel" : ""
906                         );
907         }
908         len += sprintf(page+len, "%-16s %s\n", "cardname", cinfo->cardname);
909
910
911         spin_lock_irqsave(&card->lock, flags);
912
913         txoff = (dma_addr_t)b1dma_readl(card, AMCC_TXPTR)-card->dma->sendbuf.dmaaddr;
914         txlen = b1dma_readl(card, AMCC_TXLEN);
915
916         rxoff = (dma_addr_t)b1dma_readl(card, AMCC_RXPTR)-card->dma->recvbuf.dmaaddr;
917         rxlen = b1dma_readl(card, AMCC_RXLEN);
918
919         csr  = b1dma_readl(card, AMCC_INTCSR);
920
921         spin_unlock_irqrestore(&card->lock, flags);
922
923         len += sprintf(page+len, "%-16s 0x%lx\n",
924                                 "csr (cached)", (unsigned long)card->csr);
925         len += sprintf(page+len, "%-16s 0x%lx\n",
926                                 "csr", (unsigned long)csr);
927         len += sprintf(page+len, "%-16s %lu\n",
928                                 "txoff", (unsigned long)txoff);
929         len += sprintf(page+len, "%-16s %lu\n",
930                                 "txlen", (unsigned long)txlen);
931         len += sprintf(page+len, "%-16s %lu\n",
932                                 "rxoff", (unsigned long)rxoff);
933         len += sprintf(page+len, "%-16s %lu\n",
934                                 "rxlen", (unsigned long)rxlen);
935
936         if (off+count >= len)
937            *eof = 1;
938         if (len < off)
939            return 0;
940         *start = page + off;
941         return ((count < len-off) ? count : len-off);
942 }
943
944 /* ------------------------------------------------------------- */
945
946 EXPORT_SYMBOL(b1dma_reset);
947 EXPORT_SYMBOL(t1pci_detect);
948 EXPORT_SYMBOL(b1pciv4_detect);
949 EXPORT_SYMBOL(b1dma_interrupt);
950
951 EXPORT_SYMBOL(b1dma_load_firmware);
952 EXPORT_SYMBOL(b1dma_reset_ctr);
953 EXPORT_SYMBOL(b1dma_register_appl);
954 EXPORT_SYMBOL(b1dma_release_appl);
955 EXPORT_SYMBOL(b1dma_send_message);
956 EXPORT_SYMBOL(b1dmactl_read_proc);
957
958 static int __init b1dma_init(void)
959 {
960         char *p;
961         char rev[32];
962
963         if ((p = strchr(revision, ':')) != 0 && p[1]) {
964                 strlcpy(rev, p + 2, sizeof(rev));
965                 if ((p = strchr(rev, '$')) != 0 && p > rev)
966                    *(p-1) = 0;
967         } else
968                 strcpy(rev, "1.0");
969
970         printk(KERN_INFO "b1dma: revision %s\n", rev);
971
972         return 0;
973 }
974
975 static void __exit b1dma_exit(void)
976 {
977 }
978
979 module_init(b1dma_init);
980 module_exit(b1dma_exit);