1 /* $Id: t1isa.c,v 1.16.6.7 2001/09/23 22:24:34 kai Exp $
3 * Module for AVM T1 HEMA-card.
5 * Copyright 1999 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/capi.h>
20 #include <linux/netdevice.h>
21 #include <linux/kernelcapi.h>
22 #include <linux/init.h>
23 #include <linux/pci.h>
25 #include <linux/isdn/capicmd.h>
26 #include <linux/isdn/capiutil.h>
27 #include <linux/isdn/capilli.h>
30 /* ------------------------------------------------------------- */
32 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM T1 HEMA ISA card");
33 MODULE_AUTHOR("Carsten Paeth");
34 MODULE_LICENSE("GPL");
36 /* ------------------------------------------------------------- */
38 static int hema_irq_table
[16] =
57 static int t1_detectandinit(unsigned int base
, unsigned irq
, int cardnr
)
59 unsigned char cregs
[8];
60 unsigned char reverse_cardnr
;
65 reverse_cardnr
= ((cardnr
& 0x01) << 3) | ((cardnr
& 0x02) << 1)
66 | ((cardnr
& 0x04) >> 1) | ((cardnr
& 0x08) >> 3);
67 cregs
[0] = (HEMA_VERSION_ID
<< 4) | (reverse_cardnr
& 0xf);
68 cregs
[1] = 0x00; /* fast & slow link connected to CON1 */
69 cregs
[2] = 0x05; /* fast link 20MBit, slow link 20 MBit */
71 cregs
[4] = 0x11; /* zero wait state */
72 cregs
[5] = hema_irq_table
[irq
& 0xf];
79 t1outp(base
, T1_RESETBOARD
, 0xf);
81 dummy
= t1inp(base
, T1_FASTLINK
+T1_OUTSTAT
); /* first read */
84 dummy
= (base
>> 4) & 0xff;
85 for (i
=1;i
<=0xf;i
++) t1outp(base
, i
, dummy
);
86 t1outp(base
, HEMA_PAL_ID
& 0xf, dummy
);
87 t1outp(base
, HEMA_PAL_ID
>> 4, cregs
[0]);
88 for(i
=1;i
<7;i
++) t1outp(base
, 0, cregs
[i
]);
89 t1outp(base
, ((base
>> 4)) & 0x3, cregs
[7]);
93 t1outp(base
, T1_FASTLINK
+T1_RESETLINK
, 0);
94 t1outp(base
, T1_SLOWLINK
+T1_RESETLINK
, 0);
96 t1outp(base
, T1_FASTLINK
+T1_RESETLINK
, 1);
97 t1outp(base
, T1_SLOWLINK
+T1_RESETLINK
, 1);
99 t1outp(base
, T1_FASTLINK
+T1_RESETLINK
, 0);
100 t1outp(base
, T1_SLOWLINK
+T1_RESETLINK
, 0);
102 t1outp(base
, T1_FASTLINK
+T1_ANALYSE
, 0);
104 t1outp(base
, T1_SLOWLINK
+T1_ANALYSE
, 0);
106 if (t1inp(base
, T1_FASTLINK
+T1_OUTSTAT
) != 0x1) /* tx empty */
108 if (t1inp(base
, T1_FASTLINK
+T1_INSTAT
) != 0x0) /* rx empty */
110 if (t1inp(base
, T1_FASTLINK
+T1_IRQENABLE
) != 0x0)
112 if ((t1inp(base
, T1_FASTLINK
+T1_FIFOSTAT
) & 0xf0) != 0x70)
114 if ((t1inp(base
, T1_FASTLINK
+T1_IRQMASTER
) & 0x0e) != 0)
116 if ((t1inp(base
, T1_FASTLINK
+T1_IDENT
) & 0x7d) != 1)
118 if (t1inp(base
, T1_SLOWLINK
+T1_OUTSTAT
) != 0x1) /* tx empty */
120 if ((t1inp(base
, T1_SLOWLINK
+T1_IRQMASTER
) & 0x0e) != 0)
122 if ((t1inp(base
, T1_SLOWLINK
+T1_IDENT
) & 0x7d) != 0)
127 static irqreturn_t
t1isa_interrupt(int interrupt
, void *devptr
, struct pt_regs
*regs
)
129 avmcard
*card
= devptr
;
130 avmctrl_info
*cinfo
= &card
->ctrlinfo
[0];
131 struct capi_ctr
*ctrl
= &cinfo
->capi_ctrl
;
141 while (b1_rx_full(card
->port
)) {
143 b1cmd
= b1_get_byte(card
->port
);
147 case RECEIVE_DATA_B3_IND
:
149 ApplId
= (unsigned) b1_get_word(card
->port
);
150 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
151 DataB3Len
= t1_get_slice(card
->port
, card
->databuf
);
153 if (MsgLen
< 30) { /* not CAPI 64Bit */
154 memset(card
->msgbuf
+MsgLen
, 0, 30-MsgLen
);
156 CAPIMSG_SETLEN(card
->msgbuf
, 30);
158 if (!(skb
= alloc_skb(DataB3Len
+MsgLen
, GFP_ATOMIC
))) {
159 printk(KERN_ERR
"%s: incoming packet dropped\n",
162 memcpy(skb_put(skb
, MsgLen
), card
->msgbuf
, MsgLen
);
163 memcpy(skb_put(skb
, DataB3Len
), card
->databuf
, DataB3Len
);
164 capi_ctr_handle_message(ctrl
, ApplId
, skb
);
168 case RECEIVE_MESSAGE
:
170 ApplId
= (unsigned) b1_get_word(card
->port
);
171 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
172 if (!(skb
= alloc_skb(MsgLen
, GFP_ATOMIC
))) {
173 printk(KERN_ERR
"%s: incoming packet dropped\n",
176 memcpy(skb_put(skb
, MsgLen
), card
->msgbuf
, MsgLen
);
177 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3
)
178 capilib_data_b3_conf(&cinfo
->ncci_head
, ApplId
,
179 CAPIMSG_NCCI(skb
->data
),
180 CAPIMSG_MSGID(skb
->data
));
182 capi_ctr_handle_message(ctrl
, ApplId
, skb
);
186 case RECEIVE_NEW_NCCI
:
188 ApplId
= b1_get_word(card
->port
);
189 NCCI
= b1_get_word(card
->port
);
190 WindowSize
= b1_get_word(card
->port
);
192 capilib_new_ncci(&cinfo
->ncci_head
, ApplId
, NCCI
, WindowSize
);
196 case RECEIVE_FREE_NCCI
:
198 ApplId
= b1_get_word(card
->port
);
199 NCCI
= b1_get_word(card
->port
);
201 if (NCCI
!= 0xffffffff)
202 capilib_free_ncci(&cinfo
->ncci_head
, ApplId
, NCCI
);
207 b1_put_byte(card
->port
, SEND_POLLACK
);
208 capi_ctr_resume_output(ctrl
);
212 capi_ctr_suspend_output(ctrl
);
217 cinfo
->versionlen
= t1_get_slice(card
->port
, cinfo
->versionbuf
);
218 b1_parse_version(cinfo
);
219 printk(KERN_INFO
"%s: %s-card (%s) now active\n",
221 cinfo
->version
[VER_CARDTYPE
],
222 cinfo
->version
[VER_DRIVER
]);
223 capi_ctr_ready(ctrl
);
226 case RECEIVE_TASK_READY
:
227 ApplId
= (unsigned) b1_get_word(card
->port
);
228 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
229 card
->msgbuf
[MsgLen
] = 0;
231 && ( card
->msgbuf
[MsgLen
-1] == '\n'
232 || card
->msgbuf
[MsgLen
-1] == '\r')) {
233 card
->msgbuf
[MsgLen
-1] = 0;
236 printk(KERN_INFO
"%s: task %d \"%s\" ready.\n",
237 card
->name
, ApplId
, card
->msgbuf
);
240 case RECEIVE_DEBUGMSG
:
241 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
242 card
->msgbuf
[MsgLen
] = 0;
244 && ( card
->msgbuf
[MsgLen
-1] == '\n'
245 || card
->msgbuf
[MsgLen
-1] == '\r')) {
246 card
->msgbuf
[MsgLen
-1] = 0;
249 printk(KERN_INFO
"%s: DEBUG: %s\n", card
->name
, card
->msgbuf
);
254 printk(KERN_ERR
"%s: card reseted ?\n", card
->name
);
257 printk(KERN_ERR
"%s: b1_interrupt: 0x%x ???\n",
265 /* ------------------------------------------------------------- */
267 static int t1isa_load_firmware(struct capi_ctr
*ctrl
, capiloaddata
*data
)
269 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
270 avmcard
*card
= cinfo
->card
;
271 unsigned int port
= card
->port
;
275 t1_disable_irq(port
);
278 if ((retval
= b1_load_t4file(card
, &data
->firmware
))) {
280 printk(KERN_ERR
"%s: failed to load t4file!!\n",
285 if (data
->configuration
.len
> 0 && data
->configuration
.data
) {
286 if ((retval
= b1_load_config(card
, &data
->configuration
))) {
288 printk(KERN_ERR
"%s: failed to load config!!\n",
294 if (!b1_loaded(card
)) {
295 printk(KERN_ERR
"%s: failed to load t4file.\n", card
->name
);
301 b1_setinterrupt(port
, card
->irq
, card
->cardtype
);
302 b1_put_byte(port
, SEND_INIT
);
303 b1_put_word(port
, CAPI_MAXAPPL
);
304 b1_put_word(port
, AVM_NCCI_PER_CHANNEL
*30);
305 b1_put_word(port
, ctrl
->cnr
- 1);
306 restore_flags(flags
);
311 void t1isa_reset_ctr(struct capi_ctr
*ctrl
)
313 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
314 avmcard
*card
= cinfo
->card
;
315 unsigned int port
= card
->port
;
317 t1_disable_irq(port
);
321 memset(cinfo
->version
, 0, sizeof(cinfo
->version
));
322 capilib_release(&cinfo
->ncci_head
);
323 capi_ctr_reseted(ctrl
);
326 static void t1isa_remove(struct pci_dev
*pdev
)
328 avmctrl_info
*cinfo
= pci_get_drvdata(pdev
);
336 t1_disable_irq(card
->port
);
337 b1_reset(card
->port
);
338 b1_reset(card
->port
);
339 t1_reset(card
->port
);
341 detach_capi_ctr(&cinfo
->capi_ctrl
);
342 free_irq(card
->irq
, card
);
343 release_region(card
->port
, AVMB1_PORTLEN
);
347 /* ------------------------------------------------------------- */
349 static u16
t1isa_send_message(struct capi_ctr
*ctrl
, struct sk_buff
*skb
);
350 static char *t1isa_procinfo(struct capi_ctr
*ctrl
);
352 static int __init
t1isa_probe(struct pci_dev
*pdev
, int cardnr
)
358 card
= b1_alloc_card(1);
360 printk(KERN_WARNING
"t1isa: no memory.\n");
365 cinfo
= card
->ctrlinfo
;
366 card
->port
= pci_resource_start(pdev
, 0);
367 card
->irq
= pdev
->irq
;
368 card
->cardtype
= avm_t1isa
;
369 card
->cardnr
= cardnr
;
370 sprintf(card
->name
, "t1isa-%x", card
->port
);
372 if (!(((card
->port
& 0x7) == 0) && ((card
->port
& 0x30) != 0x30))) {
373 printk(KERN_WARNING
"t1isa: invalid port 0x%x.\n", card
->port
);
377 if (hema_irq_table
[card
->irq
& 0xf] == 0) {
378 printk(KERN_WARNING
"t1isa: irq %d not valid.\n", card
->irq
);
382 if (!request_region(card
->port
, AVMB1_PORTLEN
, card
->name
)) {
383 printk(KERN_INFO
"t1isa: ports 0x%03x-0x%03x in use.\n",
384 card
->port
, card
->port
+ AVMB1_PORTLEN
);
388 retval
= request_irq(card
->irq
, t1isa_interrupt
, 0, card
->name
, card
);
390 printk(KERN_INFO
"t1isa: unable to get IRQ %d.\n", card
->irq
);
392 goto err_release_region
;
395 if ((retval
= t1_detectandinit(card
->port
, card
->irq
, card
->cardnr
)) != 0) {
396 printk(KERN_INFO
"t1isa: NO card at 0x%x (%d)\n",
401 t1_disable_irq(card
->port
);
402 b1_reset(card
->port
);
404 cinfo
->capi_ctrl
.driver_name
= "t1isa";
405 cinfo
->capi_ctrl
.driverdata
= cinfo
;
406 cinfo
->capi_ctrl
.register_appl
= b1_register_appl
;
407 cinfo
->capi_ctrl
.release_appl
= b1_release_appl
;
408 cinfo
->capi_ctrl
.send_message
= t1isa_send_message
;
409 cinfo
->capi_ctrl
.load_firmware
= t1isa_load_firmware
;
410 cinfo
->capi_ctrl
.reset_ctr
= t1isa_reset_ctr
;
411 cinfo
->capi_ctrl
.procinfo
= t1isa_procinfo
;
412 cinfo
->capi_ctrl
.ctr_read_proc
= b1ctl_read_proc
;
413 strcpy(cinfo
->capi_ctrl
.name
, card
->name
);
414 cinfo
->capi_ctrl
.owner
= THIS_MODULE
;
416 retval
= attach_capi_ctr(&cinfo
->capi_ctrl
);
418 printk(KERN_INFO
"t1isa: attach controller failed.\n");
422 printk(KERN_INFO
"t1isa: AVM T1 ISA at i/o %#x, irq %d, card %d\n",
423 card
->port
, card
->irq
, card
->cardnr
);
425 pci_set_drvdata(pdev
, cinfo
);
429 free_irq(card
->irq
, card
);
431 release_region(card
->port
, AVMB1_PORTLEN
);
438 static u16
t1isa_send_message(struct capi_ctr
*ctrl
, struct sk_buff
*skb
)
440 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
441 avmcard
*card
= cinfo
->card
;
442 unsigned int port
= card
->port
;
444 u16 len
= CAPIMSG_LEN(skb
->data
);
445 u8 cmd
= CAPIMSG_COMMAND(skb
->data
);
446 u8 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
449 if (CAPICMD(cmd
, subcmd
) == CAPI_DATA_B3_REQ
) {
450 retval
= capilib_data_b3_req(&cinfo
->ncci_head
,
451 CAPIMSG_APPID(skb
->data
),
452 CAPIMSG_NCCI(skb
->data
),
453 CAPIMSG_MSGID(skb
->data
));
454 if (retval
!= CAPI_NOERROR
)
457 dlen
= CAPIMSG_DATALEN(skb
->data
);
461 b1_put_byte(port
, SEND_DATA_B3_REQ
);
462 t1_put_slice(port
, skb
->data
, len
);
463 t1_put_slice(port
, skb
->data
+ len
, dlen
);
464 restore_flags(flags
);
466 retval
= CAPI_NOERROR
;
470 b1_put_byte(port
, SEND_MESSAGE
);
471 t1_put_slice(port
, skb
->data
, len
);
472 restore_flags(flags
);
475 dev_kfree_skb_any(skb
);
478 /* ------------------------------------------------------------- */
480 static char *t1isa_procinfo(struct capi_ctr
*ctrl
)
482 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
486 sprintf(cinfo
->infobuf
, "%s %s 0x%x %d %d",
487 cinfo
->cardname
[0] ? cinfo
->cardname
: "-",
488 cinfo
->version
[VER_DRIVER
] ? cinfo
->version
[VER_DRIVER
] : "-",
489 cinfo
->card
? cinfo
->card
->port
: 0x0,
490 cinfo
->card
? cinfo
->card
->irq
: 0,
491 cinfo
->card
? cinfo
->card
->cardnr
: 0
493 return cinfo
->infobuf
;
497 /* ------------------------------------------------------------- */
500 static struct pci_dev isa_dev
[MAX_CARDS
];
501 static int io
[MAX_CARDS
];
502 static int irq
[MAX_CARDS
];
503 static int cardnr
[MAX_CARDS
];
505 MODULE_PARM(io
, "1-" __MODULE_STRING(MAX_CARDS
) "i");
506 MODULE_PARM(irq
, "1-" __MODULE_STRING(MAX_CARDS
) "i");
507 MODULE_PARM(cardnr
, "1-" __MODULE_STRING(MAX_CARDS
) "i");
508 MODULE_PARM_DESC(io
, "I/O base address(es)");
509 MODULE_PARM_DESC(irq
, "IRQ number(s) (assigned)");
510 MODULE_PARM_DESC(cardnr
, "Card number(s) (as jumpered)");
512 static int __init
t1isa_init(void)
517 for (i
= 0; i
< MAX_CARDS
; i
++) {
521 isa_dev
[i
].resource
[0].start
= io
[i
];
522 isa_dev
[i
].irq_resource
[0].start
= irq
[i
];
524 if (t1isa_probe(&isa_dev
[i
], cardnr
[i
]) == 0)
533 static void __exit
t1isa_exit(void)
537 for (i
= 0; i
< MAX_CARDS
; i
++) {
541 t1isa_remove(&isa_dev
[i
]);
545 module_init(t1isa_init
);
546 module_exit(t1isa_exit
);