1 /* $Id: icn.c,v 1.65.6.8 2001/09/23 22:24:55 kai Exp $
3 * ISDN low-level module for the ICN active ISDN-Card.
5 * Copyright 1994,95,96 by Fritz Elfert (fritz@isdn4linux.de)
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
18 static int portbase
= ICN_BASEADDR
;
19 static unsigned long membase
= ICN_MEMADDR
;
20 static char *icn_id
= "\0";
21 static char *icn_id2
= "\0";
23 MODULE_DESCRIPTION("ISDN4Linux: Driver for ICN active ISDN card");
24 MODULE_AUTHOR("Fritz Elfert");
25 MODULE_LICENSE("GPL");
26 module_param(portbase
, int, 0);
27 MODULE_PARM_DESC(portbase
, "Port address of first card");
28 module_param(membase
, ulong
, 0);
29 MODULE_PARM_DESC(membase
, "Shared memory address of all cards");
30 module_param(icn_id
, charp
, 0);
31 MODULE_PARM_DESC(icn_id
, "ID-String of first card");
32 module_param(icn_id2
, charp
, 0);
33 MODULE_PARM_DESC(icn_id2
, "ID-String of first card, second S0 (4B only)");
36 * Verbose bootcode- and protocol-downloading.
41 * Verbose Shmem-Mapping.
46 *revision
= "$Revision: 1.65.6.8 $";
48 static int icn_addcard(int, char *, char *);
51 * Free send-queue completely.
53 * card = pointer to card struct
54 * channel = channel number
57 icn_free_queue(icn_card
*card
, int channel
)
59 struct sk_buff_head
*queue
= &card
->spqueue
[channel
];
62 skb_queue_purge(queue
);
63 card
->xlen
[channel
] = 0;
64 card
->sndcount
[channel
] = 0;
65 if ((skb
= card
->xskb
[channel
])) {
66 card
->xskb
[channel
] = NULL
;
71 /* Put a value into a shift-register, highest bit first.
73 * port = port for output (bit 0 is significant)
74 * val = value to be output
75 * firstbit = Bit-Number of highest bit
76 * bitcount = Number of bits to output
79 icn_shiftout(unsigned short port
,
88 for (s
= firstbit
, c
= bitcount
; c
> 0; s
--, c
--)
89 OUTB_P((u_char
) ((val
>> s
) & 1) ? 0xff : 0, port
);
93 * disable a cards shared memory
96 icn_disable_ram(icn_card
*card
)
98 OUTB_P(0, ICN_MAPRAM
);
102 * enable a cards shared memory
105 icn_enable_ram(icn_card
*card
)
107 OUTB_P(0xff, ICN_MAPRAM
);
111 * Map a cards channel0 (Bank0/Bank8) or channel1 (Bank4/Bank12)
113 * must called with holding the devlock
116 icn_map_channel(icn_card
*card
, int channel
)
119 printk(KERN_DEBUG
"icn_map_channel %d %d\n", dev
.channel
, channel
);
121 if ((channel
== dev
.channel
) && (card
== dev
.mcard
))
124 icn_disable_ram(dev
.mcard
);
125 icn_shiftout(ICN_BANK
, chan2bank
[channel
], 3, 4); /* Select Bank */
126 icn_enable_ram(card
);
128 dev
.channel
= channel
;
130 printk(KERN_DEBUG
"icn_map_channel done\n");
135 * Lock a cards channel.
136 * Return 0 if requested card/channel is unmapped (failure).
137 * Return 1 on success.
139 * must called with holding the devlock
142 icn_lock_channel(icn_card
*card
, int channel
)
147 printk(KERN_DEBUG
"icn_lock_channel %d\n", channel
);
149 if ((dev
.channel
== channel
) && (card
== dev
.mcard
)) {
153 printk(KERN_DEBUG
"icn_lock_channel %d OK\n", channel
);
158 printk(KERN_DEBUG
"icn_lock_channel %d FAILED, dc=%d\n", channel
, dev
.channel
);
165 * Release current card/channel lock
167 * must called with holding the devlock
170 __icn_release_channel(void)
173 printk(KERN_DEBUG
"icn_release_channel l=%d\n", dev
.chanlock
);
175 if (dev
.chanlock
> 0)
180 * Release current card/channel lock
183 icn_release_channel(void)
187 spin_lock_irqsave(&dev
.devlock
, flags
);
188 __icn_release_channel();
189 spin_unlock_irqrestore(&dev
.devlock
, flags
);
193 * Try to map and lock a cards channel.
194 * Return 1 on success, 0 on failure.
197 icn_trymaplock_channel(icn_card
*card
, int channel
)
202 printk(KERN_DEBUG
"trymaplock c=%d dc=%d l=%d\n", channel
, dev
.channel
,
205 spin_lock_irqsave(&dev
.devlock
, flags
);
206 if ((!dev
.chanlock
) ||
207 ((dev
.channel
== channel
) && (dev
.mcard
== card
))) {
209 icn_map_channel(card
, channel
);
210 spin_unlock_irqrestore(&dev
.devlock
, flags
);
212 printk(KERN_DEBUG
"trymaplock %d OK\n", channel
);
216 spin_unlock_irqrestore(&dev
.devlock
, flags
);
218 printk(KERN_DEBUG
"trymaplock %d FAILED\n", channel
);
224 * Release current card/channel lock,
225 * then map same or other channel without locking.
228 icn_maprelease_channel(icn_card
*card
, int channel
)
233 printk(KERN_DEBUG
"map_release c=%d l=%d\n", channel
, dev
.chanlock
);
235 spin_lock_irqsave(&dev
.devlock
, flags
);
236 if (dev
.chanlock
> 0)
239 icn_map_channel(card
, channel
);
240 spin_unlock_irqrestore(&dev
.devlock
, flags
);
243 /* Get Data from the B-Channel, assemble fragmented packets and put them
244 * into receive-queue. Wake up any B-Channel-reading processes.
245 * This routine is called via timer-callback from icn_pollbchan().
249 icn_pollbchan_receive(int channel
, icn_card
*card
)
251 int mch
= channel
+ ((card
->secondhalf
) ? 2 : 0);
256 if (icn_trymaplock_channel(card
, mch
)) {
258 cnt
= readb(&rbuf_l
);
259 if ((card
->rcvidx
[channel
] + cnt
) > 4000) {
261 "icn: (%s) bogus packet on ch%d, dropping.\n",
264 card
->rcvidx
[channel
] = 0;
267 memcpy_fromio(&card
->rcvbuf
[channel
][card
->rcvidx
[channel
]],
269 card
->rcvidx
[channel
] += cnt
;
270 eflag
= readb(&rbuf_f
);
273 icn_maprelease_channel(card
, mch
& 2);
275 if ((cnt
= card
->rcvidx
[channel
])) {
276 if (!(skb
= dev_alloc_skb(cnt
))) {
277 printk(KERN_WARNING
"icn: receive out of memory\n");
280 memcpy(skb_put(skb
, cnt
), card
->rcvbuf
[channel
], cnt
);
281 card
->rcvidx
[channel
] = 0;
282 card
->interface
.rcvcallb_skb(card
->myid
, channel
, skb
);
285 if (!icn_trymaplock_channel(card
, mch
))
288 icn_maprelease_channel(card
, mch
& 2);
292 /* Send data-packet to B-Channel, split it up into fragments of
293 * ICN_FRAGSIZE length. If last fragment is sent out, signal
294 * success to upper layers via statcallb with ISDN_STAT_BSENT argument.
295 * This routine is called via timer-callback from icn_pollbchan() or
296 * directly from icn_sendbuf().
300 icn_pollbchan_send(int channel
, icn_card
*card
)
302 int mch
= channel
+ ((card
->secondhalf
) ? 2 : 0);
308 if (!(card
->sndcount
[channel
] || card
->xskb
[channel
] ||
309 !skb_queue_empty(&card
->spqueue
[channel
])))
311 if (icn_trymaplock_channel(card
, mch
)) {
313 (card
->sndcount
[channel
] ||
314 !skb_queue_empty(&card
->spqueue
[channel
]) ||
315 card
->xskb
[channel
])) {
316 spin_lock_irqsave(&card
->lock
, flags
);
317 if (card
->xmit_lock
[channel
]) {
318 spin_unlock_irqrestore(&card
->lock
, flags
);
321 card
->xmit_lock
[channel
]++;
322 spin_unlock_irqrestore(&card
->lock
, flags
);
323 skb
= card
->xskb
[channel
];
325 skb
= skb_dequeue(&card
->spqueue
[channel
]);
327 /* Pop ACK-flag off skb.
328 * Store length to xlen.
330 if (*(skb_pull(skb
, 1)))
331 card
->xlen
[channel
] = skb
->len
;
333 card
->xlen
[channel
] = 0;
338 if (skb
->len
> ICN_FRAGSIZE
) {
339 writeb(0xff, &sbuf_f
);
342 writeb(0x0, &sbuf_f
);
345 writeb(cnt
, &sbuf_l
);
346 memcpy_toio(&sbuf_d
, skb
->data
, cnt
);
348 sbnext
; /* switch to next buffer */
349 icn_maprelease_channel(card
, mch
& 2);
350 spin_lock_irqsave(&card
->lock
, flags
);
351 card
->sndcount
[channel
] -= cnt
;
353 if (card
->xskb
[channel
])
354 card
->xskb
[channel
] = NULL
;
355 card
->xmit_lock
[channel
] = 0;
356 spin_unlock_irqrestore(&card
->lock
, flags
);
358 if (card
->xlen
[channel
]) {
359 cmd
.command
= ISDN_STAT_BSENT
;
360 cmd
.driver
= card
->myid
;
362 cmd
.parm
.length
= card
->xlen
[channel
];
363 card
->interface
.statcallb(&cmd
);
366 card
->xskb
[channel
] = skb
;
367 card
->xmit_lock
[channel
] = 0;
368 spin_unlock_irqrestore(&card
->lock
, flags
);
370 if (!icn_trymaplock_channel(card
, mch
))
373 icn_maprelease_channel(card
, mch
& 2);
377 /* Send/Receive Data to/from the B-Channel.
378 * This routine is called via timer-callback.
379 * It schedules itself while any B-Channel is open.
383 icn_pollbchan(unsigned long data
)
385 icn_card
*card
= (icn_card
*) data
;
388 if (card
->flags
& ICN_FLAGS_B1ACTIVE
) {
389 icn_pollbchan_receive(0, card
);
390 icn_pollbchan_send(0, card
);
392 if (card
->flags
& ICN_FLAGS_B2ACTIVE
) {
393 icn_pollbchan_receive(1, card
);
394 icn_pollbchan_send(1, card
);
396 if (card
->flags
& (ICN_FLAGS_B1ACTIVE
| ICN_FLAGS_B2ACTIVE
)) {
397 /* schedule b-channel polling again */
398 spin_lock_irqsave(&card
->lock
, flags
);
399 mod_timer(&card
->rb_timer
, jiffies
+ ICN_TIMER_BCREAD
);
400 card
->flags
|= ICN_FLAGS_RBTIMER
;
401 spin_unlock_irqrestore(&card
->lock
, flags
);
403 card
->flags
&= ~ICN_FLAGS_RBTIMER
;
406 typedef struct icn_stat
{
412 static icn_stat icn_stat_table
[] =
414 {"BCON_", ISDN_STAT_BCONN
, 1}, /* B-Channel connected */
415 {"BDIS_", ISDN_STAT_BHUP
, 2}, /* B-Channel disconnected */
417 ** add d-channel connect and disconnect support to link-level
419 {"DCON_", ISDN_STAT_DCONN
, 10}, /* D-Channel connected */
420 {"DDIS_", ISDN_STAT_DHUP
, 11}, /* D-Channel disconnected */
421 {"DCAL_I", ISDN_STAT_ICALL
, 3}, /* Incoming call dialup-line */
422 {"DSCA_I", ISDN_STAT_ICALL
, 3}, /* Incoming call 1TR6-SPV */
423 {"FCALL", ISDN_STAT_ICALL
, 4}, /* Leased line connection up */
424 {"CIF", ISDN_STAT_CINF
, 5}, /* Charge-info, 1TR6-type */
425 {"AOC", ISDN_STAT_CINF
, 6}, /* Charge-info, DSS1-type */
426 {"CAU", ISDN_STAT_CAUSE
, 7}, /* Cause code */
427 {"TEI OK", ISDN_STAT_RUN
, 0}, /* Card connected to wallplug */
428 {"E_L1: ACT FAIL", ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
429 {"E_L2: DATA LIN", ISDN_STAT_BHUP
, 8}, /* Layer-2 data link lost */
430 {"E_L1: ACTIVATION FAILED",
431 ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
438 * Check Statusqueue-Pointer from isdn-cards.
439 * If there are new status-replies from the interface, check
440 * them against B-Channel-connects/disconnects and set flags accordingly.
441 * Wake-Up any processes, who are reading the status-device.
442 * If there are B-Channels open, initiate a timer-callback to
444 * This routine is called periodically via timer.
448 icn_parse_status(u_char
*status
, int channel
, icn_card
*card
)
450 icn_stat
*s
= icn_stat_table
;
456 if (!strncmp(status
, s
->statstr
, strlen(s
->statstr
))) {
457 cmd
.command
= s
->command
;
465 cmd
.driver
= card
->myid
;
469 spin_lock_irqsave(&card
->lock
, flags
);
470 icn_free_queue(card
, channel
);
471 card
->rcvidx
[channel
] = 0;
474 ((channel
) ? ICN_FLAGS_B2ACTIVE
: ICN_FLAGS_B1ACTIVE
)) {
478 card
->flags
&= ~((channel
) ?
479 ICN_FLAGS_B2ACTIVE
: ICN_FLAGS_B1ACTIVE
);
481 memset(&ncmd
, 0, sizeof(ncmd
));
483 ncmd
.driver
= card
->myid
;
485 ncmd
.command
= ISDN_STAT_BHUP
;
486 spin_unlock_irqrestore(&card
->lock
, flags
);
487 card
->interface
.statcallb(&cmd
);
489 spin_unlock_irqrestore(&card
->lock
, flags
);
492 spin_lock_irqsave(&card
->lock
, flags
);
493 icn_free_queue(card
, channel
);
494 card
->flags
|= (channel
) ?
495 ICN_FLAGS_B2ACTIVE
: ICN_FLAGS_B1ACTIVE
;
496 spin_unlock_irqrestore(&card
->lock
, flags
);
499 spin_lock_irqsave(&card
->lock
, flags
);
500 card
->flags
&= ~((channel
) ?
501 ICN_FLAGS_B2ACTIVE
: ICN_FLAGS_B1ACTIVE
);
502 icn_free_queue(card
, channel
);
503 card
->rcvidx
[channel
] = 0;
504 spin_unlock_irqrestore(&card
->lock
, flags
);
508 char *t
= status
+ 6;
509 char *s
= strchr(t
, ',');
512 strlcpy(cmd
.parm
.setup
.phone
, t
,
513 sizeof(cmd
.parm
.setup
.phone
));
514 s
= strchr(t
= s
, ',');
517 cmd
.parm
.setup
.si1
= 0;
520 simple_strtoul(t
, NULL
, 10);
521 s
= strchr(t
= s
, ',');
524 cmd
.parm
.setup
.si2
= 0;
527 simple_strtoul(t
, NULL
, 10);
528 strlcpy(cmd
.parm
.setup
.eazmsn
, s
,
529 sizeof(cmd
.parm
.setup
.eazmsn
));
531 cmd
.parm
.setup
.plan
= 0;
532 cmd
.parm
.setup
.screen
= 0;
535 sprintf(cmd
.parm
.setup
.phone
, "LEASED%d", card
->myid
);
536 sprintf(cmd
.parm
.setup
.eazmsn
, "%d", channel
+ 1);
537 cmd
.parm
.setup
.si1
= 7;
538 cmd
.parm
.setup
.si2
= 0;
539 cmd
.parm
.setup
.plan
= 0;
540 cmd
.parm
.setup
.screen
= 0;
543 strlcpy(cmd
.parm
.num
, status
+ 3, sizeof(cmd
.parm
.num
));
546 snprintf(cmd
.parm
.num
, sizeof(cmd
.parm
.num
), "%d",
547 (int) simple_strtoul(status
+ 7, NULL
, 16));
551 if (strlen(status
) == 4)
552 snprintf(cmd
.parm
.num
, sizeof(cmd
.parm
.num
), "%s%c%c",
553 status
+ 2, *status
, *(status
+ 1));
555 strlcpy(cmd
.parm
.num
, status
+ 1, sizeof(cmd
.parm
.num
));
558 spin_lock_irqsave(&card
->lock
, flags
);
559 card
->flags
&= ~ICN_FLAGS_B1ACTIVE
;
560 icn_free_queue(card
, 0);
562 spin_unlock_irqrestore(&card
->lock
, flags
);
564 cmd
.driver
= card
->myid
;
565 card
->interface
.statcallb(&cmd
);
566 cmd
.command
= ISDN_STAT_DHUP
;
568 cmd
.driver
= card
->myid
;
569 card
->interface
.statcallb(&cmd
);
570 cmd
.command
= ISDN_STAT_BHUP
;
571 spin_lock_irqsave(&card
->lock
, flags
);
572 card
->flags
&= ~ICN_FLAGS_B2ACTIVE
;
573 icn_free_queue(card
, 1);
575 spin_unlock_irqrestore(&card
->lock
, flags
);
577 cmd
.driver
= card
->myid
;
578 card
->interface
.statcallb(&cmd
);
579 cmd
.command
= ISDN_STAT_DHUP
;
581 cmd
.driver
= card
->myid
;
584 card
->interface
.statcallb(&cmd
);
589 icn_putmsg(icn_card
*card
, unsigned char c
)
593 spin_lock_irqsave(&card
->lock
, flags
);
594 *card
->msg_buf_write
++ = (c
== 0xff) ? '\n' : c
;
595 if (card
->msg_buf_write
== card
->msg_buf_read
) {
596 if (++card
->msg_buf_read
> card
->msg_buf_end
)
597 card
->msg_buf_read
= card
->msg_buf
;
599 if (card
->msg_buf_write
> card
->msg_buf_end
)
600 card
->msg_buf_write
= card
->msg_buf
;
601 spin_unlock_irqrestore(&card
->lock
, flags
);
605 icn_polldchan(unsigned long data
)
607 icn_card
*card
= (icn_card
*) data
;
608 int mch
= card
->secondhalf
? 2 : 0;
618 if (icn_trymaplock_channel(card
, mch
)) {
620 for (left
= avail
, i
= readb(&msg_o
); left
> 0; i
++, left
--) {
621 c
= readb(&dev
.shmem
->comm_buffers
.iopc_buf
[i
& 0xff]);
624 card
->imsg
[card
->iptr
] = 0;
626 if (card
->imsg
[0] == '0' && card
->imsg
[1] >= '0' &&
627 card
->imsg
[1] <= '2' && card
->imsg
[2] == ';') {
628 ch
= (card
->imsg
[1] - '0') - 1;
630 icn_parse_status(p
, ch
, card
);
633 if (!strncmp(p
, "DRV1.", 5)) {
637 printk(KERN_INFO
"icn: (%s) %s\n", CID
, p
);
638 if (!strncmp(p
+ 7, "TC", 2)) {
639 card
->ptype
= ISDN_PTYPE_1TR6
;
640 card
->interface
.features
|= ISDN_FEATURE_P_1TR6
;
642 "icn: (%s) 1TR6-Protocol loaded and running\n", CID
);
644 if (!strncmp(p
+ 7, "EC", 2)) {
645 card
->ptype
= ISDN_PTYPE_EURO
;
646 card
->interface
.features
|= ISDN_FEATURE_P_EURO
;
648 "icn: (%s) Euro-Protocol loaded and running\n", CID
);
650 p
= strstr(card
->imsg
, "BRV") + 3;
652 if (*p
>= '0' && *p
<= '9')
659 card
->fw_rev
= (int) simple_strtoul(vstr
, NULL
, 10);
665 card
->imsg
[card
->iptr
] = c
;
670 writeb((readb(&msg_o
) + avail
) & 0xff, &msg_o
);
671 icn_release_channel();
674 cmd
.command
= ISDN_STAT_STAVAIL
;
675 cmd
.driver
= card
->myid
;
677 card
->interface
.statcallb(&cmd
);
679 spin_lock_irqsave(&card
->lock
, flags
);
680 if (card
->flags
& (ICN_FLAGS_B1ACTIVE
| ICN_FLAGS_B2ACTIVE
))
681 if (!(card
->flags
& ICN_FLAGS_RBTIMER
)) {
682 /* schedule b-channel polling */
683 card
->flags
|= ICN_FLAGS_RBTIMER
;
684 del_timer(&card
->rb_timer
);
685 card
->rb_timer
.function
= icn_pollbchan
;
686 card
->rb_timer
.data
= (unsigned long) card
;
687 card
->rb_timer
.expires
= jiffies
+ ICN_TIMER_BCREAD
;
688 add_timer(&card
->rb_timer
);
691 mod_timer(&card
->st_timer
, jiffies
+ ICN_TIMER_DCREAD
);
692 spin_unlock_irqrestore(&card
->lock
, flags
);
695 /* Append a packet to the transmit buffer-queue.
697 * channel = Number of B-channel
698 * skb = pointer to sk_buff
699 * card = pointer to card-struct
701 * Number of bytes transferred, -E??? on error
705 icn_sendbuf(int channel
, int ack
, struct sk_buff
*skb
, icn_card
*card
)
709 struct sk_buff
*nskb
;
713 "icn: Send packet too large\n");
717 if (!(card
->flags
& (channel
) ? ICN_FLAGS_B2ACTIVE
: ICN_FLAGS_B1ACTIVE
))
719 if (card
->sndcount
[channel
] > ICN_MAX_SQUEUE
)
721 #warning TODO test headroom or use skb->nb to flag ACK
722 nskb
= skb_clone(skb
, GFP_ATOMIC
);
724 /* Push ACK flag as one
725 * byte in front of data.
727 *(skb_push(nskb
, 1)) = ack
? 1 : 0;
728 skb_queue_tail(&card
->spqueue
[channel
], nskb
);
732 spin_lock_irqsave(&card
->lock
, flags
);
733 card
->sndcount
[channel
] += len
;
734 spin_unlock_irqrestore(&card
->lock
, flags
);
740 * Check card's status after starting the bootstrap loader.
741 * On entry, the card's shared memory has already to be mapped.
743 * 0 on success (Boot loader ready)
744 * -EIO on failure (timeout)
747 icn_check_loader(int cardnumber
)
753 printk(KERN_DEBUG
"Loader %d ?\n", cardnumber
);
755 if (readb(&dev
.shmem
->data_control
.scns
) ||
756 readb(&dev
.shmem
->data_control
.scnr
)) {
759 "icn: Boot-Loader %d timed out.\n",
761 icn_release_channel();
765 printk(KERN_DEBUG
"Loader %d TO?\n", cardnumber
);
767 msleep_interruptible(ICN_BOOT_TIMEOUT1
);
770 printk(KERN_DEBUG
"Loader %d OK\n", cardnumber
);
772 icn_release_channel();
778 /* Load the boot-code into the interface-card's memory and start it.
779 * Always called from user-process.
782 * buffer = pointer to packet
784 * 0 if successfully loaded
788 #define SLEEP(sec) { \
790 printk(KERN_DEBUG "SLEEP(%d)\n", slsec); \
792 msleep_interruptible(1000); \
801 icn_loadboot(u_char __user
*buffer
, icn_card
*card
)
808 printk(KERN_DEBUG
"icn_loadboot called, buffaddr=%08lx\n", (ulong
) buffer
);
810 if (!(codebuf
= kmalloc(ICN_CODE_STAGE1
, GFP_KERNEL
))) {
811 printk(KERN_WARNING
"icn: Could not allocate code buffer\n");
815 if (copy_from_user(codebuf
, buffer
, ICN_CODE_STAGE1
)) {
820 if (!request_region(card
->port
, ICN_PORTLEN
, card
->regname
)) {
822 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
825 card
->port
+ ICN_PORTLEN
);
831 card
->other
->rvalid
= 1;
834 if (!request_mem_region(dev
.memaddr
, 0x4000, "icn-isdn (all cards)")) {
836 "icn: memory at 0x%08lx in use.\n", dev
.memaddr
);
840 dev
.shmem
= ioremap(dev
.memaddr
, 0x4000);
843 OUTB_P(0, ICN_RUN
); /* Reset Controller */
844 OUTB_P(0, ICN_MAPRAM
); /* Disable RAM */
845 icn_shiftout(ICN_CFG
, 0x0f, 3, 4); /* Windowsize= 16k */
846 icn_shiftout(ICN_CFG
, dev
.memaddr
, 23, 10); /* Set RAM-Addr. */
848 printk(KERN_DEBUG
"shmem=%08lx\n", dev
.memaddr
);
852 printk(KERN_DEBUG
"Map Bank 0\n");
854 spin_lock_irqsave(&dev
.devlock
, flags
);
855 icn_map_channel(card
, 0); /* Select Bank 0 */
856 icn_lock_channel(card
, 0); /* Lock Bank 0 */
857 spin_unlock_irqrestore(&dev
.devlock
, flags
);
859 memcpy_toio(dev
.shmem
, codebuf
, ICN_CODE_STAGE1
); /* Copy code */
861 printk(KERN_DEBUG
"Bootloader transferred\n");
863 if (card
->doubleS0
) {
866 printk(KERN_DEBUG
"Map Bank 8\n");
868 spin_lock_irqsave(&dev
.devlock
, flags
);
869 __icn_release_channel();
870 icn_map_channel(card
, 2); /* Select Bank 8 */
871 icn_lock_channel(card
, 2); /* Lock Bank 8 */
872 spin_unlock_irqrestore(&dev
.devlock
, flags
);
874 memcpy_toio(dev
.shmem
, codebuf
, ICN_CODE_STAGE1
); /* Copy code */
876 printk(KERN_DEBUG
"Bootloader transferred\n");
880 OUTB_P(0xff, ICN_RUN
); /* Start Boot-Code */
881 if ((ret
= icn_check_loader(card
->doubleS0
? 2 : 1))) {
884 if (!card
->doubleS0
) {
888 /* reached only, if we have a Double-S0-Card */
890 printk(KERN_DEBUG
"Map Bank 0\n");
892 spin_lock_irqsave(&dev
.devlock
, flags
);
893 icn_map_channel(card
, 0); /* Select Bank 0 */
894 icn_lock_channel(card
, 0); /* Lock Bank 0 */
895 spin_unlock_irqrestore(&dev
.devlock
, flags
);
897 ret
= (icn_check_loader(1));
906 icn_loadproto(u_char __user
*buffer
, icn_card
*card
)
908 register u_char __user
*p
= buffer
;
910 uint left
= ICN_CODE_STAGE2
;
916 printk(KERN_DEBUG
"icn_loadproto called\n");
918 if (!access_ok(VERIFY_READ
, buffer
, ICN_CODE_STAGE2
))
921 spin_lock_irqsave(&dev
.devlock
, flags
);
922 if (card
->secondhalf
) {
923 icn_map_channel(card
, 2);
924 icn_lock_channel(card
, 2);
926 icn_map_channel(card
, 0);
927 icn_lock_channel(card
, 0);
929 spin_unlock_irqrestore(&dev
.devlock
, flags
);
931 if (sbfree
) { /* If there is a free buffer... */
935 if (copy_from_user(codebuf
, p
, cnt
)) {
936 icn_maprelease_channel(card
, 0);
939 memcpy_toio(&sbuf_l
, codebuf
, cnt
); /* copy data */
940 sbnext
; /* switch to next buffer */
946 printk(KERN_DEBUG
"boot 2 !sbfree\n");
949 icn_maprelease_channel(card
, 0);
952 schedule_timeout_interruptible(10);
955 writeb(0x20, &sbuf_n
);
958 if (readb(&cmd_o
) || readb(&cmd_i
)) {
960 printk(KERN_DEBUG
"Proto?\n");
964 "icn: (%s) Protocol timed out.\n",
967 printk(KERN_DEBUG
"Proto TO!\n");
969 icn_maprelease_channel(card
, 0);
973 printk(KERN_DEBUG
"Proto TO?\n");
975 msleep_interruptible(ICN_BOOT_TIMEOUT1
);
977 if ((card
->secondhalf
) || (!card
->doubleS0
)) {
979 printk(KERN_DEBUG
"Proto loaded, install poll-timer %d\n",
982 spin_lock_irqsave(&card
->lock
, flags
);
983 init_timer(&card
->st_timer
);
984 card
->st_timer
.expires
= jiffies
+ ICN_TIMER_DCREAD
;
985 card
->st_timer
.function
= icn_polldchan
;
986 card
->st_timer
.data
= (unsigned long) card
;
987 add_timer(&card
->st_timer
);
988 card
->flags
|= ICN_FLAGS_RUNNING
;
989 if (card
->doubleS0
) {
990 init_timer(&card
->other
->st_timer
);
991 card
->other
->st_timer
.expires
= jiffies
+ ICN_TIMER_DCREAD
;
992 card
->other
->st_timer
.function
= icn_polldchan
;
993 card
->other
->st_timer
.data
= (unsigned long) card
->other
;
994 add_timer(&card
->other
->st_timer
);
995 card
->other
->flags
|= ICN_FLAGS_RUNNING
;
997 spin_unlock_irqrestore(&card
->lock
, flags
);
999 icn_maprelease_channel(card
, 0);
1005 /* Read the Status-replies from the Interface */
1007 icn_readstatus(u_char __user
*buf
, int len
, icn_card
*card
)
1012 for (p
= buf
, count
= 0; count
< len
; p
++, count
++) {
1013 if (card
->msg_buf_read
== card
->msg_buf_write
)
1015 if (put_user(*card
->msg_buf_read
++, p
))
1017 if (card
->msg_buf_read
> card
->msg_buf_end
)
1018 card
->msg_buf_read
= card
->msg_buf
;
1023 /* Put command-strings into the command-queue of the Interface */
1025 icn_writecmd(const u_char
*buf
, int len
, int user
, icn_card
*card
)
1027 int mch
= card
->secondhalf
? 2 : 0;
1034 unsigned long flags
;
1035 int lastmap_channel
;
1036 struct icn_card
*lastmap_card
;
1048 if (copy_from_user(msg
, buf
, count
))
1051 memcpy(msg
, buf
, count
);
1053 spin_lock_irqsave(&dev
.devlock
, flags
);
1054 lastmap_card
= dev
.mcard
;
1055 lastmap_channel
= dev
.channel
;
1056 icn_map_channel(card
, mch
);
1058 icn_putmsg(card
, '>');
1059 for (p
= msg
, pp
= readb(&cmd_i
), i
= count
; i
> 0; i
--, p
++, pp
1061 writeb((*p
== '\n') ? 0xff : *p
,
1062 &dev
.shmem
->comm_buffers
.pcio_buf
[pp
& 0xff]);
1065 icn_putmsg(card
, *p
);
1066 if ((*p
== '\n') && (i
> 1)) {
1067 icn_putmsg(card
, '>');
1072 writeb((readb(&cmd_i
) + count
) & 0xff, &cmd_i
);
1074 icn_map_channel(lastmap_card
, lastmap_channel
);
1075 spin_unlock_irqrestore(&dev
.devlock
, flags
);
1084 printk(KERN_WARNING
"icn: writemsg incomplete!\n");
1085 cmd
.command
= ISDN_STAT_STAVAIL
;
1086 cmd
.driver
= card
->myid
;
1088 card
->interface
.statcallb(&cmd
);
1093 * Delete card's pending timers, send STOP to linklevel
1096 icn_stopcard(icn_card
*card
)
1098 unsigned long flags
;
1101 spin_lock_irqsave(&card
->lock
, flags
);
1102 if (card
->flags
& ICN_FLAGS_RUNNING
) {
1103 card
->flags
&= ~ICN_FLAGS_RUNNING
;
1104 del_timer(&card
->st_timer
);
1105 del_timer(&card
->rb_timer
);
1106 spin_unlock_irqrestore(&card
->lock
, flags
);
1107 cmd
.command
= ISDN_STAT_STOP
;
1108 cmd
.driver
= card
->myid
;
1109 card
->interface
.statcallb(&cmd
);
1111 icn_stopcard(card
->other
);
1113 spin_unlock_irqrestore(&card
->lock
, flags
);
1117 icn_stopallcards(void)
1119 icn_card
*p
= cards
;
1128 * Unmap all cards, because some of them may be mapped accidetly during
1129 * autoprobing of some network drivers (SMC-driver?)
1132 icn_disable_cards(void)
1134 icn_card
*card
= cards
;
1137 if (!request_region(card
->port
, ICN_PORTLEN
, "icn-isdn")) {
1139 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1142 card
->port
+ ICN_PORTLEN
);
1144 OUTB_P(0, ICN_RUN
); /* Reset Controller */
1145 OUTB_P(0, ICN_MAPRAM
); /* Disable RAM */
1146 release_region(card
->port
, ICN_PORTLEN
);
1153 icn_command(isdn_ctrl
*c
, icn_card
*card
)
1163 switch (c
->command
) {
1164 case ISDN_CMD_IOCTL
:
1165 memcpy(&a
, c
->parm
.num
, sizeof(ulong
));
1166 arg
= (char __user
*)a
;
1168 case ICN_IOCTL_SETMMIO
:
1169 if (dev
.memaddr
!= (a
& 0x0ffc000)) {
1170 if (!request_mem_region(a
& 0x0ffc000, 0x4000, "icn-isdn (all cards)")) {
1172 "icn: memory at 0x%08lx in use.\n",
1176 release_mem_region(a
& 0x0ffc000, 0x4000);
1178 spin_lock_irqsave(&card
->lock
, flags
);
1181 release_mem_region(dev
.memaddr
, 0x4000);
1184 dev
.memaddr
= a
& 0x0ffc000;
1185 spin_unlock_irqrestore(&card
->lock
, flags
);
1187 "icn: (%s) mmio set to 0x%08lx\n",
1192 case ICN_IOCTL_GETMMIO
:
1193 return (long) dev
.memaddr
;
1194 case ICN_IOCTL_SETPORT
:
1195 if (a
== 0x300 || a
== 0x310 || a
== 0x320 || a
== 0x330
1196 || a
== 0x340 || a
== 0x350 || a
== 0x360 ||
1197 a
== 0x308 || a
== 0x318 || a
== 0x328 || a
== 0x338
1198 || a
== 0x348 || a
== 0x358 || a
== 0x368) {
1199 if (card
->port
!= (unsigned short) a
) {
1200 if (!request_region((unsigned short) a
, ICN_PORTLEN
, "icn-isdn")) {
1202 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1203 CID
, (int) a
, (int) a
+ ICN_PORTLEN
);
1206 release_region((unsigned short) a
, ICN_PORTLEN
);
1208 spin_lock_irqsave(&card
->lock
, flags
);
1210 release_region(card
->port
, ICN_PORTLEN
);
1211 card
->port
= (unsigned short) a
;
1213 if (card
->doubleS0
) {
1214 card
->other
->port
= (unsigned short) a
;
1215 card
->other
->rvalid
= 0;
1217 spin_unlock_irqrestore(&card
->lock
, flags
);
1219 "icn: (%s) port set to 0x%03x\n",
1225 case ICN_IOCTL_GETPORT
:
1226 return (int) card
->port
;
1227 case ICN_IOCTL_GETDOUBLE
:
1228 return (int) card
->doubleS0
;
1229 case ICN_IOCTL_DEBUGVAR
:
1230 if (copy_to_user(arg
,
1236 ulong l
= (ulong
)&dev
;
1237 if (copy_to_user(arg
,
1243 case ICN_IOCTL_LOADBOOT
:
1244 if (dev
.firstload
) {
1245 icn_disable_cards();
1249 return (icn_loadboot(arg
, card
));
1250 case ICN_IOCTL_LOADPROTO
:
1252 if ((i
= (icn_loadproto(arg
, card
))))
1255 i
= icn_loadproto(arg
+ ICN_CODE_STAGE2
, card
->other
);
1258 case ICN_IOCTL_ADDCARD
:
1261 if (copy_from_user(&cdef
,
1265 return (icn_addcard(cdef
.port
, cdef
.id1
, cdef
.id2
));
1267 case ICN_IOCTL_LEASEDCFG
:
1269 if (!card
->leased
) {
1271 while (card
->ptype
== ISDN_PTYPE_UNKNOWN
) {
1272 msleep_interruptible(ICN_BOOT_TIMEOUT1
);
1274 msleep_interruptible(ICN_BOOT_TIMEOUT1
);
1275 sprintf(cbuf
, "00;FV2ON\n01;EAZ%c\n02;EAZ%c\n",
1276 (a
& 1) ? '1' : 'C', (a
& 2) ? '2' : 'C');
1277 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1279 "icn: (%s) Leased-line mode enabled\n",
1281 cmd
.command
= ISDN_STAT_RUN
;
1282 cmd
.driver
= card
->myid
;
1284 card
->interface
.statcallb(&cmd
);
1289 sprintf(cbuf
, "00;FV2OFF\n");
1290 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1292 "icn: (%s) Leased-line mode disabled\n",
1294 cmd
.command
= ISDN_STAT_RUN
;
1295 cmd
.driver
= card
->myid
;
1297 card
->interface
.statcallb(&cmd
);
1306 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1310 if ((c
->arg
& 255) < ICN_BCH
) {
1316 p
= c
->parm
.setup
.phone
;
1317 if (*p
== 's' || *p
== 'S') {
1320 strcpy(dcode
, "SCA");
1323 strcpy(dcode
, "CAL");
1325 sprintf(cbuf
, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a
+ 1),
1326 dcode
, dial
, c
->parm
.setup
.si1
,
1327 c
->parm
.setup
.si2
, c
->parm
.setup
.eazmsn
);
1328 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1331 case ISDN_CMD_ACCEPTD
:
1332 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1334 if (c
->arg
< ICN_BCH
) {
1336 if (card
->fw_rev
>= 300) {
1337 switch (card
->l2_proto
[a
- 1]) {
1338 case ISDN_PROTO_L2_X75I
:
1339 sprintf(cbuf
, "%02d;BX75\n", (int) a
);
1341 case ISDN_PROTO_L2_HDLC
:
1342 sprintf(cbuf
, "%02d;BTRA\n", (int) a
);
1345 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1347 sprintf(cbuf
, "%02d;DCON_R\n", (int) a
);
1348 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1351 case ISDN_CMD_ACCEPTB
:
1352 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1354 if (c
->arg
< ICN_BCH
) {
1356 if (card
->fw_rev
>= 300)
1357 switch (card
->l2_proto
[a
- 1]) {
1358 case ISDN_PROTO_L2_X75I
:
1359 sprintf(cbuf
, "%02d;BCON_R,BX75\n", (int) a
);
1361 case ISDN_PROTO_L2_HDLC
:
1362 sprintf(cbuf
, "%02d;BCON_R,BTRA\n", (int) a
);
1365 sprintf(cbuf
, "%02d;BCON_R\n", (int) a
);
1366 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1369 case ISDN_CMD_HANGUP
:
1370 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1372 if (c
->arg
< ICN_BCH
) {
1374 sprintf(cbuf
, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a
, (int) a
);
1375 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1378 case ISDN_CMD_SETEAZ
:
1379 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1383 if (c
->arg
< ICN_BCH
) {
1385 if (card
->ptype
== ISDN_PTYPE_EURO
) {
1386 sprintf(cbuf
, "%02d;MS%s%s\n", (int) a
,
1387 c
->parm
.num
[0] ? "N" : "ALL", c
->parm
.num
);
1389 sprintf(cbuf
, "%02d;EAZ%s\n", (int) a
,
1390 c
->parm
.num
[0] ? (char *)(c
->parm
.num
) : "0123456789");
1391 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1394 case ISDN_CMD_CLREAZ
:
1395 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1399 if (c
->arg
< ICN_BCH
) {
1401 if (card
->ptype
== ISDN_PTYPE_EURO
)
1402 sprintf(cbuf
, "%02d;MSNC\n", (int) a
);
1404 sprintf(cbuf
, "%02d;EAZC\n", (int) a
);
1405 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1408 case ISDN_CMD_SETL2
:
1409 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1411 if ((c
->arg
& 255) < ICN_BCH
) {
1414 case ISDN_PROTO_L2_X75I
:
1415 sprintf(cbuf
, "%02d;BX75\n", (int) (a
& 255) + 1);
1417 case ISDN_PROTO_L2_HDLC
:
1418 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1423 i
= icn_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1424 card
->l2_proto
[a
& 255] = (a
>> 8);
1427 case ISDN_CMD_SETL3
:
1428 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1438 * Find card with given driverId
1440 static inline icn_card
*
1441 icn_findcard(int driverid
)
1443 icn_card
*p
= cards
;
1446 if (p
->myid
== driverid
)
1450 return (icn_card
*) 0;
1454 * Wrapper functions for interface to linklevel
1457 if_command(isdn_ctrl
*c
)
1459 icn_card
*card
= icn_findcard(c
->driver
);
1462 return (icn_command(c
, card
));
1464 "icn: if_command %d called with invalid driverId %d!\n",
1465 c
->command
, c
->driver
);
1470 if_writecmd(const u_char __user
*buf
, int len
, int id
, int channel
)
1472 icn_card
*card
= icn_findcard(id
);
1475 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1477 return (icn_writecmd(buf
, len
, 1, card
));
1480 "icn: if_writecmd called with invalid driverId!\n");
1485 if_readstatus(u_char __user
*buf
, int len
, int id
, int channel
)
1487 icn_card
*card
= icn_findcard(id
);
1490 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1492 return (icn_readstatus(buf
, len
, card
));
1495 "icn: if_readstatus called with invalid driverId!\n");
1500 if_sendbuf(int id
, int channel
, int ack
, struct sk_buff
*skb
)
1502 icn_card
*card
= icn_findcard(id
);
1505 if (!(card
->flags
& ICN_FLAGS_RUNNING
))
1507 return (icn_sendbuf(channel
, ack
, skb
, card
));
1510 "icn: if_sendbuf called with invalid driverId!\n");
1515 * Allocate a new card-struct, initialize it
1516 * link it into cards-list and register it at linklevel.
1519 icn_initcard(int port
, char *id
)
1524 if (!(card
= kzalloc(sizeof(icn_card
), GFP_KERNEL
))) {
1526 "icn: (%s) Could not allocate card-struct.\n", id
);
1527 return (icn_card
*) 0;
1529 spin_lock_init(&card
->lock
);
1531 card
->interface
.owner
= THIS_MODULE
;
1532 card
->interface
.hl_hdrlen
= 1;
1533 card
->interface
.channels
= ICN_BCH
;
1534 card
->interface
.maxbufsize
= 4000;
1535 card
->interface
.command
= if_command
;
1536 card
->interface
.writebuf_skb
= if_sendbuf
;
1537 card
->interface
.writecmd
= if_writecmd
;
1538 card
->interface
.readstat
= if_readstatus
;
1539 card
->interface
.features
= ISDN_FEATURE_L2_X75I
|
1540 ISDN_FEATURE_L2_HDLC
|
1541 ISDN_FEATURE_L3_TRANS
|
1542 ISDN_FEATURE_P_UNKNOWN
;
1543 card
->ptype
= ISDN_PTYPE_UNKNOWN
;
1544 strlcpy(card
->interface
.id
, id
, sizeof(card
->interface
.id
));
1545 card
->msg_buf_write
= card
->msg_buf
;
1546 card
->msg_buf_read
= card
->msg_buf
;
1547 card
->msg_buf_end
= &card
->msg_buf
[sizeof(card
->msg_buf
) - 1];
1548 for (i
= 0; i
< ICN_BCH
; i
++) {
1549 card
->l2_proto
[i
] = ISDN_PROTO_L2_X75I
;
1550 skb_queue_head_init(&card
->spqueue
[i
]);
1554 if (!register_isdn(&card
->interface
)) {
1555 cards
= cards
->next
;
1557 "icn: Unable to register %s\n", id
);
1559 return (icn_card
*) 0;
1561 card
->myid
= card
->interface
.channels
;
1562 sprintf(card
->regname
, "icn-isdn (%s)", card
->interface
.id
);
1567 icn_addcard(int port
, char *id1
, char *id2
)
1572 if (!(card
= icn_initcard(port
, id1
))) {
1577 "icn: (%s) ICN-2B, port 0x%x added\n",
1578 card
->interface
.id
, port
);
1581 if (!(card2
= icn_initcard(port
, id2
))) {
1583 "icn: (%s) half ICN-4B, port 0x%x added\n",
1584 card2
->interface
.id
, port
);
1588 card
->secondhalf
= 0;
1589 card
->other
= card2
;
1590 card2
->doubleS0
= 1;
1591 card2
->secondhalf
= 1;
1592 card2
->other
= card
;
1594 "icn: (%s and %s) ICN-4B, port 0x%x added\n",
1595 card
->interface
.id
, card2
->interface
.id
, port
);
1601 icn_setup(char *line
)
1605 static char sid
[20];
1606 static char sid2
[20];
1608 str
= get_options(line
, 2, ints
);
1612 membase
= (unsigned long)ints
[2];
1616 if ((p
= strchr(sid
, ','))) {
1624 __setup("icn=", icn_setup
);
1627 static int __init
icn_init(void)
1632 memset(&dev
, 0, sizeof(icn_dev
));
1633 dev
.memaddr
= (membase
& 0x0ffc000);
1637 spin_lock_init(&dev
.devlock
);
1639 if ((p
= strchr(revision
, ':'))) {
1640 strncpy(rev
, p
+ 1, 20);
1642 p
= strchr(rev
, '$');
1646 strcpy(rev
, " ??? ");
1647 printk(KERN_NOTICE
"ICN-ISDN-driver Rev%smem=0x%08lx\n", rev
,
1649 return (icn_addcard(portbase
, icn_id
, icn_id2
));
1652 static void __exit
icn_exit(void)
1655 icn_card
*card
= cards
;
1656 icn_card
*last
, *tmpcard
;
1658 unsigned long flags
;
1662 cmd
.command
= ISDN_STAT_UNLOAD
;
1663 cmd
.driver
= card
->myid
;
1664 card
->interface
.statcallb(&cmd
);
1665 spin_lock_irqsave(&card
->lock
, flags
);
1667 OUTB_P(0, ICN_RUN
); /* Reset Controller */
1668 OUTB_P(0, ICN_MAPRAM
); /* Disable RAM */
1669 if (card
->secondhalf
|| (!card
->doubleS0
)) {
1670 release_region(card
->port
, ICN_PORTLEN
);
1673 for (i
= 0; i
< ICN_BCH
; i
++)
1674 icn_free_queue(card
, i
);
1676 tmpcard
= card
->next
;
1677 spin_unlock_irqrestore(&card
->lock
, flags
);
1689 release_mem_region(dev
.memaddr
, 0x4000);
1691 printk(KERN_NOTICE
"ICN-ISDN-driver unloaded\n");
1694 module_init(icn_init
);
1695 module_exit(icn_exit
);