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>
16 MODULE_DESCRIPTION("ISDN4Linux: Driver for Spellcaster card");
17 MODULE_AUTHOR("Spellcaster Telecommunications Inc.");
18 MODULE_LICENSE("GPL");
20 board *sc_adapter[MAX_CARDS];
23 static char devname[] = "scX";
24 static const char version[] = "2.0b1";
26 static const char *boardname[] = { "DataCommute/BRI", "DataCommute/PRI", "TeleCommute/BRI" };
28 /* insmod set parameters */
29 static unsigned int io[] = {0,0,0,0};
30 static unsigned char irq[] = {0,0,0,0};
31 static unsigned long ram[] = {0,0,0,0};
32 static int do_reset = 0;
34 module_param_array(io, int, NULL, 0);
35 module_param_array(irq, int, NULL, 0);
36 module_param_array(ram, int, NULL, 0);
37 module_param(do_reset, bool, 0);
39 static int identify_board(unsigned long, unsigned int);
41 static int __init sc_init(void)
47 unsigned long memsize = 0;
48 unsigned long features = 0;
50 unsigned char channels;
54 int last_base = IOBASE_MIN;
55 int probe_exhasted = 0;
58 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s Loaded\n", version);
60 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s\n", version);
62 pr_info("Copyright (C) 1996 SpellCaster Telecommunications Inc.\n");
64 while(b++ < MAX_CARDS - 1) {
65 pr_debug("Probing for adapter #%d\n", b);
67 * Initialize reusable variables
75 * See if we should probe for IO base
77 pr_debug("I/O Base for board %d is 0x%x, %s probe\n", b, io[b],
78 io[b] == 0 ? "will" : "won't");
81 * No, I/O Base has been provided
83 for (i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
84 if(!request_region(io[b] + i * 0x400, 1, "sc test")) {
85 pr_debug("request_region for 0x%x failed\n", io[b] + i * 0x400);
89 release_region(io[b] + i * 0x400, 1);
93 * Confirm the I/O Address with a test
96 pr_debug("I/O Address invalid.\n");
100 outb(0x18, io[b] + 0x400 * EXP_PAGE0);
101 if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
102 pr_debug("I/O Base 0x%x fails test\n",
103 io[b] + 0x400 * EXP_PAGE0);
109 * Yes, probe for I/O Base
112 pr_debug("All probe addresses exhasted, 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");
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);
174 * Yes, probe for free RAM and look for
175 * a signature and id the board model
177 for (i = SRAM_MIN ; i < SRAM_MAX ; i += SRAM_PAGESIZE) {
178 pr_debug("Checking RAM address 0x%x...\n", i);
179 if(request_region(i, SRAM_PAGESIZE, "sc test")) {
180 pr_debug(" request_region succeeded\n");
181 model = identify_board(i, io[b]);
182 release_region(i, SRAM_PAGESIZE);
184 pr_debug(" Identified a %s\n",
189 pr_debug(" Unidentifed or inaccessible\n");
192 pr_debug(" request failed\n");
196 * See if we found free RAM and the board model
198 if(!ram[b] || model < 0) {
200 * Nope, there was no place in RAM for the
201 * board, or it couldn't be identified
203 pr_debug("Failed to find an adapter at 0x%lx\n", ram[b]);
208 * Set the board's magic number, memory size and page register
215 features = PRI_FEATURES;
223 features = BRI_FEATURES;
226 switch(ram[b] >> 12 & 0x0F) {
228 pr_debug("RAM Page register set to EXP_PAGE0\n");
233 pr_debug("RAM Page register set to EXP_PAGE1\n");
238 pr_debug("RAM Page register set to EXP_PAGE2\n");
243 pr_debug("RAM Page register set to EXP_PAGE3\n");
248 pr_debug("RAM base address doesn't fall on 16K boundary\n");
252 pr_debug("current IRQ: %d b: %d\n",irq[b],b);
255 * Make sure we got an IRQ
259 * No interrupt could be used
261 pr_debug("Failed to acquire an IRQ line\n");
266 * Horray! We found a board, Make sure we can register
269 interface = kzalloc(sizeof(isdn_if), GFP_KERNEL);
270 if (interface == NULL) {
272 * Oops, can't malloc isdn_if
277 interface->owner = THIS_MODULE;
278 interface->hl_hdrlen = 0;
279 interface->channels = channels;
280 interface->maxbufsize = BUFFER_SIZE;
281 interface->features = features;
282 interface->writebuf_skb = sndpkt;
283 interface->writecmd = NULL;
284 interface->command = command;
285 strcpy(interface->id, devname);
286 interface->id[2] = '0' + cinst;
289 * Allocate the board structure
291 sc_adapter[cinst] = kzalloc(sizeof(board), GFP_KERNEL);
292 if (sc_adapter[cinst] == NULL) {
294 * Oops, can't alloc memory for the board
299 spin_lock_init(&sc_adapter[cinst]->lock);
301 if(!register_isdn(interface)) {
303 * Oops, couldn't register for some reason
306 kfree(sc_adapter[cinst]);
310 sc_adapter[cinst]->card = interface;
311 sc_adapter[cinst]->driverId = interface->channels;
312 strcpy(sc_adapter[cinst]->devicename, interface->id);
313 sc_adapter[cinst]->nChannels = channels;
314 sc_adapter[cinst]->ramsize = memsize;
315 sc_adapter[cinst]->shmem_magic = magic;
316 sc_adapter[cinst]->shmem_pgport = pgport;
317 sc_adapter[cinst]->StartOnReset = 1;
320 * Allocate channels status structures
322 sc_adapter[cinst]->channel = kzalloc(sizeof(bchan) * channels, GFP_KERNEL);
323 if (sc_adapter[cinst]->channel == NULL) {
325 * Oops, can't alloc memory for the channels
327 indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL); /* Fix me */
329 kfree(sc_adapter[cinst]);
334 * Lock down the hardware resources
336 sc_adapter[cinst]->interrupt = irq[b];
337 if (request_irq(sc_adapter[cinst]->interrupt, interrupt_handler,
338 IRQF_DISABLED, interface->id,
339 (void *)(unsigned long) cinst))
341 kfree(sc_adapter[cinst]->channel);
342 indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL); /* Fix me */
344 kfree(sc_adapter[cinst]);
348 sc_adapter[cinst]->iobase = io[b];
349 for(i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
350 sc_adapter[cinst]->ioport[i] = io[b] + i * 0x400;
351 request_region(sc_adapter[cinst]->ioport[i], 1,
353 pr_debug("Requesting I/O Port %#x\n",
354 sc_adapter[cinst]->ioport[i]);
356 sc_adapter[cinst]->ioport[IRQ_SELECT] = io[b] + 0x2;
357 request_region(sc_adapter[cinst]->ioport[IRQ_SELECT], 1,
359 pr_debug("Requesting I/O Port %#x\n",
360 sc_adapter[cinst]->ioport[IRQ_SELECT]);
361 sc_adapter[cinst]->rambase = ram[b];
362 request_region(sc_adapter[cinst]->rambase, SRAM_PAGESIZE,
365 pr_info(" %s (%d) - %s %d channels IRQ %d, I/O Base 0x%x, RAM Base 0x%lx\n",
366 sc_adapter[cinst]->devicename,
367 sc_adapter[cinst]->driverId,
368 boardname[model], channels, irq[b], io[b], ram[b]);
371 * reset the adapter to put things in motion
379 pr_info("Failed to find any adapters, driver unloaded\n");
383 static void __exit sc_exit(void)
387 for(i = 0 ; i < cinst ; i++) {
388 pr_debug("Cleaning up after adapter %d\n", i);
392 del_timer(&(sc_adapter[i]->reset_timer));
393 del_timer(&(sc_adapter[i]->stat_timer));
396 * Tell I4L we're toast
398 indicate_status(i, ISDN_STAT_STOP, 0, NULL);
399 indicate_status(i, ISDN_STAT_UNLOAD, 0, NULL);
404 release_region(sc_adapter[i]->rambase, SRAM_PAGESIZE);
409 free_irq(sc_adapter[i]->interrupt, NULL);
412 * Reset for a clean start
414 outb(0xFF, sc_adapter[i]->ioport[SFT_RESET]);
417 * Release the I/O Port regions
419 for(j = 0 ; j < MAX_IO_REGS - 1; j++) {
420 release_region(sc_adapter[i]->ioport[j], 1);
421 pr_debug("Releasing I/O Port %#x\n",
422 sc_adapter[i]->ioport[j]);
424 release_region(sc_adapter[i]->ioport[IRQ_SELECT], 1);
425 pr_debug("Releasing I/O Port %#x\n",
426 sc_adapter[i]->ioport[IRQ_SELECT]);
429 * Release any memory we alloced
431 kfree(sc_adapter[i]->channel);
432 kfree(sc_adapter[i]->card);
433 kfree(sc_adapter[i]);
435 pr_info("SpellCaster ISA ISDN Adapter Driver Unloaded.\n");
438 static int identify_board(unsigned long rambase, unsigned int iobase)
448 pr_debug("Attempting to identify adapter @ 0x%lx io 0x%x\n",
452 * Enable the base pointer
454 outb(rambase >> 12, iobase + 0x2c00);
456 switch(rambase >> 12 & 0x0F) {
458 pgport = iobase + PG0_OFFSET;
459 pr_debug("Page Register offset is 0x%x\n", PG0_OFFSET);
463 pgport = iobase + PG1_OFFSET;
464 pr_debug("Page Register offset is 0x%x\n", PG1_OFFSET);
468 pgport = iobase + PG2_OFFSET;
469 pr_debug("Page Register offset is 0x%x\n", PG2_OFFSET);
473 pgport = iobase + PG3_OFFSET;
474 pr_debug("Page Register offset is 0x%x\n", PG3_OFFSET);
477 pr_debug("Invalid rambase 0x%lx\n", rambase);
482 * Try to identify a PRI card
484 outb(PRI_BASEPG_VAL, pgport);
485 msleep_interruptible(1000);
486 sig = readl(rambase + SIG_OFFSET);
487 pr_debug("Looking for a signature, got 0x%lx\n", sig);
492 * Try to identify a PRI card
494 outb(BRI_BASEPG_VAL, pgport);
495 msleep_interruptible(1000);
496 sig = readl(rambase + SIG_OFFSET);
497 pr_debug("Looking for a signature, got 0x%lx\n", sig);
506 sig = readl(rambase + SIG_OFFSET);
507 pr_debug("Looking for a signature, got 0x%lx\n", sig);
511 dpm = (DualPortMemory *) rambase;
513 memset(&sndmsg, 0, MSG_LEN);
514 sndmsg.msg_byte_cnt = 3;
515 sndmsg.type = cmReqType1;
516 sndmsg.class = cmReqClass0;
517 sndmsg.code = cmReqHWConfig;
518 memcpy_toio(&(dpm->req_queue[dpm->req_head++]), &sndmsg, MSG_LEN);
519 outb(0, iobase + 0x400);
520 pr_debug("Sent HWConfig message\n");
522 * Wait for the response
525 while((inb(iobase + FIFOSTAT_OFFSET) & RF_HAS_DATA) && x < 100) {
526 schedule_timeout_interruptible(1);
530 pr_debug("Timeout waiting for response\n");
534 memcpy_fromio(&rcvmsg, &(dpm->rsp_queue[dpm->rsp_tail]), MSG_LEN);
535 pr_debug("Got HWConfig response, status = 0x%x\n", rcvmsg.rsp_status);
536 memcpy(&hwci, &(rcvmsg.msg_data.HWCresponse), sizeof(HWConfig_pl));
537 pr_debug("Hardware Config: Interface: %s, RAM Size: %ld, Serial: %s\n"
538 " Part: %s, Rev: %s\n",
539 hwci.st_u_sense ? "S/T" : "U", hwci.ram_size,
540 hwci.serial_no, hwci.part_no, hwci.rev_no);
542 if(!strncmp(PRI_PARTNO, hwci.part_no, 6))
544 if(!strncmp(BRI_PARTNO, hwci.part_no, 6))
550 module_init(sc_init);
551 module_exit(sc_exit);