1 /* $Id: t1isa.c,v 1.1.2.3 2004/02/10 01:07:12 keil 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 static char *revision
= "$Revision: 1.1.2.3 $";
34 /* ------------------------------------------------------------- */
36 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM T1 HEMA ISA card");
37 MODULE_AUTHOR("Carsten Paeth");
38 MODULE_LICENSE("GPL");
40 /* ------------------------------------------------------------- */
42 static int hema_irq_table
[16] =
61 static int t1_detectandinit(unsigned int base
, unsigned irq
, int cardnr
)
63 unsigned char cregs
[8];
64 unsigned char reverse_cardnr
;
68 reverse_cardnr
= ((cardnr
& 0x01) << 3) | ((cardnr
& 0x02) << 1)
69 | ((cardnr
& 0x04) >> 1) | ((cardnr
& 0x08) >> 3);
70 cregs
[0] = (HEMA_VERSION_ID
<< 4) | (reverse_cardnr
& 0xf);
71 cregs
[1] = 0x00; /* fast & slow link connected to CON1 */
72 cregs
[2] = 0x05; /* fast link 20MBit, slow link 20 MBit */
74 cregs
[4] = 0x11; /* zero wait state */
75 cregs
[5] = hema_irq_table
[irq
& 0xf];
80 * no one else should use the ISA bus in this moment,
81 * but no function there to prevent this :-(
82 * save_flags(flags); cli();
86 t1outp(base
, T1_RESETBOARD
, 0xf);
88 dummy
= t1inp(base
, T1_FASTLINK
+T1_OUTSTAT
); /* first read */
91 dummy
= (base
>> 4) & 0xff;
92 for (i
=1;i
<=0xf;i
++) t1outp(base
, i
, dummy
);
93 t1outp(base
, HEMA_PAL_ID
& 0xf, dummy
);
94 t1outp(base
, HEMA_PAL_ID
>> 4, cregs
[0]);
95 for(i
=1;i
<7;i
++) t1outp(base
, 0, cregs
[i
]);
96 t1outp(base
, ((base
>> 4)) & 0x3, cregs
[7]);
97 /* restore_flags(flags); */
100 t1outp(base
, T1_FASTLINK
+T1_RESETLINK
, 0);
101 t1outp(base
, T1_SLOWLINK
+T1_RESETLINK
, 0);
103 t1outp(base
, T1_FASTLINK
+T1_RESETLINK
, 1);
104 t1outp(base
, T1_SLOWLINK
+T1_RESETLINK
, 1);
106 t1outp(base
, T1_FASTLINK
+T1_RESETLINK
, 0);
107 t1outp(base
, T1_SLOWLINK
+T1_RESETLINK
, 0);
109 t1outp(base
, T1_FASTLINK
+T1_ANALYSE
, 0);
111 t1outp(base
, T1_SLOWLINK
+T1_ANALYSE
, 0);
113 if (t1inp(base
, T1_FASTLINK
+T1_OUTSTAT
) != 0x1) /* tx empty */
115 if (t1inp(base
, T1_FASTLINK
+T1_INSTAT
) != 0x0) /* rx empty */
117 if (t1inp(base
, T1_FASTLINK
+T1_IRQENABLE
) != 0x0)
119 if ((t1inp(base
, T1_FASTLINK
+T1_FIFOSTAT
) & 0xf0) != 0x70)
121 if ((t1inp(base
, T1_FASTLINK
+T1_IRQMASTER
) & 0x0e) != 0)
123 if ((t1inp(base
, T1_FASTLINK
+T1_IDENT
) & 0x7d) != 1)
125 if (t1inp(base
, T1_SLOWLINK
+T1_OUTSTAT
) != 0x1) /* tx empty */
127 if ((t1inp(base
, T1_SLOWLINK
+T1_IRQMASTER
) & 0x0e) != 0)
129 if ((t1inp(base
, T1_SLOWLINK
+T1_IDENT
) & 0x7d) != 0)
134 static irqreturn_t
t1isa_interrupt(int interrupt
, void *devptr
)
136 avmcard
*card
= devptr
;
137 avmctrl_info
*cinfo
= &card
->ctrlinfo
[0];
138 struct capi_ctr
*ctrl
= &cinfo
->capi_ctrl
;
149 spin_lock_irqsave(&card
->lock
, flags
);
151 while (b1_rx_full(card
->port
)) {
153 b1cmd
= b1_get_byte(card
->port
);
157 case RECEIVE_DATA_B3_IND
:
159 ApplId
= (unsigned) b1_get_word(card
->port
);
160 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
161 DataB3Len
= t1_get_slice(card
->port
, card
->databuf
);
162 spin_unlock_irqrestore(&card
->lock
, flags
);
164 if (MsgLen
< 30) { /* not CAPI 64Bit */
165 memset(card
->msgbuf
+MsgLen
, 0, 30-MsgLen
);
167 CAPIMSG_SETLEN(card
->msgbuf
, 30);
169 if (!(skb
= alloc_skb(DataB3Len
+MsgLen
, GFP_ATOMIC
))) {
170 printk(KERN_ERR
"%s: incoming packet dropped\n",
173 memcpy(skb_put(skb
, MsgLen
), card
->msgbuf
, MsgLen
);
174 memcpy(skb_put(skb
, DataB3Len
), card
->databuf
, DataB3Len
);
175 capi_ctr_handle_message(ctrl
, ApplId
, skb
);
179 case RECEIVE_MESSAGE
:
181 ApplId
= (unsigned) b1_get_word(card
->port
);
182 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
183 if (!(skb
= alloc_skb(MsgLen
, GFP_ATOMIC
))) {
184 spin_unlock_irqrestore(&card
->lock
, flags
);
185 printk(KERN_ERR
"%s: incoming packet dropped\n",
188 memcpy(skb_put(skb
, MsgLen
), card
->msgbuf
, MsgLen
);
189 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3
)
190 capilib_data_b3_conf(&cinfo
->ncci_head
, ApplId
,
191 CAPIMSG_NCCI(skb
->data
),
192 CAPIMSG_MSGID(skb
->data
));
193 spin_unlock_irqrestore(&card
->lock
, flags
);
194 capi_ctr_handle_message(ctrl
, ApplId
, skb
);
198 case RECEIVE_NEW_NCCI
:
200 ApplId
= b1_get_word(card
->port
);
201 NCCI
= b1_get_word(card
->port
);
202 WindowSize
= b1_get_word(card
->port
);
203 capilib_new_ncci(&cinfo
->ncci_head
, ApplId
, NCCI
, WindowSize
);
204 spin_unlock_irqrestore(&card
->lock
, flags
);
207 case RECEIVE_FREE_NCCI
:
209 ApplId
= b1_get_word(card
->port
);
210 NCCI
= b1_get_word(card
->port
);
211 if (NCCI
!= 0xffffffff)
212 capilib_free_ncci(&cinfo
->ncci_head
, ApplId
, NCCI
);
213 spin_unlock_irqrestore(&card
->lock
, flags
);
217 b1_put_byte(card
->port
, SEND_POLLACK
);
218 spin_unlock_irqrestore(&card
->lock
, flags
);
219 capi_ctr_resume_output(ctrl
);
223 spin_unlock_irqrestore(&card
->lock
, flags
);
224 capi_ctr_suspend_output(ctrl
);
229 cinfo
->versionlen
= t1_get_slice(card
->port
, cinfo
->versionbuf
);
230 spin_unlock_irqrestore(&card
->lock
, flags
);
231 b1_parse_version(cinfo
);
232 printk(KERN_INFO
"%s: %s-card (%s) now active\n",
234 cinfo
->version
[VER_CARDTYPE
],
235 cinfo
->version
[VER_DRIVER
]);
236 capi_ctr_ready(ctrl
);
239 case RECEIVE_TASK_READY
:
240 ApplId
= (unsigned) b1_get_word(card
->port
);
241 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
242 spin_unlock_irqrestore(&card
->lock
, flags
);
243 card
->msgbuf
[MsgLen
] = 0;
245 && ( card
->msgbuf
[MsgLen
-1] == '\n'
246 || card
->msgbuf
[MsgLen
-1] == '\r')) {
247 card
->msgbuf
[MsgLen
-1] = 0;
250 printk(KERN_INFO
"%s: task %d \"%s\" ready.\n",
251 card
->name
, ApplId
, card
->msgbuf
);
254 case RECEIVE_DEBUGMSG
:
255 MsgLen
= t1_get_slice(card
->port
, card
->msgbuf
);
256 spin_unlock_irqrestore(&card
->lock
, flags
);
257 card
->msgbuf
[MsgLen
] = 0;
259 && ( card
->msgbuf
[MsgLen
-1] == '\n'
260 || card
->msgbuf
[MsgLen
-1] == '\r')) {
261 card
->msgbuf
[MsgLen
-1] = 0;
264 printk(KERN_INFO
"%s: DEBUG: %s\n", card
->name
, card
->msgbuf
);
269 spin_unlock_irqrestore(&card
->lock
, flags
);
270 printk(KERN_ERR
"%s: card reseted ?\n", card
->name
);
273 spin_unlock_irqrestore(&card
->lock
, flags
);
274 printk(KERN_ERR
"%s: b1_interrupt: 0x%x ???\n",
282 /* ------------------------------------------------------------- */
284 static int t1isa_load_firmware(struct capi_ctr
*ctrl
, capiloaddata
*data
)
286 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
287 avmcard
*card
= cinfo
->card
;
288 unsigned int port
= card
->port
;
292 t1_disable_irq(port
);
295 if ((retval
= b1_load_t4file(card
, &data
->firmware
))) {
297 printk(KERN_ERR
"%s: failed to load t4file!!\n",
302 if (data
->configuration
.len
> 0 && data
->configuration
.data
) {
303 if ((retval
= b1_load_config(card
, &data
->configuration
))) {
305 printk(KERN_ERR
"%s: failed to load config!!\n",
311 if (!b1_loaded(card
)) {
312 printk(KERN_ERR
"%s: failed to load t4file.\n", card
->name
);
316 spin_lock_irqsave(&card
->lock
, flags
);
317 b1_setinterrupt(port
, card
->irq
, card
->cardtype
);
318 b1_put_byte(port
, SEND_INIT
);
319 b1_put_word(port
, CAPI_MAXAPPL
);
320 b1_put_word(port
, AVM_NCCI_PER_CHANNEL
*30);
321 b1_put_word(port
, ctrl
->cnr
- 1);
322 spin_unlock_irqrestore(&card
->lock
, flags
);
327 static void t1isa_reset_ctr(struct capi_ctr
*ctrl
)
329 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
330 avmcard
*card
= cinfo
->card
;
331 unsigned int port
= card
->port
;
334 t1_disable_irq(port
);
338 memset(cinfo
->version
, 0, sizeof(cinfo
->version
));
339 spin_lock_irqsave(&card
->lock
, flags
);
340 capilib_release(&cinfo
->ncci_head
);
341 spin_unlock_irqrestore(&card
->lock
, flags
);
345 static void t1isa_remove(struct pci_dev
*pdev
)
347 avmctrl_info
*cinfo
= pci_get_drvdata(pdev
);
355 t1_disable_irq(card
->port
);
356 b1_reset(card
->port
);
357 b1_reset(card
->port
);
358 t1_reset(card
->port
);
360 detach_capi_ctr(&cinfo
->capi_ctrl
);
361 free_irq(card
->irq
, card
);
362 release_region(card
->port
, AVMB1_PORTLEN
);
366 /* ------------------------------------------------------------- */
368 static u16
t1isa_send_message(struct capi_ctr
*ctrl
, struct sk_buff
*skb
);
369 static char *t1isa_procinfo(struct capi_ctr
*ctrl
);
371 static int t1isa_probe(struct pci_dev
*pdev
, int cardnr
)
377 card
= b1_alloc_card(1);
379 printk(KERN_WARNING
"t1isa: no memory.\n");
384 cinfo
= card
->ctrlinfo
;
385 card
->port
= pci_resource_start(pdev
, 0);
386 card
->irq
= pdev
->irq
;
387 card
->cardtype
= avm_t1isa
;
388 card
->cardnr
= cardnr
;
389 sprintf(card
->name
, "t1isa-%x", card
->port
);
391 if (!(((card
->port
& 0x7) == 0) && ((card
->port
& 0x30) != 0x30))) {
392 printk(KERN_WARNING
"t1isa: invalid port 0x%x.\n", card
->port
);
396 if (hema_irq_table
[card
->irq
& 0xf] == 0) {
397 printk(KERN_WARNING
"t1isa: irq %d not valid.\n", card
->irq
);
401 if (!request_region(card
->port
, AVMB1_PORTLEN
, card
->name
)) {
402 printk(KERN_INFO
"t1isa: ports 0x%03x-0x%03x in use.\n",
403 card
->port
, card
->port
+ AVMB1_PORTLEN
);
407 retval
= request_irq(card
->irq
, t1isa_interrupt
, 0, card
->name
, card
);
409 printk(KERN_INFO
"t1isa: unable to get IRQ %d.\n", card
->irq
);
411 goto err_release_region
;
414 if ((retval
= t1_detectandinit(card
->port
, card
->irq
, card
->cardnr
)) != 0) {
415 printk(KERN_INFO
"t1isa: NO card at 0x%x (%d)\n",
420 t1_disable_irq(card
->port
);
421 b1_reset(card
->port
);
423 cinfo
->capi_ctrl
.owner
= THIS_MODULE
;
424 cinfo
->capi_ctrl
.driver_name
= "t1isa";
425 cinfo
->capi_ctrl
.driverdata
= cinfo
;
426 cinfo
->capi_ctrl
.register_appl
= b1_register_appl
;
427 cinfo
->capi_ctrl
.release_appl
= b1_release_appl
;
428 cinfo
->capi_ctrl
.send_message
= t1isa_send_message
;
429 cinfo
->capi_ctrl
.load_firmware
= t1isa_load_firmware
;
430 cinfo
->capi_ctrl
.reset_ctr
= t1isa_reset_ctr
;
431 cinfo
->capi_ctrl
.procinfo
= t1isa_procinfo
;
432 cinfo
->capi_ctrl
.proc_fops
= &b1ctl_proc_fops
;
433 strcpy(cinfo
->capi_ctrl
.name
, card
->name
);
435 retval
= attach_capi_ctr(&cinfo
->capi_ctrl
);
437 printk(KERN_INFO
"t1isa: attach controller failed.\n");
441 printk(KERN_INFO
"t1isa: AVM T1 ISA at i/o %#x, irq %d, card %d\n",
442 card
->port
, card
->irq
, card
->cardnr
);
444 pci_set_drvdata(pdev
, cinfo
);
448 free_irq(card
->irq
, card
);
450 release_region(card
->port
, AVMB1_PORTLEN
);
457 static u16
t1isa_send_message(struct capi_ctr
*ctrl
, struct sk_buff
*skb
)
459 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
460 avmcard
*card
= cinfo
->card
;
461 unsigned int port
= card
->port
;
463 u16 len
= CAPIMSG_LEN(skb
->data
);
464 u8 cmd
= CAPIMSG_COMMAND(skb
->data
);
465 u8 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
468 spin_lock_irqsave(&card
->lock
, flags
);
469 if (CAPICMD(cmd
, subcmd
) == CAPI_DATA_B3_REQ
) {
470 retval
= capilib_data_b3_req(&cinfo
->ncci_head
,
471 CAPIMSG_APPID(skb
->data
),
472 CAPIMSG_NCCI(skb
->data
),
473 CAPIMSG_MSGID(skb
->data
));
474 if (retval
!= CAPI_NOERROR
) {
475 spin_unlock_irqrestore(&card
->lock
, flags
);
478 dlen
= CAPIMSG_DATALEN(skb
->data
);
480 b1_put_byte(port
, SEND_DATA_B3_REQ
);
481 t1_put_slice(port
, skb
->data
, len
);
482 t1_put_slice(port
, skb
->data
+ len
, dlen
);
484 b1_put_byte(port
, SEND_MESSAGE
);
485 t1_put_slice(port
, skb
->data
, len
);
487 spin_unlock_irqrestore(&card
->lock
, flags
);
488 dev_kfree_skb_any(skb
);
491 /* ------------------------------------------------------------- */
493 static char *t1isa_procinfo(struct capi_ctr
*ctrl
)
495 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
499 sprintf(cinfo
->infobuf
, "%s %s 0x%x %d %d",
500 cinfo
->cardname
[0] ? cinfo
->cardname
: "-",
501 cinfo
->version
[VER_DRIVER
] ? cinfo
->version
[VER_DRIVER
] : "-",
502 cinfo
->card
? cinfo
->card
->port
: 0x0,
503 cinfo
->card
? cinfo
->card
->irq
: 0,
504 cinfo
->card
? cinfo
->card
->cardnr
: 0
506 return cinfo
->infobuf
;
510 /* ------------------------------------------------------------- */
513 static struct pci_dev isa_dev
[MAX_CARDS
];
514 static int io
[MAX_CARDS
];
515 static int irq
[MAX_CARDS
];
516 static int cardnr
[MAX_CARDS
];
518 module_param_array(io
, int, NULL
, 0);
519 module_param_array(irq
, int, NULL
, 0);
520 module_param_array(cardnr
, int, NULL
, 0);
521 MODULE_PARM_DESC(io
, "I/O base address(es)");
522 MODULE_PARM_DESC(irq
, "IRQ number(s) (assigned)");
523 MODULE_PARM_DESC(cardnr
, "Card number(s) (as jumpered)");
525 static int t1isa_add_card(struct capi_driver
*driver
, capicardparams
*data
)
529 for (i
= 0; i
< MAX_CARDS
; i
++) {
530 if (isa_dev
[i
].resource
[0].start
)
533 isa_dev
[i
].resource
[0].start
= data
->port
;
534 isa_dev
[i
].irq
= data
->irq
;
536 if (t1isa_probe(&isa_dev
[i
], data
->cardnr
) == 0)
542 static struct capi_driver capi_driver_t1isa
= {
545 .add_card
= t1isa_add_card
,
548 static int __init
t1isa_init(void)
554 if ((p
= strchr(revision
, ':')) != NULL
&& p
[1]) {
555 strlcpy(rev
, p
+ 2, 32);
556 if ((p
= strchr(rev
, '$')) != NULL
&& p
> rev
)
561 for (i
= 0; i
< MAX_CARDS
; i
++) {
565 isa_dev
[i
].resource
[0].start
= io
[i
];
566 isa_dev
[i
].irq
= irq
[i
];
568 if (t1isa_probe(&isa_dev
[i
], cardnr
[i
]) != 0)
572 strlcpy(capi_driver_t1isa
.revision
, rev
, 32);
573 register_capi_driver(&capi_driver_t1isa
);
574 printk(KERN_INFO
"t1isa: revision %s\n", rev
);
579 static void __exit
t1isa_exit(void)
583 unregister_capi_driver(&capi_driver_t1isa
);
584 for (i
= 0; i
< MAX_CARDS
; i
++) {
588 t1isa_remove(&isa_dev
[i
]);
592 module_init(t1isa_init
);
593 module_exit(t1isa_exit
);