2 * This software may be used and distributed according to the terms
3 * of the GNU General Public License, incorporated herein by reference.
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/delay.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
17 MODULE_DESCRIPTION("ISDN4Linux: Driver for Spellcaster card");
18 MODULE_AUTHOR("Spellcaster Telecommunications Inc.");
19 MODULE_LICENSE("GPL");
21 board
*sc_adapter
[MAX_CARDS
];
24 static char devname
[] = "scX";
25 static const char version
[] = "2.0b1";
27 static const char *boardname
[] = { "DataCommute/BRI", "DataCommute/PRI", "TeleCommute/BRI" };
29 /* insmod set parameters */
30 static unsigned int io
[] = {0, 0, 0, 0};
31 static unsigned char irq
[] = {0, 0, 0, 0};
32 static unsigned long ram
[] = {0, 0, 0, 0};
35 module_param_array(io
, int, NULL
, 0);
36 module_param_array(irq
, byte
, NULL
, 0);
37 module_param_array(ram
, long, NULL
, 0);
38 module_param(do_reset
, bool, 0);
40 static int identify_board(unsigned long, unsigned int);
42 static int __init
sc_init(void)
48 unsigned long memsize
= 0;
49 unsigned long features
= 0;
51 unsigned char channels
;
55 int last_base
= IOBASE_MIN
;
56 int probe_exhasted
= 0;
59 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s Loaded\n", version
);
61 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s\n", version
);
63 pr_info("Copyright (C) 1996 SpellCaster Telecommunications Inc.\n");
65 while (b
++ < MAX_CARDS
- 1) {
66 pr_debug("Probing for adapter #%d\n", b
);
68 * Initialize reusable variables
76 * See if we should probe for IO base
78 pr_debug("I/O Base for board %d is 0x%x, %s probe\n", b
, io
[b
],
79 io
[b
] == 0 ? "will" : "won't");
82 * No, I/O Base has been provided
84 for (i
= 0; i
< MAX_IO_REGS
- 1; i
++) {
85 if (!request_region(io
[b
] + i
* 0x400, 1, "sc test")) {
86 pr_debug("request_region for 0x%x failed\n", io
[b
] + i
* 0x400);
90 release_region(io
[b
] + i
* 0x400, 1);
94 * Confirm the I/O Address with a test
97 pr_debug("I/O Address invalid.\n");
101 outb(0x18, io
[b
] + 0x400 * EXP_PAGE0
);
102 if (inb(io
[b
] + 0x400 * EXP_PAGE0
) != 0x18) {
103 pr_debug("I/O Base 0x%x fails test\n",
104 io
[b
] + 0x400 * EXP_PAGE0
);
109 * Yes, probe for I/O Base
111 if (probe_exhasted
) {
112 pr_debug("All probe addresses exhausted, skipping\n");
115 pr_debug("Probing for I/O...\n");
116 for (i
= last_base
; i
<= IOBASE_MAX
; i
+= IOBASE_OFFSET
) {
118 if (i
== IOBASE_MAX
) {
119 probe_exhasted
= 1; /* No more addresses to probe */
120 pr_debug("End of Probes\n");
122 last_base
= i
+ IOBASE_OFFSET
;
123 pr_debug(" checking 0x%x...", i
);
124 for (j
= 0; j
< MAX_IO_REGS
- 1; j
++) {
125 if (!request_region(i
+ j
* 0x400, 1, "sc test")) {
126 pr_debug("Failed\n");
130 release_region(i
+ j
* 0x400, 1);
135 outb(0x18, io
[b
] + 0x400 * EXP_PAGE0
);
136 if (inb(io
[b
] + 0x400 * EXP_PAGE0
) != 0x18) {
137 pr_debug("Failed by test\n");
140 pr_debug("Passed\n");
144 if (probe_exhasted
) {
150 * See if we should probe for shared RAM
153 pr_debug("Doing a SAFE probe reset\n");
154 outb(0xFF, io
[b
] + RESET_OFFSET
);
155 msleep_interruptible(10000);
157 pr_debug("RAM Base for board %d is 0x%lx, %s probe\n", b
,
158 ram
[b
], ram
[b
] == 0 ? "will" : "won't");
162 * No, the RAM base has been provided
163 * Just look for a signature and ID the
166 if (request_region(ram
[b
], SRAM_PAGESIZE
, "sc test")) {
167 pr_debug("request_region for RAM base 0x%lx succeeded\n", ram
[b
]);
168 model
= identify_board(ram
[b
], io
[b
]);
169 release_region(ram
[b
], SRAM_PAGESIZE
);
173 * Yes, probe for free RAM and look for
174 * a signature and id the board model
176 for (i
= SRAM_MIN
; i
< SRAM_MAX
; i
+= SRAM_PAGESIZE
) {
177 pr_debug("Checking RAM address 0x%x...\n", i
);
178 if (request_region(i
, SRAM_PAGESIZE
, "sc test")) {
179 pr_debug(" request_region succeeded\n");
180 model
= identify_board(i
, io
[b
]);
181 release_region(i
, SRAM_PAGESIZE
);
183 pr_debug(" Identified a %s\n",
188 pr_debug(" Unidentified or inaccessible\n");
191 pr_debug(" request failed\n");
195 * See if we found free RAM and the board model
197 if (!ram
[b
] || model
< 0) {
199 * Nope, there was no place in RAM for the
200 * board, or it couldn't be identified
202 pr_debug("Failed to find an adapter at 0x%lx\n", ram
[b
]);
207 * Set the board's magic number, memory size and page register
214 features
= PRI_FEATURES
;
222 features
= BRI_FEATURES
;
225 switch (ram
[b
] >> 12 & 0x0F) {
227 pr_debug("RAM Page register set to EXP_PAGE0\n");
232 pr_debug("RAM Page register set to EXP_PAGE1\n");
237 pr_debug("RAM Page register set to EXP_PAGE2\n");
242 pr_debug("RAM Page register set to EXP_PAGE3\n");
247 pr_debug("RAM base address doesn't fall on 16K boundary\n");
251 pr_debug("current IRQ: %d b: %d\n", irq
[b
], b
);
254 * Make sure we got an IRQ
258 * No interrupt could be used
260 pr_debug("Failed to acquire an IRQ line\n");
265 * Horray! We found a board, Make sure we can register
268 interface
= kzalloc(sizeof(isdn_if
), GFP_KERNEL
);
269 if (interface
== NULL
) {
271 * Oops, can't malloc isdn_if
276 interface
->owner
= THIS_MODULE
;
277 interface
->hl_hdrlen
= 0;
278 interface
->channels
= channels
;
279 interface
->maxbufsize
= BUFFER_SIZE
;
280 interface
->features
= features
;
281 interface
->writebuf_skb
= sndpkt
;
282 interface
->writecmd
= NULL
;
283 interface
->command
= command
;
284 strcpy(interface
->id
, devname
);
285 interface
->id
[2] = '0' + cinst
;
288 * Allocate the board structure
290 sc_adapter
[cinst
] = kzalloc(sizeof(board
), GFP_KERNEL
);
291 if (sc_adapter
[cinst
] == NULL
) {
293 * Oops, can't alloc memory for the board
298 spin_lock_init(&sc_adapter
[cinst
]->lock
);
300 if (!register_isdn(interface
)) {
302 * Oops, couldn't register for some reason
305 kfree(sc_adapter
[cinst
]);
309 sc_adapter
[cinst
]->card
= interface
;
310 sc_adapter
[cinst
]->driverId
= interface
->channels
;
311 strcpy(sc_adapter
[cinst
]->devicename
, interface
->id
);
312 sc_adapter
[cinst
]->nChannels
= channels
;
313 sc_adapter
[cinst
]->ramsize
= memsize
;
314 sc_adapter
[cinst
]->shmem_magic
= magic
;
315 sc_adapter
[cinst
]->shmem_pgport
= pgport
;
316 sc_adapter
[cinst
]->StartOnReset
= 1;
319 * Allocate channels status structures
321 sc_adapter
[cinst
]->channel
= kzalloc(sizeof(bchan
) * channels
, GFP_KERNEL
);
322 if (sc_adapter
[cinst
]->channel
== NULL
) {
324 * Oops, can't alloc memory for the channels
326 indicate_status(cinst
, ISDN_STAT_UNLOAD
, 0, NULL
); /* Fix me */
328 kfree(sc_adapter
[cinst
]);
333 * Lock down the hardware resources
335 sc_adapter
[cinst
]->interrupt
= irq
[b
];
336 if (request_irq(sc_adapter
[cinst
]->interrupt
, interrupt_handler
,
338 (void *)(unsigned long) cinst
)) {
339 kfree(sc_adapter
[cinst
]->channel
);
340 indicate_status(cinst
, ISDN_STAT_UNLOAD
, 0, NULL
); /* Fix me */
342 kfree(sc_adapter
[cinst
]);
346 sc_adapter
[cinst
]->iobase
= io
[b
];
347 for (i
= 0; i
< MAX_IO_REGS
- 1; i
++) {
348 sc_adapter
[cinst
]->ioport
[i
] = io
[b
] + i
* 0x400;
349 request_region(sc_adapter
[cinst
]->ioport
[i
], 1,
351 pr_debug("Requesting I/O Port %#x\n",
352 sc_adapter
[cinst
]->ioport
[i
]);
354 sc_adapter
[cinst
]->ioport
[IRQ_SELECT
] = io
[b
] + 0x2;
355 request_region(sc_adapter
[cinst
]->ioport
[IRQ_SELECT
], 1,
357 pr_debug("Requesting I/O Port %#x\n",
358 sc_adapter
[cinst
]->ioport
[IRQ_SELECT
]);
359 sc_adapter
[cinst
]->rambase
= ram
[b
];
360 request_region(sc_adapter
[cinst
]->rambase
, SRAM_PAGESIZE
,
363 pr_info(" %s (%d) - %s %d channels IRQ %d, I/O Base 0x%x, RAM Base 0x%lx\n",
364 sc_adapter
[cinst
]->devicename
,
365 sc_adapter
[cinst
]->driverId
,
366 boardname
[model
], channels
, irq
[b
], io
[b
], ram
[b
]);
369 * reset the adapter to put things in motion
377 pr_info("Failed to find any adapters, driver unloaded\n");
381 static void __exit
sc_exit(void)
385 for (i
= 0; i
< cinst
; i
++) {
386 pr_debug("Cleaning up after adapter %d\n", i
);
390 del_timer_sync(&(sc_adapter
[i
]->reset_timer
));
391 del_timer_sync(&(sc_adapter
[i
]->stat_timer
));
394 * Tell I4L we're toast
396 indicate_status(i
, ISDN_STAT_STOP
, 0, NULL
);
397 indicate_status(i
, ISDN_STAT_UNLOAD
, 0, NULL
);
402 release_region(sc_adapter
[i
]->rambase
, SRAM_PAGESIZE
);
407 free_irq(sc_adapter
[i
]->interrupt
, NULL
);
410 * Reset for a clean start
412 outb(0xFF, sc_adapter
[i
]->ioport
[SFT_RESET
]);
415 * Release the I/O Port regions
417 for (j
= 0; j
< MAX_IO_REGS
- 1; j
++) {
418 release_region(sc_adapter
[i
]->ioport
[j
], 1);
419 pr_debug("Releasing I/O Port %#x\n",
420 sc_adapter
[i
]->ioport
[j
]);
422 release_region(sc_adapter
[i
]->ioport
[IRQ_SELECT
], 1);
423 pr_debug("Releasing I/O Port %#x\n",
424 sc_adapter
[i
]->ioport
[IRQ_SELECT
]);
427 * Release any memory we alloced
429 kfree(sc_adapter
[i
]->channel
);
430 kfree(sc_adapter
[i
]->card
);
431 kfree(sc_adapter
[i
]);
433 pr_info("SpellCaster ISA ISDN Adapter Driver Unloaded.\n");
436 static int identify_board(unsigned long rambase
, unsigned int iobase
)
446 pr_debug("Attempting to identify adapter @ 0x%lx io 0x%x\n",
450 * Enable the base pointer
452 outb(rambase
>> 12, iobase
+ 0x2c00);
454 switch (rambase
>> 12 & 0x0F) {
456 pgport
= iobase
+ PG0_OFFSET
;
457 pr_debug("Page Register offset is 0x%x\n", PG0_OFFSET
);
461 pgport
= iobase
+ PG1_OFFSET
;
462 pr_debug("Page Register offset is 0x%x\n", PG1_OFFSET
);
466 pgport
= iobase
+ PG2_OFFSET
;
467 pr_debug("Page Register offset is 0x%x\n", PG2_OFFSET
);
471 pgport
= iobase
+ PG3_OFFSET
;
472 pr_debug("Page Register offset is 0x%x\n", PG3_OFFSET
);
475 pr_debug("Invalid rambase 0x%lx\n", rambase
);
480 * Try to identify a PRI card
482 outb(PRI_BASEPG_VAL
, pgport
);
483 msleep_interruptible(1000);
484 sig
= readl(rambase
+ SIG_OFFSET
);
485 pr_debug("Looking for a signature, got 0x%lx\n", sig
);
486 if (sig
== SIGNATURE
)
490 * Try to identify a PRI card
492 outb(BRI_BASEPG_VAL
, pgport
);
493 msleep_interruptible(1000);
494 sig
= readl(rambase
+ SIG_OFFSET
);
495 pr_debug("Looking for a signature, got 0x%lx\n", sig
);
496 if (sig
== SIGNATURE
)
504 sig
= readl(rambase
+ SIG_OFFSET
);
505 pr_debug("Looking for a signature, got 0x%lx\n", sig
);
506 if (sig
!= SIGNATURE
)
509 dpm
= (DualPortMemory
*) rambase
;
511 memset(&sndmsg
, 0, MSG_LEN
);
512 sndmsg
.msg_byte_cnt
= 3;
513 sndmsg
.type
= cmReqType1
;
514 sndmsg
.class = cmReqClass0
;
515 sndmsg
.code
= cmReqHWConfig
;
516 memcpy_toio(&(dpm
->req_queue
[dpm
->req_head
++]), &sndmsg
, MSG_LEN
);
517 outb(0, iobase
+ 0x400);
518 pr_debug("Sent HWConfig message\n");
520 * Wait for the response
523 while ((inb(iobase
+ FIFOSTAT_OFFSET
) & RF_HAS_DATA
) && x
< 100) {
524 schedule_timeout_interruptible(1);
528 pr_debug("Timeout waiting for response\n");
532 memcpy_fromio(&rcvmsg
, &(dpm
->rsp_queue
[dpm
->rsp_tail
]), MSG_LEN
);
533 pr_debug("Got HWConfig response, status = 0x%x\n", rcvmsg
.rsp_status
);
534 memcpy(&hwci
, &(rcvmsg
.msg_data
.HWCresponse
), sizeof(HWConfig_pl
));
535 pr_debug("Hardware Config: Interface: %s, RAM Size: %ld, Serial: %s\n"
536 " Part: %s, Rev: %s\n",
537 hwci
.st_u_sense
? "S/T" : "U", hwci
.ram_size
,
538 hwci
.serial_no
, hwci
.part_no
, hwci
.rev_no
);
540 if (!strncmp(PRI_PARTNO
, hwci
.part_no
, 6))
542 if (!strncmp(BRI_PARTNO
, hwci
.part_no
, 6))
548 module_init(sc_init
);
549 module_exit(sc_exit
);