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