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};
33 static bool do_reset
= 0;
35 module_param_array(io
, int, NULL
, 0);
36 module_param_array(irq
, int, NULL
, 0);
37 module_param_array(ram
, int, 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
);
110 * Yes, probe for I/O Base
113 pr_debug("All probe addresses exhasted, skipping\n");
116 pr_debug("Probing for I/O...\n");
117 for (i
= last_base
; i
<= IOBASE_MAX
; i
+= IOBASE_OFFSET
) {
119 if (i
== IOBASE_MAX
) {
120 probe_exhasted
= 1; /* No more addresses to probe */
121 pr_debug("End of Probes\n");
123 last_base
= i
+ IOBASE_OFFSET
;
124 pr_debug(" checking 0x%x...", i
);
125 for ( j
= 0 ; j
< MAX_IO_REGS
- 1 ; j
++) {
126 if(!request_region(i
+ j
* 0x400, 1, "sc test")) {
127 pr_debug("Failed\n");
131 release_region(i
+ j
* 0x400, 1);
136 outb(0x18, io
[b
] + 0x400 * EXP_PAGE0
);
137 if(inb(io
[b
] + 0x400 * EXP_PAGE0
) != 0x18) {
138 pr_debug("Failed by test\n");
141 pr_debug("Passed\n");
151 * See if we should probe for shared RAM
154 pr_debug("Doing a SAFE probe reset\n");
155 outb(0xFF, io
[b
] + RESET_OFFSET
);
156 msleep_interruptible(10000);
158 pr_debug("RAM Base for board %d is 0x%lx, %s probe\n", b
,
159 ram
[b
], ram
[b
] == 0 ? "will" : "won't");
163 * No, the RAM base has been provided
164 * Just look for a signature and ID the
167 if(request_region(ram
[b
], SRAM_PAGESIZE
, "sc test")) {
168 pr_debug("request_region for RAM base 0x%lx succeeded\n", ram
[b
]);
169 model
= identify_board(ram
[b
], io
[b
]);
170 release_region(ram
[b
], SRAM_PAGESIZE
);
175 * Yes, probe for free RAM and look for
176 * a signature and id the board model
178 for (i
= SRAM_MIN
; i
< SRAM_MAX
; i
+= SRAM_PAGESIZE
) {
179 pr_debug("Checking RAM address 0x%x...\n", i
);
180 if(request_region(i
, SRAM_PAGESIZE
, "sc test")) {
181 pr_debug(" request_region succeeded\n");
182 model
= identify_board(i
, io
[b
]);
183 release_region(i
, SRAM_PAGESIZE
);
185 pr_debug(" Identified a %s\n",
190 pr_debug(" Unidentifed or inaccessible\n");
193 pr_debug(" request failed\n");
197 * See if we found free RAM and the board model
199 if(!ram
[b
] || model
< 0) {
201 * Nope, there was no place in RAM for the
202 * board, or it couldn't be identified
204 pr_debug("Failed to find an adapter at 0x%lx\n", ram
[b
]);
209 * Set the board's magic number, memory size and page register
216 features
= PRI_FEATURES
;
224 features
= BRI_FEATURES
;
227 switch(ram
[b
] >> 12 & 0x0F) {
229 pr_debug("RAM Page register set to EXP_PAGE0\n");
234 pr_debug("RAM Page register set to EXP_PAGE1\n");
239 pr_debug("RAM Page register set to EXP_PAGE2\n");
244 pr_debug("RAM Page register set to EXP_PAGE3\n");
249 pr_debug("RAM base address doesn't fall on 16K boundary\n");
253 pr_debug("current IRQ: %d b: %d\n",irq
[b
],b
);
256 * Make sure we got an IRQ
260 * No interrupt could be used
262 pr_debug("Failed to acquire an IRQ line\n");
267 * Horray! We found a board, Make sure we can register
270 interface
= kzalloc(sizeof(isdn_if
), GFP_KERNEL
);
271 if (interface
== NULL
) {
273 * Oops, can't malloc isdn_if
278 interface
->owner
= THIS_MODULE
;
279 interface
->hl_hdrlen
= 0;
280 interface
->channels
= channels
;
281 interface
->maxbufsize
= BUFFER_SIZE
;
282 interface
->features
= features
;
283 interface
->writebuf_skb
= sndpkt
;
284 interface
->writecmd
= NULL
;
285 interface
->command
= command
;
286 strcpy(interface
->id
, devname
);
287 interface
->id
[2] = '0' + cinst
;
290 * Allocate the board structure
292 sc_adapter
[cinst
] = kzalloc(sizeof(board
), GFP_KERNEL
);
293 if (sc_adapter
[cinst
] == NULL
) {
295 * Oops, can't alloc memory for the board
300 spin_lock_init(&sc_adapter
[cinst
]->lock
);
302 if(!register_isdn(interface
)) {
304 * Oops, couldn't register for some reason
307 kfree(sc_adapter
[cinst
]);
311 sc_adapter
[cinst
]->card
= interface
;
312 sc_adapter
[cinst
]->driverId
= interface
->channels
;
313 strcpy(sc_adapter
[cinst
]->devicename
, interface
->id
);
314 sc_adapter
[cinst
]->nChannels
= channels
;
315 sc_adapter
[cinst
]->ramsize
= memsize
;
316 sc_adapter
[cinst
]->shmem_magic
= magic
;
317 sc_adapter
[cinst
]->shmem_pgport
= pgport
;
318 sc_adapter
[cinst
]->StartOnReset
= 1;
321 * Allocate channels status structures
323 sc_adapter
[cinst
]->channel
= kzalloc(sizeof(bchan
) * channels
, GFP_KERNEL
);
324 if (sc_adapter
[cinst
]->channel
== NULL
) {
326 * Oops, can't alloc memory for the channels
328 indicate_status(cinst
, ISDN_STAT_UNLOAD
, 0, NULL
); /* Fix me */
330 kfree(sc_adapter
[cinst
]);
335 * Lock down the hardware resources
337 sc_adapter
[cinst
]->interrupt
= irq
[b
];
338 if (request_irq(sc_adapter
[cinst
]->interrupt
, interrupt_handler
,
339 IRQF_DISABLED
, interface
->id
,
340 (void *)(unsigned long) cinst
))
342 kfree(sc_adapter
[cinst
]->channel
);
343 indicate_status(cinst
, ISDN_STAT_UNLOAD
, 0, NULL
); /* Fix me */
345 kfree(sc_adapter
[cinst
]);
349 sc_adapter
[cinst
]->iobase
= io
[b
];
350 for(i
= 0 ; i
< MAX_IO_REGS
- 1 ; i
++) {
351 sc_adapter
[cinst
]->ioport
[i
] = io
[b
] + i
* 0x400;
352 request_region(sc_adapter
[cinst
]->ioport
[i
], 1,
354 pr_debug("Requesting I/O Port %#x\n",
355 sc_adapter
[cinst
]->ioport
[i
]);
357 sc_adapter
[cinst
]->ioport
[IRQ_SELECT
] = io
[b
] + 0x2;
358 request_region(sc_adapter
[cinst
]->ioport
[IRQ_SELECT
], 1,
360 pr_debug("Requesting I/O Port %#x\n",
361 sc_adapter
[cinst
]->ioport
[IRQ_SELECT
]);
362 sc_adapter
[cinst
]->rambase
= ram
[b
];
363 request_region(sc_adapter
[cinst
]->rambase
, SRAM_PAGESIZE
,
366 pr_info(" %s (%d) - %s %d channels IRQ %d, I/O Base 0x%x, RAM Base 0x%lx\n",
367 sc_adapter
[cinst
]->devicename
,
368 sc_adapter
[cinst
]->driverId
,
369 boardname
[model
], channels
, irq
[b
], io
[b
], ram
[b
]);
372 * reset the adapter to put things in motion
380 pr_info("Failed to find any adapters, driver unloaded\n");
384 static void __exit
sc_exit(void)
388 for(i
= 0 ; i
< cinst
; i
++) {
389 pr_debug("Cleaning up after adapter %d\n", i
);
393 del_timer(&(sc_adapter
[i
]->reset_timer
));
394 del_timer(&(sc_adapter
[i
]->stat_timer
));
397 * Tell I4L we're toast
399 indicate_status(i
, ISDN_STAT_STOP
, 0, NULL
);
400 indicate_status(i
, ISDN_STAT_UNLOAD
, 0, NULL
);
405 release_region(sc_adapter
[i
]->rambase
, SRAM_PAGESIZE
);
410 free_irq(sc_adapter
[i
]->interrupt
, NULL
);
413 * Reset for a clean start
415 outb(0xFF, sc_adapter
[i
]->ioport
[SFT_RESET
]);
418 * Release the I/O Port regions
420 for(j
= 0 ; j
< MAX_IO_REGS
- 1; j
++) {
421 release_region(sc_adapter
[i
]->ioport
[j
], 1);
422 pr_debug("Releasing I/O Port %#x\n",
423 sc_adapter
[i
]->ioport
[j
]);
425 release_region(sc_adapter
[i
]->ioport
[IRQ_SELECT
], 1);
426 pr_debug("Releasing I/O Port %#x\n",
427 sc_adapter
[i
]->ioport
[IRQ_SELECT
]);
430 * Release any memory we alloced
432 kfree(sc_adapter
[i
]->channel
);
433 kfree(sc_adapter
[i
]->card
);
434 kfree(sc_adapter
[i
]);
436 pr_info("SpellCaster ISA ISDN Adapter Driver Unloaded.\n");
439 static int identify_board(unsigned long rambase
, unsigned int iobase
)
449 pr_debug("Attempting to identify adapter @ 0x%lx io 0x%x\n",
453 * Enable the base pointer
455 outb(rambase
>> 12, iobase
+ 0x2c00);
457 switch(rambase
>> 12 & 0x0F) {
459 pgport
= iobase
+ PG0_OFFSET
;
460 pr_debug("Page Register offset is 0x%x\n", PG0_OFFSET
);
464 pgport
= iobase
+ PG1_OFFSET
;
465 pr_debug("Page Register offset is 0x%x\n", PG1_OFFSET
);
469 pgport
= iobase
+ PG2_OFFSET
;
470 pr_debug("Page Register offset is 0x%x\n", PG2_OFFSET
);
474 pgport
= iobase
+ PG3_OFFSET
;
475 pr_debug("Page Register offset is 0x%x\n", PG3_OFFSET
);
478 pr_debug("Invalid rambase 0x%lx\n", rambase
);
483 * Try to identify a PRI card
485 outb(PRI_BASEPG_VAL
, pgport
);
486 msleep_interruptible(1000);
487 sig
= readl(rambase
+ SIG_OFFSET
);
488 pr_debug("Looking for a signature, got 0x%lx\n", sig
);
493 * Try to identify a PRI card
495 outb(BRI_BASEPG_VAL
, pgport
);
496 msleep_interruptible(1000);
497 sig
= readl(rambase
+ SIG_OFFSET
);
498 pr_debug("Looking for a signature, got 0x%lx\n", sig
);
507 sig
= readl(rambase
+ SIG_OFFSET
);
508 pr_debug("Looking for a signature, got 0x%lx\n", sig
);
512 dpm
= (DualPortMemory
*) rambase
;
514 memset(&sndmsg
, 0, MSG_LEN
);
515 sndmsg
.msg_byte_cnt
= 3;
516 sndmsg
.type
= cmReqType1
;
517 sndmsg
.class = cmReqClass0
;
518 sndmsg
.code
= cmReqHWConfig
;
519 memcpy_toio(&(dpm
->req_queue
[dpm
->req_head
++]), &sndmsg
, MSG_LEN
);
520 outb(0, iobase
+ 0x400);
521 pr_debug("Sent HWConfig message\n");
523 * Wait for the response
526 while((inb(iobase
+ FIFOSTAT_OFFSET
) & RF_HAS_DATA
) && x
< 100) {
527 schedule_timeout_interruptible(1);
531 pr_debug("Timeout waiting for response\n");
535 memcpy_fromio(&rcvmsg
, &(dpm
->rsp_queue
[dpm
->rsp_tail
]), MSG_LEN
);
536 pr_debug("Got HWConfig response, status = 0x%x\n", rcvmsg
.rsp_status
);
537 memcpy(&hwci
, &(rcvmsg
.msg_data
.HWCresponse
), sizeof(HWConfig_pl
));
538 pr_debug("Hardware Config: Interface: %s, RAM Size: %ld, Serial: %s\n"
539 " Part: %s, Rev: %s\n",
540 hwci
.st_u_sense
? "S/T" : "U", hwci
.ram_size
,
541 hwci
.serial_no
, hwci
.part_no
, hwci
.rev_no
);
543 if(!strncmp(PRI_PARTNO
, hwci
.part_no
, 6))
545 if(!strncmp(BRI_PARTNO
, hwci
.part_no
, 6))
551 module_init(sc_init
);
552 module_exit(sc_exit
);