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>
13 MODULE_DESCRIPTION("ISDN4Linux: Driver for Spellcaster card");
14 MODULE_AUTHOR("Spellcaster Telecommunications Inc.");
15 MODULE_LICENSE("GPL");
16 MODULE_PARM( io, "1-" __MODULE_STRING(MAX_CARDS) "i");
17 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
18 MODULE_PARM(ram, "1-" __MODULE_STRING(MAX_CARDS) "i");
19 MODULE_PARM(do_reset, "i");
21 board *adapter[MAX_CARDS];
24 static char devname[] = "scX";
25 const char version[] = "2.0b1";
27 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};
33 static int do_reset = 0;
35 static int sup_irq[] = { 11, 10, 9, 5, 12, 14, 7, 3, 4, 6 };
38 extern void interrupt_handler(int, void *, struct pt_regs *);
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 int identify_board(unsigned long, unsigned int);
46 int irq_supported(int irq_x)
49 for(i=0 ; i < MAX_IRQS ; i++) {
50 if(sup_irq[i] == irq_x)
56 static int __init sc_init(void)
62 unsigned long memsize = 0;
63 unsigned long features = 0;
65 unsigned char channels;
69 int last_base = IOBASE_MIN;
70 int probe_exhasted = 0;
73 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s Loaded\n", version);
75 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s\n", version);
77 pr_info("Copyright (C) 1996 SpellCaster Telecommunications Inc.\n");
79 while(b++ < MAX_CARDS - 1) {
80 pr_debug("Probing for adapter #%d\n", b);
82 * Initialize reusable variables
90 * See if we should probe for IO base
92 pr_debug("I/O Base for board %d is 0x%x, %s probe\n", b, io[b],
93 io[b] == 0 ? "will" : "won't");
96 * No, I/O Base has been provided
98 for (i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
99 if(check_region(io[b] + i * 0x400, 1)) {
100 pr_debug("check_region for 0x%x failed\n", io[b] + i * 0x400);
107 * Confirm the I/O Address with a test
110 pr_debug("I/O Address 0x%x is in use.\n");
114 outb(0x18, io[b] + 0x400 * EXP_PAGE0);
115 if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
116 pr_debug("I/O Base 0x%x fails test\n");
122 * Yes, probe for I/O Base
125 pr_debug("All probe addresses exhasted, skipping\n");
128 pr_debug("Probing for I/O...\n");
129 for (i = last_base ; i <= IOBASE_MAX ; i += IOBASE_OFFSET) {
131 if (i == IOBASE_MAX) {
132 probe_exhasted = 1; /* No more addresses to probe */
133 pr_debug("End of Probes\n");
135 last_base = i + IOBASE_OFFSET;
136 pr_debug(" checking 0x%x...", i);
137 for ( j = 0 ; j < MAX_IO_REGS - 1 ; j++) {
138 if(check_region(i + j * 0x400, 1)) {
139 pr_debug("Failed\n");
147 outb(0x18, io[b] + 0x400 * EXP_PAGE0);
148 if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
149 pr_debug("Failed by test\n");
152 pr_debug("Passed\n");
162 * See if we should probe for shared RAM
165 pr_debug("Doing a SAFE probe reset\n");
166 outb(0xFF, io[b] + RESET_OFFSET);
167 set_current_state(TASK_INTERRUPTIBLE);
168 schedule_timeout(milliseconds(10000));
170 pr_debug("RAM Base for board %d is 0x%x, %s probe\n", b, ram[b],
171 ram[b] == 0 ? "will" : "won't");
175 * No, the RAM base has been provided
176 * Just look for a signature and ID the
179 if(!check_region(ram[b], SRAM_PAGESIZE)) {
180 pr_debug("check_region for RAM base 0x%x succeeded\n", ram[b]);
181 model = identify_board(ram[b], io[b]);
186 * Yes, probe for free RAM and look for
187 * a signature and id the board model
189 for (i = SRAM_MIN ; i < SRAM_MAX ; i += SRAM_PAGESIZE) {
190 pr_debug("Checking RAM address 0x%x...\n", i);
191 if(!check_region(i, SRAM_PAGESIZE)) {
192 pr_debug(" check_region succeeded\n");
193 model = identify_board(i, io[b]);
195 pr_debug(" Identified a %s\n",
200 pr_debug(" Unidentifed or inaccessible\n");
203 pr_debug(" check_region failed\n");
207 * See if we found free RAM and the board model
209 if(!ram[b] || model < 0) {
211 * Nope, there was no place in RAM for the
212 * board, or it couldn't be identified
214 pr_debug("Failed to find an adapter at 0x%x\n", ram[b]);
219 * Set the board's magic number, memory size and page register
226 features = PRI_FEATURES;
234 features = BRI_FEATURES;
237 switch(ram[b] >> 12 & 0x0F) {
239 pr_debug("RAM Page register set to EXP_PAGE0\n");
244 pr_debug("RAM Page register set to EXP_PAGE1\n");
249 pr_debug("RAM Page register set to EXP_PAGE2\n");
254 pr_debug("RAM Page register set to EXP_PAGE3\n");
259 pr_debug("RAM base address doesn't fall on 16K boundary\n");
263 pr_debug("current IRQ: %d b: %d\n",irq[b],b);
265 * See if we should probe for an irq
269 * No we were given one
270 * See that it is supported and free
272 pr_debug("Trying for IRQ: %d\n",irq[b]);
273 if (irq_supported(irq[b])) {
274 if(REQUEST_IRQ(irq[b], interrupt_handler,
275 SA_PROBE, "sc_probe", NULL)) {
276 pr_debug("IRQ %d is already in use\n",
280 FREE_IRQ(irq[b], NULL);
285 * Yes, we need to probe for an IRQ
287 pr_debug("Probing for IRQ...\n");
288 for (i = 0; i < MAX_IRQS ; i++) {
289 if(!REQUEST_IRQ(sup_irq[i], interrupt_handler, SA_PROBE, "sc_probe", NULL)) {
290 pr_debug("Probed for and found IRQ %d\n", sup_irq[i]);
291 FREE_IRQ(sup_irq[i], NULL);
299 * Make sure we got an IRQ
303 * No interrupt could be used
305 pr_debug("Failed to acquire an IRQ line\n");
310 * Horray! We found a board, Make sure we can register
313 interface = kmalloc(sizeof(isdn_if), GFP_KERNEL);
314 if (interface == NULL) {
316 * Oops, can't malloc isdn_if
320 memset(interface, 0, sizeof(isdn_if));
322 interface->hl_hdrlen = 0;
323 interface->channels = channels;
324 interface->maxbufsize = BUFFER_SIZE;
325 interface->features = features;
326 interface->writebuf_skb = sndpkt;
327 interface->writecmd = NULL;
328 interface->command = command;
329 strcpy(interface->id, devname);
330 interface->id[2] = '0' + cinst;
333 * Allocate the board structure
335 adapter[cinst] = kmalloc(sizeof(board), GFP_KERNEL);
336 if (adapter[cinst] == NULL) {
338 * Oops, can't alloc memory for the board
343 memset(adapter[cinst], 0, sizeof(board));
345 if(!register_isdn(interface)) {
347 * Oops, couldn't register for some reason
350 kfree(adapter[cinst]);
354 adapter[cinst]->card = interface;
355 adapter[cinst]->driverId = interface->channels;
356 strcpy(adapter[cinst]->devicename, interface->id);
357 adapter[cinst]->nChannels = channels;
358 adapter[cinst]->ramsize = memsize;
359 adapter[cinst]->shmem_magic = magic;
360 adapter[cinst]->shmem_pgport = pgport;
361 adapter[cinst]->StartOnReset = 1;
364 * Allocate channels status structures
366 adapter[cinst]->channel = kmalloc(sizeof(bchan) * channels, GFP_KERNEL);
367 if (adapter[cinst]->channel == NULL) {
369 * Oops, can't alloc memory for the channels
371 indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL); /* Fix me */
373 kfree(adapter[cinst]);
376 memset(adapter[cinst]->channel, 0, sizeof(bchan) * channels);
379 * Lock down the hardware resources
381 adapter[cinst]->interrupt = irq[b];
382 REQUEST_IRQ(adapter[cinst]->interrupt, interrupt_handler, SA_INTERRUPT,
383 interface->id, NULL);
384 adapter[cinst]->iobase = io[b];
385 for(i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
386 adapter[cinst]->ioport[i] = io[b] + i * 0x400;
387 request_region(adapter[cinst]->ioport[i], 1, interface->id);
388 pr_debug("Requesting I/O Port %#x\n", adapter[cinst]->ioport[i]);
390 adapter[cinst]->ioport[IRQ_SELECT] = io[b] + 0x2;
391 request_region(adapter[cinst]->ioport[IRQ_SELECT], 1, interface->id);
392 pr_debug("Requesting I/O Port %#x\n", adapter[cinst]->ioport[IRQ_SELECT]);
393 adapter[cinst]->rambase = ram[b];
394 request_region(adapter[cinst]->rambase, SRAM_PAGESIZE, interface->id);
396 pr_info(" %s (%d) - %s %d channels IRQ %d, I/O Base 0x%x, RAM Base 0x%lx\n",
397 adapter[cinst]->devicename, adapter[cinst]->driverId,
398 boardname[model], channels, irq[b], io[b], ram[b]);
401 * reset the adapter to put things in motion
409 pr_info("Failed to find any adapters, driver unloaded\n");
413 static void __exit sc_exit(void)
417 for(i = 0 ; i < cinst ; i++) {
418 pr_debug("Cleaning up after adapter %d\n", i);
422 del_timer(&(adapter[i]->reset_timer));
423 del_timer(&(adapter[i]->stat_timer));
426 * Tell I4L we're toast
428 indicate_status(i, ISDN_STAT_STOP, 0, NULL);
429 indicate_status(i, ISDN_STAT_UNLOAD, 0, NULL);
434 release_region(adapter[i]->rambase, SRAM_PAGESIZE);
439 FREE_IRQ(adapter[i]->interrupt, NULL);
442 * Reset for a clean start
444 outb(0xFF, adapter[i]->ioport[SFT_RESET]);
447 * Release the I/O Port regions
449 for(j = 0 ; j < MAX_IO_REGS - 1; j++) {
450 release_region(adapter[i]->ioport[j], 1);
451 pr_debug("Releasing I/O Port %#x\n", adapter[i]->ioport[j]);
453 release_region(adapter[i]->ioport[IRQ_SELECT], 1);
454 pr_debug("Releasing I/O Port %#x\n", adapter[i]->ioport[IRQ_SELECT]);
457 * Release any memory we alloced
459 kfree(adapter[i]->channel);
460 kfree(adapter[i]->card);
463 pr_info("SpellCaster ISA ISDN Adapter Driver Unloaded.\n");
466 int identify_board(unsigned long rambase, unsigned int iobase)
476 pr_debug("Attempting to identify adapter @ 0x%x io 0x%x\n",
480 * Enable the base pointer
482 outb(rambase >> 12, iobase + 0x2c00);
484 switch(rambase >> 12 & 0x0F) {
486 pgport = iobase + PG0_OFFSET;
487 pr_debug("Page Register offset is 0x%x\n", PG0_OFFSET);
491 pgport = iobase + PG1_OFFSET;
492 pr_debug("Page Register offset is 0x%x\n", PG1_OFFSET);
496 pgport = iobase + PG2_OFFSET;
497 pr_debug("Page Register offset is 0x%x\n", PG2_OFFSET);
501 pgport = iobase + PG3_OFFSET;
502 pr_debug("Page Register offset is 0x%x\n", PG3_OFFSET);
505 pr_debug("Invalid rambase 0x%lx\n", rambase);
510 * Try to identify a PRI card
512 outb(PRI_BASEPG_VAL, pgport);
513 set_current_state(TASK_INTERRUPTIBLE);
514 schedule_timeout(HZ);
515 sig = readl(rambase + SIG_OFFSET);
516 pr_debug("Looking for a signature, got 0x%x\n", sig);
521 * Try to identify a PRI card
523 outb(BRI_BASEPG_VAL, pgport);
524 set_current_state(TASK_INTERRUPTIBLE);
525 schedule_timeout(HZ);
526 sig = readl(rambase + SIG_OFFSET);
527 pr_debug("Looking for a signature, got 0x%x\n", sig);
536 sig = readl(rambase + SIG_OFFSET);
537 pr_debug("Looking for a signature, got 0x%x\n", sig);
541 dpm = (DualPortMemory *) rambase;
543 memset(&sndmsg, 0, MSG_LEN);
544 sndmsg.msg_byte_cnt = 3;
545 sndmsg.type = cmReqType1;
546 sndmsg.class = cmReqClass0;
547 sndmsg.code = cmReqHWConfig;
548 memcpy_toio(&(dpm->req_queue[dpm->req_head++]), &sndmsg, MSG_LEN);
549 outb(0, iobase + 0x400);
550 pr_debug("Sent HWConfig message\n");
552 * Wait for the response
555 while((inb(iobase + FIFOSTAT_OFFSET) & RF_HAS_DATA) && x < 100) {
556 set_current_state(TASK_INTERRUPTIBLE);
561 pr_debug("Timeout waiting for response\n");
565 memcpy_fromio(&rcvmsg, &(dpm->rsp_queue[dpm->rsp_tail]), MSG_LEN);
566 pr_debug("Got HWConfig response, status = 0x%x\n", rcvmsg.rsp_status);
567 memcpy(&hwci, &(rcvmsg.msg_data.HWCresponse), sizeof(HWConfig_pl));
568 pr_debug("Hardware Config: Interface: %s, RAM Size: %d, Serial: %s\n"
569 " Part: %s, Rev: %s\n",
570 hwci.st_u_sense ? "S/T" : "U", hwci.ram_size,
571 hwci.serial_no, hwci.part_no, hwci.rev_no);
573 if(!strncmp(PRI_PARTNO, hwci.part_no, 6))
575 if(!strncmp(BRI_PARTNO, hwci.part_no, 6))
581 module_init(sc_init);
582 module_exit(sc_exit);