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>
15 MODULE_DESCRIPTION("ISDN4Linux: Driver for Spellcaster card");
16 MODULE_AUTHOR("Spellcaster Telecommunications Inc.");
17 MODULE_LICENSE("GPL");
19 board *sc_adapter[MAX_CARDS];
22 static char devname[] = "scX";
23 static const char version[] = "2.0b1";
25 static const char *boardname[] = { "DataCommute/BRI", "DataCommute/PRI", "TeleCommute/BRI" };
27 /* insmod set parameters */
28 static unsigned int io[] = {0,0,0,0};
29 static unsigned char irq[] = {0,0,0,0};
30 static unsigned long ram[] = {0,0,0,0};
31 static int do_reset = 0;
33 module_param_array(io, int, NULL, 0);
34 module_param_array(irq, int, NULL, 0);
35 module_param_array(ram, int, NULL, 0);
36 module_param(do_reset, bool, 0);
38 extern irqreturn_t interrupt_handler(int, void *);
39 extern int sndpkt(int, int, int, struct sk_buff *);
40 extern int command(isdn_ctrl *);
41 extern int indicate_status(int, int, ulong, char*);
42 extern int reset(int);
44 static int identify_board(unsigned long, unsigned int);
46 static int __init sc_init(void)
52 unsigned long memsize = 0;
53 unsigned long features = 0;
55 unsigned char channels;
59 int last_base = IOBASE_MIN;
60 int probe_exhasted = 0;
63 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s Loaded\n", version);
65 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s\n", version);
67 pr_info("Copyright (C) 1996 SpellCaster Telecommunications Inc.\n");
69 while(b++ < MAX_CARDS - 1) {
70 pr_debug("Probing for adapter #%d\n", b);
72 * Initialize reusable variables
80 * See if we should probe for IO base
82 pr_debug("I/O Base for board %d is 0x%x, %s probe\n", b, io[b],
83 io[b] == 0 ? "will" : "won't");
86 * No, I/O Base has been provided
88 for (i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
89 if(!request_region(io[b] + i * 0x400, 1, "sc test")) {
90 pr_debug("request_region for 0x%x failed\n", io[b] + i * 0x400);
94 release_region(io[b] + i * 0x400, 1);
98 * Confirm the I/O Address with a test
101 pr_debug("I/O Address invalid.\n");
105 outb(0x18, io[b] + 0x400 * EXP_PAGE0);
106 if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
107 pr_debug("I/O Base 0x%x fails test\n",
108 io[b] + 0x400 * EXP_PAGE0);
114 * Yes, probe for I/O Base
117 pr_debug("All probe addresses exhasted, skipping\n");
120 pr_debug("Probing for I/O...\n");
121 for (i = last_base ; i <= IOBASE_MAX ; i += IOBASE_OFFSET) {
123 if (i == IOBASE_MAX) {
124 probe_exhasted = 1; /* No more addresses to probe */
125 pr_debug("End of Probes\n");
127 last_base = i + IOBASE_OFFSET;
128 pr_debug(" checking 0x%x...", i);
129 for ( j = 0 ; j < MAX_IO_REGS - 1 ; j++) {
130 if(!request_region(i + j * 0x400, 1, "sc test")) {
131 pr_debug("Failed\n");
135 release_region(i + j * 0x400, 1);
140 outb(0x18, io[b] + 0x400 * EXP_PAGE0);
141 if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
142 pr_debug("Failed by test\n");
145 pr_debug("Passed\n");
155 * See if we should probe for shared RAM
158 pr_debug("Doing a SAFE probe reset\n");
159 outb(0xFF, io[b] + RESET_OFFSET);
160 msleep_interruptible(10000);
162 pr_debug("RAM Base for board %d is 0x%lx, %s probe\n", b,
163 ram[b], ram[b] == 0 ? "will" : "won't");
167 * No, the RAM base has been provided
168 * Just look for a signature and ID the
171 if(request_region(ram[b], SRAM_PAGESIZE, "sc test")) {
172 pr_debug("request_region for RAM base 0x%lx succeeded\n", ram[b]);
173 model = identify_board(ram[b], io[b]);
174 release_region(ram[b], SRAM_PAGESIZE);
179 * Yes, probe for free RAM and look for
180 * a signature and id the board model
182 for (i = SRAM_MIN ; i < SRAM_MAX ; i += SRAM_PAGESIZE) {
183 pr_debug("Checking RAM address 0x%x...\n", i);
184 if(request_region(i, SRAM_PAGESIZE, "sc test")) {
185 pr_debug(" request_region succeeded\n");
186 model = identify_board(i, io[b]);
187 release_region(i, SRAM_PAGESIZE);
189 pr_debug(" Identified a %s\n",
194 pr_debug(" Unidentifed or inaccessible\n");
197 pr_debug(" request failed\n");
201 * See if we found free RAM and the board model
203 if(!ram[b] || model < 0) {
205 * Nope, there was no place in RAM for the
206 * board, or it couldn't be identified
208 pr_debug("Failed to find an adapter at 0x%lx\n", ram[b]);
213 * Set the board's magic number, memory size and page register
220 features = PRI_FEATURES;
228 features = BRI_FEATURES;
231 switch(ram[b] >> 12 & 0x0F) {
233 pr_debug("RAM Page register set to EXP_PAGE0\n");
238 pr_debug("RAM Page register set to EXP_PAGE1\n");
243 pr_debug("RAM Page register set to EXP_PAGE2\n");
248 pr_debug("RAM Page register set to EXP_PAGE3\n");
253 pr_debug("RAM base address doesn't fall on 16K boundary\n");
257 pr_debug("current IRQ: %d b: %d\n",irq[b],b);
260 * Make sure we got an IRQ
264 * No interrupt could be used
266 pr_debug("Failed to acquire an IRQ line\n");
271 * Horray! We found a board, Make sure we can register
274 interface = kzalloc(sizeof(isdn_if), GFP_KERNEL);
275 if (interface == NULL) {
277 * Oops, can't malloc isdn_if
282 interface->owner = THIS_MODULE;
283 interface->hl_hdrlen = 0;
284 interface->channels = channels;
285 interface->maxbufsize = BUFFER_SIZE;
286 interface->features = features;
287 interface->writebuf_skb = sndpkt;
288 interface->writecmd = NULL;
289 interface->command = command;
290 strcpy(interface->id, devname);
291 interface->id[2] = '0' + cinst;
294 * Allocate the board structure
296 sc_adapter[cinst] = kzalloc(sizeof(board), GFP_KERNEL);
297 if (sc_adapter[cinst] == NULL) {
299 * Oops, can't alloc memory for the board
304 spin_lock_init(&sc_adapter[cinst]->lock);
306 if(!register_isdn(interface)) {
308 * Oops, couldn't register for some reason
311 kfree(sc_adapter[cinst]);
315 sc_adapter[cinst]->card = interface;
316 sc_adapter[cinst]->driverId = interface->channels;
317 strcpy(sc_adapter[cinst]->devicename, interface->id);
318 sc_adapter[cinst]->nChannels = channels;
319 sc_adapter[cinst]->ramsize = memsize;
320 sc_adapter[cinst]->shmem_magic = magic;
321 sc_adapter[cinst]->shmem_pgport = pgport;
322 sc_adapter[cinst]->StartOnReset = 1;
325 * Allocate channels status structures
327 sc_adapter[cinst]->channel = kzalloc(sizeof(bchan) * channels, GFP_KERNEL);
328 if (sc_adapter[cinst]->channel == NULL) {
330 * Oops, can't alloc memory for the channels
332 indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL); /* Fix me */
334 kfree(sc_adapter[cinst]);
339 * Lock down the hardware resources
341 sc_adapter[cinst]->interrupt = irq[b];
342 if (request_irq(sc_adapter[cinst]->interrupt, interrupt_handler,
343 IRQF_DISABLED, interface->id, NULL))
345 kfree(sc_adapter[cinst]->channel);
346 indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL); /* Fix me */
348 kfree(sc_adapter[cinst]);
352 sc_adapter[cinst]->iobase = io[b];
353 for(i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
354 sc_adapter[cinst]->ioport[i] = io[b] + i * 0x400;
355 request_region(sc_adapter[cinst]->ioport[i], 1,
357 pr_debug("Requesting I/O Port %#x\n",
358 sc_adapter[cinst]->ioport[i]);
360 sc_adapter[cinst]->ioport[IRQ_SELECT] = io[b] + 0x2;
361 request_region(sc_adapter[cinst]->ioport[IRQ_SELECT], 1,
363 pr_debug("Requesting I/O Port %#x\n",
364 sc_adapter[cinst]->ioport[IRQ_SELECT]);
365 sc_adapter[cinst]->rambase = ram[b];
366 request_region(sc_adapter[cinst]->rambase, SRAM_PAGESIZE,
369 pr_info(" %s (%d) - %s %d channels IRQ %d, I/O Base 0x%x, RAM Base 0x%lx\n",
370 sc_adapter[cinst]->devicename,
371 sc_adapter[cinst]->driverId,
372 boardname[model], channels, irq[b], io[b], ram[b]);
375 * reset the adapter to put things in motion
383 pr_info("Failed to find any adapters, driver unloaded\n");
387 static void __exit sc_exit(void)
391 for(i = 0 ; i < cinst ; i++) {
392 pr_debug("Cleaning up after adapter %d\n", i);
396 del_timer(&(sc_adapter[i]->reset_timer));
397 del_timer(&(sc_adapter[i]->stat_timer));
400 * Tell I4L we're toast
402 indicate_status(i, ISDN_STAT_STOP, 0, NULL);
403 indicate_status(i, ISDN_STAT_UNLOAD, 0, NULL);
408 release_region(sc_adapter[i]->rambase, SRAM_PAGESIZE);
413 FREE_IRQ(sc_adapter[i]->interrupt, NULL);
416 * Reset for a clean start
418 outb(0xFF, sc_adapter[i]->ioport[SFT_RESET]);
421 * Release the I/O Port regions
423 for(j = 0 ; j < MAX_IO_REGS - 1; j++) {
424 release_region(sc_adapter[i]->ioport[j], 1);
425 pr_debug("Releasing I/O Port %#x\n",
426 sc_adapter[i]->ioport[j]);
428 release_region(sc_adapter[i]->ioport[IRQ_SELECT], 1);
429 pr_debug("Releasing I/O Port %#x\n",
430 sc_adapter[i]->ioport[IRQ_SELECT]);
433 * Release any memory we alloced
435 kfree(sc_adapter[i]->channel);
436 kfree(sc_adapter[i]->card);
437 kfree(sc_adapter[i]);
439 pr_info("SpellCaster ISA ISDN Adapter Driver Unloaded.\n");
442 static int identify_board(unsigned long rambase, unsigned int iobase)
452 pr_debug("Attempting to identify adapter @ 0x%lx io 0x%x\n",
456 * Enable the base pointer
458 outb(rambase >> 12, iobase + 0x2c00);
460 switch(rambase >> 12 & 0x0F) {
462 pgport = iobase + PG0_OFFSET;
463 pr_debug("Page Register offset is 0x%x\n", PG0_OFFSET);
467 pgport = iobase + PG1_OFFSET;
468 pr_debug("Page Register offset is 0x%x\n", PG1_OFFSET);
472 pgport = iobase + PG2_OFFSET;
473 pr_debug("Page Register offset is 0x%x\n", PG2_OFFSET);
477 pgport = iobase + PG3_OFFSET;
478 pr_debug("Page Register offset is 0x%x\n", PG3_OFFSET);
481 pr_debug("Invalid rambase 0x%lx\n", rambase);
486 * Try to identify a PRI card
488 outb(PRI_BASEPG_VAL, pgport);
489 msleep_interruptible(1000);
490 sig = readl(rambase + SIG_OFFSET);
491 pr_debug("Looking for a signature, got 0x%lx\n", sig);
496 * Try to identify a PRI card
498 outb(BRI_BASEPG_VAL, pgport);
499 msleep_interruptible(1000);
500 sig = readl(rambase + SIG_OFFSET);
501 pr_debug("Looking for a signature, got 0x%lx\n", sig);
510 sig = readl(rambase + SIG_OFFSET);
511 pr_debug("Looking for a signature, got 0x%lx\n", sig);
515 dpm = (DualPortMemory *) rambase;
517 memset(&sndmsg, 0, MSG_LEN);
518 sndmsg.msg_byte_cnt = 3;
519 sndmsg.type = cmReqType1;
520 sndmsg.class = cmReqClass0;
521 sndmsg.code = cmReqHWConfig;
522 memcpy_toio(&(dpm->req_queue[dpm->req_head++]), &sndmsg, MSG_LEN);
523 outb(0, iobase + 0x400);
524 pr_debug("Sent HWConfig message\n");
526 * Wait for the response
529 while((inb(iobase + FIFOSTAT_OFFSET) & RF_HAS_DATA) && x < 100) {
530 schedule_timeout_interruptible(1);
534 pr_debug("Timeout waiting for response\n");
538 memcpy_fromio(&rcvmsg, &(dpm->rsp_queue[dpm->rsp_tail]), MSG_LEN);
539 pr_debug("Got HWConfig response, status = 0x%x\n", rcvmsg.rsp_status);
540 memcpy(&hwci, &(rcvmsg.msg_data.HWCresponse), sizeof(HWConfig_pl));
541 pr_debug("Hardware Config: Interface: %s, RAM Size: %ld, Serial: %s\n"
542 " Part: %s, Rev: %s\n",
543 hwci.st_u_sense ? "S/T" : "U", hwci.ram_size,
544 hwci.serial_no, hwci.part_no, hwci.rev_no);
546 if(!strncmp(PRI_PARTNO, hwci.part_no, 6))
548 if(!strncmp(BRI_PARTNO, hwci.part_no, 6))
554 module_init(sc_init);
555 module_exit(sc_exit);